Android: scroll to the bottom of the view - android

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

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.

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

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

listview doesn't scroll up until i touch it

i have a listView and a button ...
when i scroll down on the list .. the button which i named 'scroll_up' appears, so when i click it the list should scroll to the top.. but it didn't until i touch it.. i mean after clicking the button nothing happens ..but if i touch the list it scoll to the top ..
here is my button onClick
public void scrollup(View v){
lv.smoothScrollToPosition(0);
}
there is no error in the logcat...
Note:I use a header in the list .. maybe this what cause the problem ..
so is there a way to refresh the layout or somthing similar ?
If you want to move to first index of the list view, use below code:
listView.setSelection();
If you want to smooth scroll you may add line in listview in xml
android:smoothScrollbar="true"
or in code:
lv.setSmoothScrollbarEnabled(true);
And after your code
lv.smoothScrollToPosition(0);

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