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.
Related
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.
...
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 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);
Good afternoon,
I have a list view that show a list of items...well not all of them but they are all there. Rather than let the user scroll up / down the requirement asks for and up / down buttons. I have everything handled quite well EXCEPT how to advance the ListView. Say I'm looking at item 3 in the list. The user hits next...I want to ListView to advance to item 4. I am keeping track internally of which page they are on for incrementing / decrementing. I was expecting to find something like this but no dice.
ListView.MoveNext();
or
ListView.Move(int);
Per Brayden's suggestion I tried the accepted solution here: Maintain/Save/Restore scroll position when returning to a ListView and it did not APPEAR to scroll the list....i.e. it stayed on the first list item. I also tried setSelection.
Here is my code:
public void btn_NextClick(View vw_Current)
{
//increment page count 1
i_PageTracker ++;
//advance to next record
//int x = lstvw_LiftData.getFirstVisiblePosition();
View v = lstvw_LiftData.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
lstvw_LiftData.setSelectionFromTop(i_PageTracker, top);
//lstvw_LiftData.setSelection(i_PageTracker);
}
Based upon Vladimir's success and my lack thereof I wanted to provide some additional info that I thought irrelevant but....
I've extended the SimpleAedapter and override the getView. I also have a DataBinder to populate TextViews with certain data from the data object.
Interestingly when I set a breakpoint in the getView of the DataAdapter and the setViewValue of the binder at load we hit them as appropriate. However when the next button is clicked we do not hit them which I would expect if the list was advancing to the next position....hmmmm I wonder....I have another Idea....be right back.
I figured it out....but I don't know what to do about it. See not knowing what I am doing I shot myself. I ONLY want to see one item in the list at a time and I don't want the user to be able to scroll. Well what I did to accomplish that was to override getCount in the DataAdapter and return a 1. This SEEMED to be the answer as only one item displayed cool! But then overriding getCount also means there is only one item to display so we never advance...there's nothing to advance to.
So what I want to know is this. How do I keep the user from being able to scroll and force them to use the navigation button. Do I handle the scroll event and return null?
I figured it out....but I don't know what to do about it. See not knowing what I am doing I shot myself. I ONLY want to see one item in the list at a time and I don't want the user to be able to scroll. Well what I did to accomplish that was to override getCount in the DataAdapter and return a 1. This SEEMED to be the answer as only one item displayed cool! But then overriding getCount also means there is only one item to display so we never advance...there's nothing to advance to.
So what I want to know is this. How do I keep the user from being able to scroll and force them to use the navigation button. Do I handle the scroll event and return null?