我在scrollview中使用listview,当listview到达底部时添加新项目。一切都工作得很好但是在添加/从服务器获取新项目时(在显示列表中的那些之前)应用程序正在卡车一段时间。 我正在使用AsyncHttpClient库从服务器获取数据。
我正在设置适配器,
if(adapter == null) {
adapter = new NetworkAdapter(activity, fragmentManager, R.layout.lv_network_custom_row, networkDataArrayList);
lvNetwork.setAdapter(adapter);
Log.d("network_response","adapter set");
}
else {
adapter.notifyDataSetChanged();
Log.d("network_response","adapter notify");
}
据我所知,我一次创建适配器对象,后来只是通知更改。 此外,我保留其先前的职位,
int index = lvNetwork.getFirstVisiblePosition();
View v = lvNetwork.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
lvNetwork.setSelectionFromTop(index, top);
我还需要动态设置listview高度,我这样做,
ListAdapter myListAdapter = listView.getAdapter();
if (myListAdapter == null) {
return;
}
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, listView);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
@SuppressWarnings("deprecation")
int screenWidth = display.getWidth();
int listViewWidth = screenWidth - 65;
int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth,
View.MeasureSpec.AT_MOST);
listItem.measure(widthSpec, 0);
totalHeight += listItem.getMeasuredHeight();
listTotalHeight = totalHeight;
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (myListAdapter.getCount()));
listView.setLayoutParams(params);
listView.requestLayout();
我调试了代码,我猜设置listview高度时卡住了。
我想要的是,app / UI在显示新添加的项目之前不会卡住。我怎么解决这个问题? 任何建议将不胜感激。