ListView scroll to selected item - android

I have a ListView with an edit text and a button below it. When I click on a listView item the keyboard appears and push up the edit text and the button. I want the list to scroll to the selected item. Any idea? Thanks

You can use ListView's setSelection(int position) method to scroll to a row.

You could use ListView's smoothScrollToPosition(int position) to scroll to a particular location in the list.

For a direct scroll:
getListView().setSelection(11);
For a smooth scroll:
getListView().smoothScrollToPosition(11);
To Scroll to top
getListView().setSelectionAfterHeaderView();
Note
try to call it in post because sometime listview is not yet created while you calling it's method
getListView().postDelayed(new Runnable() {
#Override
public void run() {
lst.setSelection(15);
}
},100L);

You should use transcript mode:
getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);

Setup a listener on your the list item being clicked, then use View.getTop() or View.getBottom() when clicked to get it's position within the parent. You can then use ListView.scrollTo(x, y) to scroll to the list item.

Yo can look For
listView.setSelectionFromTop(position, distanceFromHeader);
It will position the Item at position , specified pixels below the top of listview

You can use
smoothScrollToPosition(position)
Just increase the position of item with 1, and you will get the view of item.
getListView().smoothScrollToPosition(position + 1);

Using duration gives a better user experience. Use this, with duration added. Will scroll the item in position smoothly to the top of the listview.
int duration = 500; //miliseconds
int offset = 0; //fromListTop
listview.smoothScrollToPositionFromTop(position,offset,duration);
descrease duration to make scrolling faster

Related

How to change selected item position to top in Recyclerview?

I have recyclerview in that I want to change item's position dynamically to top after selecting the item of recyclerview.
Please suggest best way to achieve the above problem?
You need to swap selected item with top item in list, then notify your adapter about position change.
A sample code would look like:
Collections.swap(orderItems.getItems(), position, 0);
notifyItemMoved(position, 0);
Hope it helps!
Try this
String Item = List.get(position);
List.remove(position); // remove item from the list
List.add(0,removedItem); // add at 0 index of your list
notifyDataSetChanged();
linearLayoutManager.scrollToPositionWithOffset(0, 20); // scroll recyclerview to top
OR
recyclerview.getLayoutManager().scrollToPosition(0) // scroll recyclerview to top
OR
recyclerview.smoothScrollToPosition(0); // scroll recyclerview to top
add below code in onclick of item,
String removedItem = actualList.remove(position);
//actualList is list used to populate recylerview and position is selected
//item position
actualList.add(0,removedItem);
notifyDataSetChanged();
If you have different cases like this, we can achieve this approach from #manohar-reddy and call notifyItemChanged(position) it will also call the onBindViewHolder, then update your layout accordingly.
*In this case, if the 0 item has a different layout from the other items
You might wanna read this reference for getting the item position correctly
thanks to #amit-srivastava answer and #manohar-reddy

RecycleView: how to go to a row without scrolling

If the recyclerview has a list of items and I want to go to a specific row immediately when user enters the UI, so I don't want the user to see it scroll to reach that row. Is it achievable?
Thanks.
You can use method scrollToPositionWithOffset (int position, int offset)
Scroll to the specified adapter position with the given offset from
resolved layout start.
See documentation
Example:
//Scroll to item position 2 with offset 0
RECYCLERVIEW_LAYOUT_MANAGER.scrollToPositionWithOffset(2, 0);
Hope this will help~
I would say it is impossible.
RecyclerView#scrollToPosition causes sort of animation because it does not know the height of all rows initially. Not all items are loaded immediatly but one by one as you scroll down the list.
I use ListView and I solved the issue by calling setSelection(position)

Android search in recyclerview and scroll to that

You suppose in simple recyclerview i have some text, i'm trying to find how can i search text in that and scroll to found text position, for example in simple recyclerview i have 10 row and on 4th position i have this text:
Hello World
i want to scroll to that on 4th position, how can i do that?
Check your RecyclerView items for their texts, once you have a match to your desired text, get the item position and call:
myRecyclerview.scrollToPosition(position);
Iterate through your Adapter's data list and store the index for the item you're searching for. Then, use RecyclerView's scrollToPosition(y) method with that index to scroll to that particular item's position.
Workaround that I found on stackoverflow which really worked for me
myRecyclerView.scrollToPosition(position);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
myRecyclerView.smoothScrollToPosition(position);
}
}, 50); //sometime not working, need some delay
https://stackoverflow.com/a/57207024/6940711

Scrolling listview with buttons

I want to scroll my listview by pressing the buttons. Am using two buttons up and down and when i press up button the listview should move one row up and when i press down button the listview should move one row down.
My listview is based on this link. I found a good example of scrollview here. Now my question is instead of scrollview i need to use my listview and it should increase/decrease by one row. As am new to android anyone help me in solving this issue. Thanks in advance.
You can try either of the following:
For a direct scroll:
getListView().setSelection(int);
For a smooth scroll:
getListView().smoothScrollToPosition(int);
Sample Code:
public View.OnClickListener onChk = new View.OnClickListener() {
public void onClick(View v) {
int index = list.getFirstVisiblePosition();
getListView().smoothScrollToPosition(index+1); // For increment.
}
});
However you need to handle one case, that is if only half or part of the view at the top is visible.
Have you even checked for IDE suggestions? ;)
ListView lv = (ListView) findViewById(R.id.myListView);
// to scroll to a given position
lv.scrollTo(int x, int y);
// to scroll by a given number of dp
lv.scrollBy(int x, int y);
Scroll with smooth Duration
getListView().smoothScrollToPositionFromTop(position,offset,duration);
Parameters
position -> Position to scroll to
offset ---->Desired distance in pixels of position from the top of the view when scrolling is finished
duration-> Number of milliseconds to use for the scroll
Note: From API 11.

Android: scroll to the bottom of the view

I have a ListView containing some custom Views, and at the end of my list, a FooterView containing a Button.
When I click the button, it adds a view at the end of my ListView. But the cursor of the scrollbar stays at its position.
Now, what I want is to auto-scroll to the end of the list (so the user can see the new item).
Do you have any idea on how I can do that?
i think you will find you happyness here
ListView has a method just for this: smoothScrollToPosition
Once you've added the new item, just calculate the length of the list and pass that (remembering zero-indexing) to the above method
Try this one.. it will solve your problem, i tried it and it works great.
listView.post(new Runnable(){
public void run() {
listView.setSelection(listView.getCount() - 1);
}});

Categories

Resources