Android search in recyclerview and scroll to that - android

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

Related

custom listview save selected item position

I create a custom listview.
I want to select any item using another button click or something views click.
Already I tried via these code for custom selection.
And I also want to get previous selected item on resume.
listView.setItemChecked(2,true);
listView.setSelection(2);
listView.requestFocus();
But everytime , I failed.
I used this method to create custom listview.
Android ListView with Custom Adapter Example Tutorial
Advance Thanks .
For setItemChecked to work, your list items need to be Checkable; you can, for example use CheckedTextView (at the top level of the item view, no further layout around it).
For setSelection to work, you need to use it inside a Runnable, like so:
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(2);
}
});
In both cases, don't ask why.
For a selection to become visible, you can put a statelist in the background attribute of your list item.

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)

How to avoid jerk when notifyDataSetChanged method of adapter is called?

I am trying to add pagination in my listView.
I added a method in my custom BaseAdapter which adds next page results at top of list on scroll to first item in list.
public void addEntriesToTop(List<ChatModel> entries) {
// Add entries to the top of the list
this.chatMessages.addAll(0, entries);
notifyDataSetChanged();
}
So if I have to add next page on top of ListView, I am using like this in my activity.
//result contain new items to be added to top of list
adapter.addEntriesToTop(result);
//so that list scroll sets to last visible message to user
final int index = result.size();
listView.post(new Runnable() {
#Override
public void run() {
//go to user's last scroll position
messagesContainer.setSelectionFromTop(index, 0);
}
});
This method is working fine and retains user's last scroll position. But it has a problem, when I call addEntriesToTop() it scrolls the ListView to bottom(because of notifyDataSetChanged() ) and then when I call setSelectionFromTop it scrolls to user's last position. In this transition, there is small jerk.
Please help me to smooth this transition.
Thanks
As ListView is old and now its obselete, now you can use Recyclerview
instead of ListView, here is the link that helps you to convert your
ListView to RecyclerView. RecyclerView have many methods to add your custom animations and also it have not any jerks while adding/removing items.
Replacing ListView with RecyclerView
For Default Animations you can use this link:
RecyclerView Animations – Add & Remove Items

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);
}});

ListView scroll to selected item

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

Categories

Resources