【Android】ScrollView 嵌套 ListView 顯示不全以及最後一個item顯示不全的問題

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章