So, I have a recyclerview inside a recyclerview. What I wanna do is click the image inside the first item (at position 0) in the child recyclerview. How am i gonna do that? Nothing works in every answer. Thank you.
So, I have a recyclerview inside a recyclerview. What I wanna do is click the image inside the first item (at position 0) in the child recyclerview.
Consulting the documentation we see there are a few options.
So you want to perform an action on the main recycler. And that action is to perform another recycler view action on a child recycler. And that action it to click an image. In Espresso terms, that translates to something like:
// Starting from the bottom up, you want to click on the image
val clickOnImage = actionOnItem(withId(R.id.image_view), click())
// Next you want to do the above click action on the specific view in the child recycler
val clickOnImageOnChildRecycler = actionOnItemAtPosition(0, clickOnImage)
// Next you want to do the above on the specific child recycler
val clickOnChildReycler = actionOnItemAtPosition(INDEX_OF_CHILD_RECYCLER, clickOnImageOnChildRecycler)
// Finally, you want to do the clicking of a child on the main recycler
onView(withId(R.id.parent_recycler_view)).perform(clickOnChildReycler)
Then you can simplify and inline:
onView(withId(R.id.parent_recycler_view)) // On the parent recycler ...
.perform(actionOnItemAtPosition(INDEX_OF_CHILD_RECYCLER, // On the specific child recycler ...
actionOnItemAtPosition(0, // On the first item ...
actionOnItem(withId(R.id.image_view), click()))) // Click the image ...
Should go without saying that I did not test this, but hopefully it works or at least sets you on the right path.
This is how I did it:
//Scroll to the 2nd Position of Parent Recyclerview
onView(withId(PARENT_RECYCLERVIEW_ID)).perform(actionOnItemAtPosition(1, scrollTo()));
//Added this line Just to make sure I'm already in the Child Recyclerview
onView(withId(PARENT_RECYCLERVIEW_ID)).perform(actionOnItemAtPosition(1, CustomViewAction.clickChildViewWithId(CHILD_RECYCLERVIEW_ID)));
//Click the Image inside the first item in the child recyclerview
onView(allOf(withId(CHILD_RECYCLERVIEW_ID), withParent(withRecyclerView(PARENT_RECYCLERVIEW_ID).atPosition(1)))).perform(actionOnItemAtPosition(0, click()));
Related
if each item of recycler view has multiple items (Button and textview)
I have interface to handle Recycler's item click , I want upon clicking Recycler view item , be able to update text view and button but from outside of the adapter class ,
how could I achieve this ?
thanks
call adapter.notifyItemChanged(position) on the adapter to change only a single object
EDIT: Based on your comment:
you don't need reference of textview. Just update the state of the item in the list at that position.
For exmaple, take a boolean, isclicked and mark it as true for position = x
Hadle the text and UI inside the onbindviewholder only based on the
isClicked = true or false.
You can use AdapterObject.NotifyDataSetChanged instead of setting the adapter again after the data in list gets updated.
I have created RecyclerView with list of clients (from 0 till 14). I need maximum down state. For this purpose I am using method RecyclerView.scrollToPosition(14).
When RecyclerView are placing in Activity, I have the good result.
IMAGE: RecyclerView are placing in Activity
And now I am inflating new RecyclerView and put it in Dialog. The Adapter of RecyclerView the same. Method RecyclerView.scrollToPosition(14) give me the bad result (scroll is not set on the 14th position, by the way I can pull it to the down manually):
IMAGE: Bad result in Dialog
Why it happens? I think the problem in parent (Dialog) of RecyclerView. But I don't know what to do.
UPDATED! I think this is a really bug of RecyclerView. Because with ListView is all right (in my case with setSelection(14)).
Because your interaction panel hide the last element. Position your recycler view above the panel.
I have a standard recyclerview with 20 elements in a fragment. Now what I want is to open the view from the fifth element. Please note that I don't want to smoothscrolltoposition. I want it to be open from the fifth element.
UPDATE:
I want it to be opened with this without scroll-
I want a functionality where the user can still scroll up-
And I have already populated my recyclerview.
You can try any of these two methods according to your use case. It won't be a smooth scroll.
If you want to scroll to a specific position but that position is the adapter's position and not the RecyclerView's item position. You can only achieve this through the LayoutManager.
recylerView.getLayoutManager().scrollToPosition(adapterPosition);
If it is RecyclerView's item position:
recylerView.scrollToPosition(itemPosition);
Hope this helps you out.
References: 1 and 2.
NOTE:
Try to call this before onStart callback method. Because by that time, activity/fragment is not visible to the user and thus scroll won't be visible.
Example - If the RecylerView is inside an Activity, call it from onCreate.
I am building ExpandableListView in my android app. I have made child xml layout that repeats upto specific numbers. I have a Delete Button in my xml layout with id set in xml as btnDelete. By clicking delete button I want to delete specific child.
e.g:
Group:
Child 1 Delete
Child 2 Delete
Chile 3 Delete
Now my problem is that how to identify that which child delete button is clicked. As all delete buttons have same ID in my xml layout. Please suggest me a solution.
In your adapter, where you inflate your row layout, you can set a tag (with the setTag () method) to each delete button. The tag can be the row position that this delete button corresponds to.
Later, when someone clicks the button, in the onClickListener, you get the button tag (with v.getTag ()), parse it to integer and delete the row at that position.
I have a custom listview defined in my xml layout file. I can add items to this ListView inside onCreate method, through an array adapter.
However when I add items from another content view and then go back to the content view with the ListView all the items are gone and there's nothing listed. Even after calling .notifyDataSetChanged();
It seems like I can only add to the list when the content view containing the ListView is currently being displayed. Is this the default behavior?
Failed attempted workaround
I used another array to keep the newly added items and then try to add them when the ListView became visible again. I had to override onContentChanged() to do so but then no items were added still.
So the main question is
How can I dynamically add items to the ListView even if it's out of sight and still preserve the old items?
PS: I have to say the Android API is one of the worst I've ever come across.
If you change content view, then all the previous views are going to be destroyed. Are you using an adapter? If so, then it would be very easy to add all the items to the list again.
There shouldn't be any reason to setContentView any time other than in onCreate.
If you were looking to have multiple screens, instead of changing content view, then start a new activity.
dynamically add items:
//add at the top of the list
mListView.addHeaderView(itemView);
// add at the bottom of the list
mListView.addFooterView(itemView);