android listview display all available items without scroll with static header

前端 未结 12 658
一生所求
一生所求 2020-12-02 10:12

I\'m having a little difficulties while trying to get a certain layout to work: I want to have list. List does not have to be scrollable, but should be shown completely. But

12条回答
  •  青春惊慌失措
    2020-12-02 10:25

    If someone still has the problem then you can make customList and add onMesure() method just like I implemented it:

    public class ScrolleDisabledListView extends ListView {
    
    private int mPosition;
    
    public ScrolleDisabledListView(Context context) {
        super(context);
    }
    
    public ScrolleDisabledListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public ScrolleDisabledListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;
    
        if (actionMasked == MotionEvent.ACTION_DOWN) {
            // Record the position the list the touch landed on
            mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
            return super.dispatchTouchEvent(ev);
        }
    
        if (actionMasked == MotionEvent.ACTION_MOVE) {
            // Ignore move events
            return true;
        }
    
        if (actionMasked == MotionEvent.ACTION_UP) {
            // Check if we are still within the same view
            if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
                super.dispatchTouchEvent(ev);
            } else {
                // Clear pressed state, cancel the action
                setPressed(false);
                invalidate();
                return true;
            }
        }
    
        return super.dispatchTouchEvent(ev);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
    }
    

提交回复
热议问题