自定义ListView分别获取子类和父类的权限

定制ListView组件
本文介绍了一个定制的ListView组件——InnerListView,该组件通过重写onMeasure方法实现高度限制,并通过onInterceptTouchEvent方法处理触摸事件,使得ListView在嵌套滚动场景下能够正确处理滑动操作。

1、整理项目中遇到的ListView使用方法:


import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;

public class InnerListView extends ListView {

    private int maxHeight;

    public int getMaxHeight() {
        return maxHeight;
    }

    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
    }

    public InnerListView(Context context) {
        super(context);
    }

    public InnerListView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public InnerListView(Context context, AttributeSet attributeSet,
                         int defStyle) {
        super(context, attributeSet, defStyle);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
//        if (maxHeight > -1) {
//            heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
//        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //当手指触到listview的时候,让父ScrollView交出ontouch权限,也就是让父scrollview
                setParentScrallAble(false);
                break;

            case MotionEvent.ACTION_UP:
                ////当手指松开时,让父ScrollView重新拿到onTouch权限
                setParentScrallAble(true);
                break;

            default:
                break;
        }

        return super.onInterceptTouchEvent(ev);
    }

    private void setParentScrallAble(boolean flag) {
        // TODO Auto-generated method stub
        getParent().requestDisallowInterceptTouchEvent(!flag);

    }



}

层参考网络文档,具体链接已经记不清了





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值