首页 > 系统 > Android > 正文

Android的ListView数据更新后,如何使最新的条目可以自动滚动到可视范围内?

2019-11-09 15:49:51
字体:
来源:转载
供稿:网友

在ListView的layout配置中添加 Android:transcriptMode="alwaysScroll"

[html] view plaincopy<ListView      android:id="@+id/listView"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:layout_alignParentLeft="true"      android:transcriptMode="alwaysScroll"  </ListView>  

或者在java代码中执行

[java] view plaincopymListView = (ListView) view.findViewById(R.id.listview);          mListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);  

 

当数据改变的时候,在回调函数中使用ListView.setSelection()方法来定位到最后一行

[java] view plaincopyChatAdapter adapter = new ChatAdapter(this);    ListView lv = (ListView) findViewById(R.id.chatList);  lv.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);  lv.setAdapter(adapter);    adapter.registerDataSetObserver(new DataSetObserver() {      @Override      public void onChanged() {          super.onChanged();          lv.setSelection(adapter.getCount() - 1);          }  });  

 PS:http://stackoverflow.com/questions/3606530/listview-scroll-to-the-end-of-the-list-after-updating-the-list

 

 

        ====================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址:http://blog.csdn.net/ouyang_peng

====================================================================================


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表