在listview 与ScrollVIew 冲突

来源:互联网 发布:千里马计划软件官方 编辑:程序博客网 时间:2024/05/02 01:59

    public MylListView(Context context, AttributeSet attrs, int defStyleAttr,
            int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        // TODO Auto-generated constructor stub
    }

    public MylListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public MylListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MylListView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    
    @Override
    /**
     * 重写该方法,达到使ListView适应ScrollView的效果  
     * 设置为最大值
     */
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(370,
        MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

    }








        setContentView(R.layout.activity_my_list_view);
        sc = (ScrollView) findViewById(R.id.sl);
        lv = (ListView) findViewById(R.id.lv);
        ll = (LinearLayout) findViewById(R.id.ll);
        
        
//        触摸的时候 系统先调用 listview 的action 所以你让这些父类 把事件向下传递
//        
        lv.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
        
                if(event.getAction()==MotionEvent.ACTION_DOWN&&event.getAction()==MotionEvent.ACTION_MOVE){                    
//                    这个事件就是 ScrollView去请求父类  所以 不用进行消费
//                    父类进行拦截  就是不分发给scrollview了
//                    onInterceptTouchEvent  false就是不拦截了    ture 是拦截
//                    sc要进行拦截  让listview能进行滑动
                    sc.requestDisallowInterceptTouchEvent(false);
               
                    
                }else{
                     // 我们告诉父组件,不要拦截我的事件   我的事件自己处理  就是让scrollview 具有滑动了
                    sc.requestDisallowInterceptTouchEvent(true);

                }
                return false;
            }
        });
        
        lv.setOnScrollListener(new OnScrollListener() {
            
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
//             为什么会正好进行相反那
                if(lv.getLastVisiblePosition()==15){
                    System.out.println("------");
//                    事件进分发  拦截
                    sc.requestDisallowInterceptTouchEvent(false);                
                }else{
//                    不让拦截我的事件 让scrllview进行滑动
                    sc.requestDisallowInterceptTouchEvent(true);    
                }
                
           







0 0
原创粉丝点击