【问题标题】:How to set ListView Scroll position to bottom in android?如何在android中将ListView滚动位置设置为底部?
【发布时间】:2014-02-17 20:32:31
【问题描述】:

我知道这个问题之前被问过好几次,但我的情况与其他问题有点不同。

我有一个列表视图,最初我想将滚动位置设置为列表的底部。

我尝试了两种方法。

第一个

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

第二个

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

所以我的问题是上面的代码都可以在模拟器上正常工作但它不能在真实设备上工作。

我错过了什么?

【问题讨论】:

  • 你在哪里调用这些方法调用?贴出更完整的代码。
  • 来自 Asynactask OnPostExecute()。
  • 试试这个:mCommentListView.setSelection(mCommentListView.getCount() - 1);
  • AsyncTask 什么时候开始?发布 Activity 和列表​​适配器的代码。
  • @JoelFernandes 我试过但没有运气:(

标签: android android-listview


【解决方案1】:

您是否在 ListView 中设置了这些?

android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"

【讨论】:

  • 是的,我也试过这个,但如果你设置 android:transcriptMode="alwaysScroll" 它会一直滚动到底部,我不需要我第一次想要那个。
【解决方案2】:

试试,

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

【讨论】:

    【解决方案3】:

    试试这个

    srollView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
    
                @Override
                public void onGlobalLayout() {
    
                    mScrollView.post(new Runnable() {
    
                        @Override
                        public void run() {
    
    
                            scrollView.smoothScrollTo(0,scrollView.getBottom());
                            }
                        });
                    }
            });
    

    【讨论】:

    • 嘿,什么是 srollView?听说我正在处理一个列表视图
    • scrollView 在这里是 ScrollView 的对象。但是科学你正在处理一个列表视图,你可以尝试在滚动视图的一侧有一个列表视图。
    • @Biplab 在滚动视图中放置一个列表视图无论如何在 android 中都不是一个好习惯,非常感谢您的帮助。
    【解决方案4】:

    试试这个。下面的代码对我来说工作正常。

    给加载listview 500时间延迟,然后调用setselection方法。

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

    【讨论】:

    • 好吧 Ganash 我明白了。非常感谢其他人也给了我很大的帮助。
    【解决方案5】:

    :D 我刚刚设置:

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

    还有我的布局

    <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>
    

    设置

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

    希望它适合你。

    【讨论】:

      【解决方案6】:

      试试下面的代码

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

      【讨论】:

      • 这并没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
      • 我得到了 OP 的要求,但解决方案给出了动画滚动到最后一个位置。
      • 这并没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
      • @DennisKriechel 为什么不呢?
      【解决方案7】:
      listView.setAdapter(adapter);
      listView.setSelection(position);
      

      【讨论】:

        猜你喜欢
        • 2019-06-28
        • 2021-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-04
        • 2020-01-20
        • 2015-09-22
        相关资源
        最近更新 更多