I want to get the all values from the RecyclerView using Espresso. I am able to get only the visible values from the recyclerView. But not able to get the values which are visible when scrolling.
Ex. In RecyclerView 25 items are there and Only 7 items are visible. Others are visible when we scroll it. I am able to get the values of 7 items. But want to get all the values
Can Anyone please tell me how to get this
Thanks in Advance
You need to remember how RecyclerView really works. It optimizes showing scrollable elements, by displaying only items that are currently within the visible range.
In other words: before you scroll to items below, they are no Views within the RecyclerView to represent those items. Just the data in the Adapter.
So the way you should be probably doing your test is to:
Verify proper state of currently visible items.
Scroll the RecyclerView (using one of the RecyclerViewActions.scrollTo methods)
Verify proper state of currently visible items.
Scroll the RecyclerView.
...
Related
So, I have a recyclerview which consists of images and I want to show how many views the image got via server json.
So as the user scrolls and sees an image for 2+ seconds I want to call the api and increment the view of that particular item which is visible currently. Any help on how to achieve this would be appreciated!
If you are using LinearLayoutManager in RecyclerView you can get an item position by using
int findFirstCompletelyVisibleItemPosition()
int findLastCompletelyVisibleItemPosition()
To detect scroll events in the RecyclerView, you need to add a scroll listener, RecyclerView.OnScrollListener and with every item scroll maintain timestamp.
You can read this article explaining the same functionality Detecting List Items Observed by User
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 have a little Problem with my ListView.
I fill my ListView with an ArrayList<String> per ArrayAdapter<String> with more than 100 items.
Before my dialog will be show i want to highlight and disable some items. I have found the solution with ListView.post(new Runnable() {...});
I highlight my items with lv.getChildAt(2).setBackgroundColor(Color.BLUE);
and disbable an item with lv.getChildAt(3).setEnabled(false);
I do both also in an OnItemClickListener().
Now my problem:
if in the ListView are 11 items visible at runtime, the the highligt- and disable-pattern will repeat every 11 items.
i.e. if i highlight just the 3. item also the 14., 25. ... item will get a blue background.
And if i disable the 4. item also the 15. and 26. and so on is disabled.
If i scroll fast to buttom and back to top other items are highlighted and disabled.
Another problem is, that i can only access the first (11) visible items in the post-runnable. If i try to highlight the 20. item the app will crash with a NullPointerException.
What can i do to prevent the "item-recycling" and to get full access to all items before the Dialog is shown?
I'm not sure i understand what you want to achieve but here are some suggestions for you.
1) Always Recycle, you should never avoid recicling since you maybe run into another problem, you will run out of memory.
2) In your model implement the checkeable interface, so the model should know if an item is selected or not, Not the view only
3) When iterating each element make use of a ViewHolder and then check the model to see if the elment being inflaed it selected or not and use the desired color
Please have a look at this example link, it describes the use of CAB (Contextual Action Bar) but it uses the things i'm mentioning.
I hope it helps you.
I have made a horizontal listview with ArrayAdapter as in the picture below.
There will be always 3 items on screen. But there is a problem. I need to get which three items are on screen. With getView function, I can only get last activated item.
Is there a way that I can get which items are on screen ?
try this
ListView.getFirstVisiblePosition()
ListView.getLastVisiblePosition()
you may refer answer here as well
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);