How to set listview scroll position down in android?

I know that this question is asked several times, but my situation is slightly different from other questions.

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

I tried 2 ways.

1st number

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

2nd one

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

So, my problem is that both codes work correctly with the emulator , but it does not work with the real device.

What did I miss?

+4
source share
7 answers

try it. below code works fine for me.

specify a time delay of 500 for the load list, and then call the setselection method.

commentslistview.postDelayed(new Runnable() {
    @Override
    public void run() {
        commentslistview.setSelection(commentsarraylist.size());
    }
}, 500);
+11
source

ListView?

android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
+8
listView.setAdapter(adapter);
listView.setSelection(position);
+2
source

try it

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

            @Override
            public void onGlobalLayout() {

                mScrollView.post(new Runnable() {

                    @Override
                    public void run() {


                        scrollView.smoothScrollTo(0,scrollView.getBottom());
                        }
                    });
                }
        });
+1
source

: D I just installed:

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 great with you.

+1
source

Try

mCommentListView.getChildAt(mCommentListView.getChildCount() - 1).requestFocus();
0
source

Try the code below

commentslistview.smoothScrollToPosition(commentsarraylist.size() -1);
0
source

Source: https://habr.com/ru/post/1523840/


All Articles