美文网首页
【Android】ScrollView 嵌套 ListView

【Android】ScrollView 嵌套 ListView

作者: DawnYu | 来源:发表于2019-08-15 23:46 被阅读0次

1. ListView 数据只显示一条

ScrollView 里直接嵌套 ListView 时,数据只能显示一条,通常会重写 ListViewonMeasure() 方法,也可以重新计算高度。

1.1 ListView 中的 item 高度固定

1.1.1 继承 ListView 重写 onMeasure() 方法

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

1.1.2 手动计算 ListView 的高度

public static void setListViewHeightBasedOnChildren(ListView listView) {
    //获取ListView对应的Adapter
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
    // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) { //listAdapter.getCount()返回数据项的数目
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0); //计算子项View 的宽高
        totalHeight += listItem.getMeasuredHeight(); //统计所有子项的总高度
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    //listView.getDividerHeight()获取子项间分隔符占用的高度
    //params.height最后得到整个ListView完整显示需要的高度
    listView.setLayoutParams(params);
}

1.2 最后一个 item 显示不全的问题

1.2.1 取消设置 ListViewdivider 高度

这里我没有试过方法 2,但是在使用方法 1 重写 onMeasure() 时,在 小米 5 手机上遇到了最后一个 item 始终显示不全的问题,但在另外几个测试机上是正常显示的。
后来在谷歌搜索到,原来是给 ListView 设置了 divider 高度导致的,因为重写 onMeasure() 时没有计算 divider 的高度,去掉这个设置就行了。

如果 item 高度不固定,也可以使用下述方法 1.2.2 。

1.2.2 ListView 中的 item 高度不固定,自定义 ListView

public class NestedListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {

    private int listViewTouchAction;
    private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;

    public NestedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        listViewTouchAction = -1;
        setOnScrollListener(this);
        setOnTouchListener(this);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, -1);
            }
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int newHeight = 0;
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode != MeasureSpec.EXACTLY) {
            ListAdapter listAdapter = getAdapter();
            if (listAdapter != null && !listAdapter.isEmpty()) {
                int listPosition = 0;
                for (listPosition = 0; listPosition < listAdapter.getCount()
                        && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                    View listItem = listAdapter.getView(listPosition, null, this);
                    //now it will not throw a NPE if listItem is a ViewGroup instance
                    if (listItem instanceof ViewGroup) {
                        listItem.setLayoutParams(new LayoutParams(
                                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    }
                    listItem.measure(widthMeasureSpec, heightMeasureSpec);
                    newHeight += listItem.getMeasuredHeight();
                }
                newHeight += getDividerHeight() * listPosition;
            }
            if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
                if (newHeight > heightSize) {
                    newHeight = heightSize;
                }
            }
        } else {
            newHeight = getMeasuredHeight();
        }
        setMeasuredDimension(getMeasuredWidth(), newHeight);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, 1);
            }
        }
        return false;
    }
}

注:1.1.1 和 1.2.1 是实际碰到的问题和使用的方法。1.1.2 和 1.2.2 待验证,记录一下笔记。


参考:

  1. 解决滑动冲突带来的问题
  2. ScrollView嵌套ListView显示不全解决方案
  3. Android list view inside a scroll view
  4. ListView inside ScrollView is not scrolling on Android

相关文章

网友评论

      本文标题:【Android】ScrollView 嵌套 ListView

      本文链接:https://www.haomeiwen.com/subject/sgrxsctx.html