Android list view inside a scroll view

后端 未结 30 2125
一向
一向 2020-11-21 13:43

I have an android layout which has a scrollView with a number of elements with in it. At the bottom of the scrollView I have a listView

30条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 13:43

    As others had already mentioned, don't use ListView inside a ScrollView.

    To workaround, you can use a LinearLayout, but to still keep things neat - populate your LinearLayout with an Adapter, same as you do with a ListView

    You can use this class as a LinearLayout replacement that supports Adapters

    import android.content.Context;
    import android.database.DataSetObserver;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.LinearLayout;
    
    public class AdaptableLinearLayout extends LinearLayout {
    
    private BaseAdapter mAdapter;
    
    private int mItemCount = 0;
    
    private boolean mDisableChildrenWhenDisabled = false;
    
    private int mWidthMeasureSpec;
    private int mHeightMeasureSpec;
    
    
    public AdaptableLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    
    public BaseAdapter getAdapter() {
        return mAdapter;
    }
    
    public void setAdapter(BaseAdapter adapter) {
        mAdapter = adapter;
        adapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                updateLayout();
                super.onChanged();
            }
    
            @Override
            public void onInvalidated() {
                updateLayout();
                super.onInvalidated();
            }
        });
        updateLayout();
    }
    
    private void updateLayout() {
        mItemCount = mAdapter.getCount();
        requestLayout();
        invalidate();
    }
    
    /**
     * set size for the current View
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidthMeasureSpec = widthMeasureSpec;
        mHeightMeasureSpec = heightMeasureSpec;
    
        removeAllViewsInLayout();
        for (int i = 0; i < mItemCount; i++) {
            makeAndAddView(i);
        }
    }
    
    private View makeAndAddView(int position) {
        View child;
    
        // Nothing found in the recycler -- ask the adapter for a view
        child = mAdapter.getView(position, null, this);
    
        // Position the view
        setUpChild(child, position);
    
        return child;
    
    }
    
    private void setUpChild(View child, int position) {
    
        ViewGroup.LayoutParams lp = child.getLayoutParams();
        if (lp == null) {
            lp = generateDefaultLayoutParams();
        }
        addViewInLayout(child, position, lp);
    
        // Get measure specs
        int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec, getPaddingTop() + getPaddingBottom(), lp.height);
        int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec, getPaddingLeft() + getPaddingRight(), lp.width);
    
        // Measure child
        child.measure(childWidthSpec, childHeightSpec);
    
        int childLeft;
        int childRight;
    
        // Position vertically based on gravity setting
        int childTop = getPaddingTop() + ((getMeasuredHeight() - getPaddingBottom() - getPaddingTop() - child.getMeasuredHeight()) / 2);
        int childBottom = childTop + child.getMeasuredHeight();
    
        int width = child.getMeasuredWidth();
        childLeft = 0;
        childRight = childLeft + width;
    
        child.layout(childLeft, childTop, childRight, childBottom);
    
        if (mDisableChildrenWhenDisabled) {
            child.setEnabled(isEnabled());
        }
    }
    }
    

提交回复
热议问题