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.
Related
I have a EditText in my android NestedScrollView and I want it to go to top when It's focused, something like this:
I tried:
SmoothScrolling/Scrolling to a direct pixel,
SmoothScrolling/Scrolling to the top of the view(or bottom)
none of them work, and not even respond consistently.
Edit:
I got it, will leave the question for documentation porposes.
I got the ScrollView size with
scrollView.getBottom();
And then scrolled to the intended place using:
scrollView.scrollTo(0,2*scrollView.getBottom()/5); //size will vary from view to view
Maybe your scrollView hasn't get enough time to calculate it's size? Did you try:
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.scrollTo(0, 0);
}
});
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 am trying to create an app for which i need to scroll down programmatically upon receiving some message..
I have tried using ScrollView using following code
ScrollView mScrollView;
mScrollView = (ScrollView)findViewById(R.id.abc);
mScrollView.post(new Runnable() {
public void run() {
mScrollView.scrollTo(0, mScrollView.getBottom());
}
});
It works and it takes me to bottom , but there's no animation like actual scrolling.
How can i get "scrolling animation" along with that?
is there any other way to scroll programmatically with animation? please help me out.
try using ScrollView.smoothScrollTo method
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.
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());