LoadMore icon indicating copy to clipboard operation
LoadMore copied to clipboard

LoadMoreListView和ScrollView一起用的时候会冲突,高度只有一个item的高度

Open yarnbyte opened this issue 7 years ago • 0 comments

当然问题也好解决,自定义一个ListView继承LoadMoreListView,重写onMeasure方法,再动态设置listView的高度即可。

public class MyListView extends LoadMoreListView {}

然后重写onMeasure:

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

动态设置listView的高度:

public void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) { 
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); 
            totalHeight += listItem.getMeasuredHeight(); 
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params)
    }

最后

listView.setAdapter(adapter);
setListViewHeightBasedOnChildren(listView);

在布局layout.xml中的ListView就是MyListView

yarnbyte avatar Jan 17 '18 06:01 yarnbyte