l3l*_*h4t 9 android listview scroll
我在我的应用程序中设置了一个计时器,我可以从Web服务获取一些信息,并生成一个列表视图来显示.现在我的问题是,每次计时器运行时,滚动回到开头......
如何在列表视图中每次刷新时保持滚动位置?
我的部分代码:
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(DashboardActivity.this,
all_chat,
R.layout.list_item,
new String[] { TAG_FULLNAME,
TAG_DATE,
TAG_MESSAGE },
new int[] { R.id.fullname,
R.id.date,
R.id.message }
);
// updating listview
setListAdapter(adapter);
}
});
Run Code Online (Sandbox Code Playgroud)
TNX.
Bir*_*dia 14
不要打电话setAdapter().做这样的事情:
ListAdapter adapter; // declare as class level variable
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
*/
if (adapter == null) {
adapter = new SimpleAdapter(
DashboardActivity.this, all_chat, R.layout.list_item, new String[]{TAG_FULLNAME, TAG_DATE, TAG_MESSAGE},
new int[]{R.id.fullname, R.id.date, R.id.message});
setListAdapter(adapter);
} else {
//update only dataset
allChat = latestetParedJson;
((SimpleAdapter) adapter).notifyDataSetChanged();
}
// updating listview
}
});
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以将以下属性添加到ListViewin xml中.
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
Run Code Online (Sandbox Code Playgroud)
添加这些属性,您ListView 将始终在底部绘制,就像您希望它在聊天中一样.
或者如果你想把它保持在以前的同一个地方,替换alwaysScroll为normal
in the android:transcriptMode attribute.
Run Code Online (Sandbox Code Playgroud)
干杯!!!