I though this would be a simple task, but apparently there is no way to scroll listview to the bottom. All solutions that I found are using variations of setSelection(lastItem) method which only sets selection to last item, but does not scrolls to the bottom of it.
In my case I have a empty listview (with a long empty view header set) and I want to scroll to bottom of it.
So, is there a way to do it?
Edit:
So for those who are interested the working solution is:
getListView().setSelectionFromTop(0, -mHeader.getHeight());
and
getListView().scrollTo(mOffset)
This also works, with right offset (calculated based on current scroll position), but might give you some unexpected results.
If you want the list view to be scrolled always at the bottom even when you are updating the list view dynamically then you can add these attributes in list view itself.
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
use the following
lv.setSelection(adapter.getCount() - 1);
If you would like to have a scroll animation programmatically you could easily scroll to the bottom of the list using:
listView.smoothScrollToPosition(adapter.getCount());
scrolling to the top most of the list can be achieved using:
listView.smoothScrollToPosition(0);
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);
}});
private void scrollMyListViewToBottom() {
myListView.post(new Runnable() {
#Override
public void run() {
// Select the last row so it will scroll into view...
myListView.setSelection(myListAdapter.getCount() - 1);
}
});
}
Its working for me
Use following and try.
listview.setSelection(adapter.getCount()-1);
You might want to try myListView.smoothScrollToPosition(myListView.getCount() - 1). This way, you're not forced into selecting the last row. Plus, you get some smooth, beautiful scrolling! (:
I Had the same problem, This works best for me
listView.setSelection(listView.getAdapter().getCount()-1);
if there is no adapter set to your ListView then it has no children at all, empty view is not a child of your ListView
Try using list.scrollTo(0, list.getHeight());
Related
I have a list of images in a recycler view with a Horizontal Linear Layout. I want to programmatically scroll to say position = 20, while the image at that position is not in view.
I have tried using:
recyclerView.scrollToPosition(position);
but this only scrolls if the item is in view. I have also tried using smoothScrollBy(x,y) and getLayoutManager().scrollToPosition(position) but it doesn't work.
Use below code:
yourRecyclerViewObject.getLayoutManager().scrollToPosition(itemPosition);
I had this same issue and using delay worked for me
recyclerView.postDelayed(new Runnable(){
#Override
public void run(){
recyclerView.scrollToPosition(position);
}
},300);
I have application with ListView. This ListView has a Progressbar.When I am clicking on this Progressbar, then it is showing a hided Layout(This layout contain textview with lyrics). So as I am clicking on the last Progressbar then it is showing the hided Layout, but I need to scroll manually. I want that scrolling must be automatically.
Please help me.
Try something like this:
private void scroll2bottom() {
yourListView.post(new Runnable() {
#Override
public void run() {
yourListView.setSelection(yourListAdapter.getCount() - 1);
}
});
}
You might find the smoothScrollByOffset() or smoothScrollBy() methods of ListView most useful. In particular smoothScrollBy() takes both a distance to scroll and a duration over which the scroll animation should take place. With this method you can control precisely how far to scroll the list.
in your layout of your listview just use the transcriptMode
a listview has builtin auto scroll
android:transcriptMode="normal" witll scroll when its at the bottom but not when you have scrolled away
and the setting alwaysScroll will always scroll to the bottom
and then you can set the android:stackFromBottom="true" to make it grow from the bottom
Try in this way:
lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
lv.setStackFromBottom(true);
To scroll a listView programmatically you can use its smoothScrollByOffset(int offset) or smoothScrollToPosition(int position) methods, you can find here the documentation about the ListView widget and how to use those methods.
this is not a repeat of another question.. it's more an expansion. I hope :)
I've a list view with a few hundred items in it.. They are date stamped entries in another view.
When I show the screen I want it to scroll to the bottom, so the most recent items are displayed, but in a logical "end of the thing" sort of way.
m_list.setSelection(m_list.getCount()-1);
Kewl as... but :( Now my list won't scroll. It seem stuck on the bottom. I can see the "truck" and it's tiny and at the bottom.
Ok.. so my questy is thus.. How do I start off by scrolling to the bottom, but still allow the user (me) to scroll up when they (me again) want to scroll up.
set the following on your listview in the XML.
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
You can use following code to reach at bottom of list view.
listView.postDelayed(new Runnable() {
#Override
public void run() {
listView.setSelection(listView.getCount());
}
}, 500);
This will set delay of 500 ms. Or you can use
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(listView.getCount());
}
});
This will scroll your list view pragmatically.
i`m looking for way to position expandableListView group on top, when expanded.
I tried onGroupExpand, or in performItemClick of ExpandableListView.
setSelectedPositionFromTop(int, int), partialy works, but if it starts in a time when system starts it's own scroll, than list is overscrolled, and group is out of screen.
Sorry for messy description, but it's hard to say without showing exactly what i need.
Change transcriptmode to disabled.
android:transcriptmode="disabled"
in expandable list layout.
Put this in your adapter:
public void onGroupExpanded(final int groupPosition) {
super.onGroupExpanded(groupPosition);
listView.setSelectedGroup(groupPosition);
}
Try setSelection(groupPosition) in onGroupExpand(int groupPosition).
I tried many solutions but the below code works for me.Hope this code will be helpful for some one.Implements the OnGroupExpandListener with in onGroupExpand
use the below code
public void onGroupExpand(final int groupPosition) {
super.onGroupExpand(groupPosition);
expandableListView.post(new Runnable() {
#Override
public void run() {
expandableListView.setSelection(groupPosition);
if(expandableListView.getChildAt(groupPosition)!=null)
expandableListView.requestChildRectangleOnScreen(expandableListView.getChildAt(groupPosition),
new Rect(0, 0, expandableListView.getChildAt(groupPosition).getRight(), expandableListView.getChildAt(groupPosition).getHeight()), false);
}
});
}
I have solution to this problem. It's not gentle one, but it works quite fine.
Expandable listView messures is it should move by child number. It seems, that system thinks that child is same height as parent(group), so if you put different size child it will cause problems with auto scrolling list.
My solution was to trick list to think that it has more children.
If my child is twice as big as group, i just return information that group has 2 children. Now, since it is data constructed list, i make check on my data, is there a data for 2 childs, or only 1, if i don't have data for second child, i know that i can put 0 height view as second child.
It worked fine when i had to make List with small group, and than with 1 big child.
Since there was no answer to my question, i post this one, it may help someone.
I have a ScrollView which I'm adding a number of custom Views to. After adding the custom Views, I would like to be able to Scroll to a particular one. But, immediately after adding the custom views, the ScrollView hasn't been resized, so I can not scroll. Any idea how I can force this resize?
for(int i = 0, i < numberOfViews, i++)
{
scrollView.addView(new MyView(...));
}
//tried this, doesn't seem to be helpful
scrollView.requestLayout();
//at this point scrollView dimensions are 0,0,0,0
scrollView.scrollTo(5, 200);
I'm not completely sure what you're trying to do, but I wanted to scroll all the way down, and couldn't just do that: I had to do it like this:
((ScrollView) findViewById(R.id.yourId)).post(new Runnable() {
public void run() {
((ScrollView) findViewById(R.id.yourId)).fullScroll(View.FOCUS_DOWN);
}
});
It will update later, so your view is made :)
An elegant solution is to plan the scroll and do it on the next onLayout(). Example code here:
https://stackoverflow.com/a/10209457/1310343