ScrollView嵌套ListView解决办法

来源:互联网 发布:sar算法平台 编辑:程序博客网 时间:2024/06/13 02:00
public class ListViewRelayout {public void setListViewHeightBasedOnChildren(ListView listView) {// get the list view adapter, so this function must be invoked after set the adapter.ListAdapter listAdapter = listView.getAdapter();if (listAdapter == null) {return;}int totalHeight = 0;// get the ListView countint count = listAdapter.getCount();for (int i = 0; i < count; i++) {View listItem = listAdapter.getView(i, null, listView);// measure the child viewlistItem.measure(0, 0);// calculate the total height of itemstotalHeight += listItem.getMeasuredHeight();}ViewGroup.LayoutParams params = listView.getLayoutParams();// get divider height for all items and add the total heightparams.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));listView.setLayoutParams(params);    }}
2、在ListActivity.java   mAppListView.setAdapter(mAppListAdapter);    mRelayout.setListViewHeightBasedOnChildren(mAppListView);


如果你还增加了对ListView的动态改变,那么,还需要在notifyDataSetChanged后再调用一次,如:

    mEnableAppListAdapter.notifyDataSetChanged();    mUtil.setListViewHeightBasedOnChildren(mEnableAppListView);

                                             
0 0