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?

grebulon
  • 7,697
  • 5
  • 42
  • 66
Dinesh Anuruddha
  • 7,137
  • 6
  • 32
  • 45

7 Answers7

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);
grebulon
  • 7,697
  • 5
  • 42
  • 66
RVG
  • 3,538
  • 4
  • 37
  • 64
8

Did you set these in your ListView?

android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
Luc
  • 2,800
  • 2
  • 25
  • 46
  • 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. – Dinesh Anuruddha Jan 27 '14 at 05:24
2
listView.setAdapter(adapter);
listView.setSelection(position);
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
Mulayam
  • 31
  • 3
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());
                        }
                    });
                }
        });
Biplab
  • 564
  • 1
  • 5
  • 19
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.

Luc
  • 2,800
  • 2
  • 25
  • 46
0

Try,

mCommentListView.getChildAt(mCommentListView.getChildCount() - 1).requestFocus();
Luis
  • 3,451
  • 1
  • 27
  • 41
0

Try the below code

commentslistview.smoothScrollToPosition(commentsarraylist.size() -1);
Muhammad Younas
  • 1,543
  • 1
  • 22
  • 32
mrsus
  • 5,446
  • 4
  • 31
  • 44