在ListView的layout配置中添加 android:transcriptMode="alwaysScroll"
<ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:transcriptMode="alwaysScroll" </ListView>
或者在Java代码中执行
mListView = (ListView) view.findViewById(R.id.listview); mListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
当数据改变的时候,在回调函数中使用ListView.setSelection()方法来定位到最后一行
ChatAdapter 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://*.com/questions/3606530/listview-scroll-to-the-end-of-the-list-after-updating-the-list
====================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
====================================================================================