When a button is clicked, I would like the app to smoothly scroll to the bottom of a listview. Currently this is my implementation (kotlin)
listview.post { listview.smoothScrollToPosition(adapter.count - 1) }
However, this does not always work properly: Some list items contain some expandable parts (i.e. view whose visibility can be VISIBLE or GONE), which initially are hidden.
When these parts are expanded however, the function sometimes does not scroll to the end, but to a position before the end.
How can I reliably scroll to the end of the listview?
Try following. it will solve your problem, i tried it and it works great.
listView.post(new Runnable(){
public void run() {
listView.setSelection(listView.getCount() - 1);
}});
add android:transcriptMode="alwaysScroll"
to the ListView block in xml
Related
I have an EditText with number input, a button and a ScrollView one below the other.
Inside the ScrollView I have a Linear Layout with vertical orientation and inside the Layout I have about 50 TextViews each with id 1,2,3,4....
What I want is that when the user enters a number in EditText and clicks the button,it should read which number is entered (say 28),then the ScrollView should scroll Textview with id 28 without disturbing the others.
I have no problem if I have to type the same code for each and every TextView even 500 times but it should be a working code like I mentioned above.
I have been trying to achieve this for a while without any success.Please Help!!!
Find the view at that position in LinearLayout and scroll to that view
scrollView.post(new Runnable() {
public void run() {
scrollView.scrollTo(0, parentLinearLayoute.getChidAt(position).getBottom());
}
});
You will have to replace Scrollview and Linearlayout with RecylerView. And you can use smoothScrollToPosition to scroll to a particular position. I think this should be the best approach
Hope you can help me, this has been bugging me for quite some time now-
I have an activity that at some point inflates a view which has its own layout and logic behind. This view contains a ScrollView which I want to be scrolled down x dp when its shown to the user (I don't want user to actually see the scrolling, I want ScrollView to be at the right position as the view displays).
Here's what I tried so far:
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.scrollTo(0, someInnerView.getBottom());
}
});
I've tried using this code in different parts of my view logic such as onFinishInflate() but that doesn't work as the data isn't binded to the view at that point. So I added OnGlobalLayoutListener and called the Runnable inside onGlobalLayout() which does work but with a short delay which can clearly be detected.
How do I solve this problem? All I want to do is to have some part of my ScrollView hidden when it's presented to the user.
All help appreciated!
EDIT : to make my intentions clearer- I want to create something similar to this, only that I'm using ScrollView
I've having a chat app, if the ListView items are not enough to scroll then the row should be at the top of the list. If the items are enough to trigger scroll then the list should scroll always scroll down to show the latest conversation like in the iMessage setup.
I tried adding in the XML:
android:stackFromBottom="true"
but this would cause the ListView to scroll down ALWAYS. I only want to scroll down if the items are enough to cause scrolling, if not then it should scroll up.
I would use this if I could detect the enabling of scroll:
myListView.post(new Runnable() {
#Override
public void run() {
// Select the last row so it will scroll into view...
myListView.setSelection(myListAdapter.getCount() - 1);
}
});
So how would I detect if the ListView items are enough to trigger scroll?
You could try this to detect the scrollable
if (yourListView.getLastVisiblePosition() == yourListView.getAdapter().getCount() -1)
{
//It is scrolled all the way down here
}
to check the last visible item is still smaller the item number :)
I fixed this by placing this code listView.setSelection(rowAdapter.getCount()); after listView.setAdapter(rowAdapter);. When chat messages are only few, it is scrolled up and when the messages are enough to trigger scroll then it is scrolled to the bottom. Just like I want it.
I'm working with a rather large layout that includes a ListView and a LinearLayout beneath it as a footer. I first tried to wrap this in a ScrollView to make the whole layout scrollable, but ran into problems due to both the ListView and the ScrollView being incompatible with each other since they both have scrollable features. So, a workaround was to include the LinearLayout as a footer to the ListView.
Now, in the LinearLayout, I have buttons at various places that the user can click to return to the top of the page. The behaviour I am getting from this is odd, to say the least.
If I have not scrolled down too far, the buttons function normally. However, if I scroll down a bit too far, then clicking the button (even the same buttons that previously worked) will result in the layout scrolling up to roughly half of the way up the listview instead of the top.
Here is the method that does the scrolling, it's rather simple:
public void backToTop(View view) {
lv = (ListView)findViewById(R.id.listview);
lv.smoothScrollToPosition(0);
}
This method is triggered when any of the buttons are clicked.
I have also tried to use the "scrollTo(0, 0)" function, but that failed to do anything.
Any help would be appreciated!
**edit: After testing some more, it appears as though the point where scrolling does not seem to function properly anymore is when the listview is no longer visible on the page. As soon as I scroll past it, the buttons begin to function incorrectly.
Edit 2: SOLVED. My solution: I changed the line
lv.smoothScrollToPosition(0);
to:
lv.setSelected(0);
This seems to give the correct behaviour for all my buttons at any position that the user has placed their screen. Using setSelected does not seem to have the side-effect that I was expecting of automatically triggering the click-event. Hooray!
My solution: I changed the second line of my backToTop method:
lv.smoothScrollToPosition(0);
to:
lv.setSelected(0);
This seems to give the correct behaviour for all my buttons at any position that the user has placed their screen. Using setSelected does not seem to have the side-effect that I was expecting of automatically triggering the click-event. Hooray!
I want to scroll to the end of the view. Currently i am trying to scroll the scrollview to the end of the latest added view. I tried with setscrolly, smoothscrollto. both works but the problem is both wont scroll to the end, it just scrolls to the last-1 view. Please suggest me how to overcome this.
I got the reason why it was not scrolling to the last view and was stopping at the last-1 view. I was not smoothScrollTo(x,y) in a post thread of the view. Since my scrollview is not refreshed when the last view is added it was not scrolling to the last view.
view.post(new Runnable() {
public void run() {
scrollview.smoothScrollTo(x,y);
});
Try to use transcript mode param of list view. It provides scrolling to new items automatically:
getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);