How to set ListView Scroll position to bottom in android?
Asked Answered
B

7

5

I know this question is asked several times before but my situation is bit different from other questions.

I have a listview and initially i want to set the scroll position to the bottom of the list.

I have try 2 ways.

1st one

mCommentListView.setSelection(mAdaptor.getCount()-1);

2nd one

mCommentListView.post(new Runnable() {
    @Override
    public void run() {
        mCommentListView.setSelection(mAdaptor.getCount()-1);
    }
});

So my problem is both of above code working properly with the emulator but it is not working with the real device.

What have i missed?

Blunder answered 27/1, 2014 at 5:15 Comment(10)
Where are you invoking those method calls? Post more complete code.Gustie
from Asynactask OnPostExecute().Blunder
Try this: mCommentListView.setSelection(mCommentListView.getCount() - 1);Abbatial
And when is the AsyncTask started? Post your code for the Activity and the list adapter.Gustie
@JoelFernandes i tried but no luck :(Blunder
@kaluwila Did you try that with the first method? Also, try giving a number and see if it is working that way. Ex; mCommentListView.setSelection(5);Abbatial
@ JoelFernandes yeah you got a point hmm it is not going to work with any number it will always set to first item.but anly in the device working fine with the emulaterBlunder
see #3607030Bruiser
@kaluwila Hmm that's strange. Can you post all the code that's related to the ListView?Abbatial
@Shayan pourvata that is what i have already tried.Blunder
C
12

try this. the below code is working fine for me.

give time delay 500 for load listview then call setselection method.

commentslistview.postDelayed(new Runnable() {
    @Override
    public void run() {
        commentslistview.setSelection(commentsarraylist.size());
    }
}, 500);
Children answered 27/1, 2014 at 5:43 Comment(1)
Ok Ganash i got it. thanks a lot to other people also giving me a great help.Blunder
D
8

Did you set these in your ListView?

android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
Dresden answered 27/1, 2014 at 5:21 Comment(1)
yes i tried this on also but if you set android:transcriptMode="alwaysScroll" it will scroll to bottom always i dont need that i want that for the first time only.Blunder
P
2
listView.setAdapter(adapter);
listView.setSelection(position);
Proceed answered 22/10, 2014 at 5:59 Comment(0)
T
1

Try this

srollView.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {

                mScrollView.post(new Runnable() {

                    @Override
                    public void run() {


                        scrollView.smoothScrollTo(0,scrollView.getBottom());
                        }
                    });
                }
        });
Tailband answered 27/1, 2014 at 5:29 Comment(3)
hyy what is the srollView? hear i am dealing with a listviewBlunder
scrollView is object of a ScrollView here .But science u are dealing with a list view you can try to to have a the listview in side of a scrollView.Tailband
@ Biplab putting a listview inside scrollview is not a good practice in android anyway thanks a lot for your help.Blunder
D
1

:D I just set:

mListView.setSelection(adapter.getCount() - 1);

And my Layout

<ListView
    android:id="@+id/tb_body"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:choiceMode="singleChoice"
    android:stackFromBottom="true"
    tools:ignore="NestedScrolling" >
</ListView>

SET

android:choiceMode="singleChoice"
android:stackFromBottom="true"

Hope it works fine with you.

Dresden answered 27/1, 2014 at 5:45 Comment(0)
P
0

Try,

mCommentListView.getChildAt(mCommentListView.getChildCount() - 1).requestFocus();
Peptonize answered 27/1, 2014 at 5:23 Comment(0)
Z
0

Try the below code

commentslistview.smoothScrollToPosition(commentsarraylist.size() -1);
Zombie answered 27/1, 2014 at 6:29 Comment(4)
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.Tardiff
I got what OP was asking, but solution gives an animation scroll To last position.Zombie
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From ReviewUnderproduction
@DennisKriechel Why not?Lading

© 2022 - 2024 — McMap. All rights reserved.