zoukankan      html  css  js  c++  java
  • 关于ScrollView中嵌套listview焦点滑动问题 解决

    (第三种,第四种简单推荐使用)

    在这里我要提出的是,listview能滚动的前提是:当listview本身的高度小于listview里的子view。

    第一种方法

    只需在MainActivity中 找到listview 和 scrollview

    然后给listview设置监听事件

    复制代码
    listView.setOnTouchListener(new OnTouchListener() {
                
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                  
                    if(event.getAction() == MotionEvent.ACTION_UP){  
                        scrollView.requestDisallowInterceptTouchEvent(false);  
                    }else{  
                        scrollView.requestDisallowInterceptTouchEvent(true);  
                    }  
                    return false;
                }
            });
    复制代码

    第二种方法

    只需重写listview即可

    复制代码
    package com.bawei.day06;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.ListView;
    
    public class ListViewForScrollView extends ListView {
        int mLastMotionY;
        boolean bottomFlag;
        public ListViewForScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
        }
        
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            
            if (bottomFlag) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            // TODO Auto-generated method stub
            int y = (int) ev.getRawY();
            switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastMotionY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaY = y - mLastMotionY;
                if (deltaY < 0) {
                    View child = getChildAt(0);
                    if (child != null) {
                        if (getLastVisiblePosition() == (getChildCount()-1) && child.getBottom() == (getChildCount()-1)) {
                            bottomFlag = true;
                            getParent().requestDisallowInterceptTouchEvent(true);
                        }
    
                        int bottom = child.getBottom();
                        int padding = getPaddingTop();
                        if (getLastVisiblePosition() == (getChildCount()-1)
                                && Math.abs(bottom - padding) >= 20) {
                            bottomFlag = true;
                            getParent().requestDisallowInterceptTouchEvent(true);
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                break;
            }
            return super.onTouchEvent(ev);
        }
    
        public void setBottomFlag(boolean flag) {
            bottomFlag = flag;
        }
    }
    复制代码

    第三种方法

    只需重写listview即可

    复制代码
    package com.bawei.day06;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.ListView;
    
    public class ListViewForScrollView extends ListView {
        int mLastMotionY;
        boolean bottomFlag;
        public ListViewForScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
        }
    
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            // TODO Auto-generated method stub
            getParent().requestDisallowInterceptTouchEvent(true);
            
            return  super.dispatchTouchEvent(ev);
        }
    
    }
    复制代码

    第四种方法

    只需在MainActivity中 找到listview 

    然后给listview设置监听事件

    复制代码
    listView.setOnTouchListener(new OnTouchListener() {
                
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    listView.getParent().requestDisallowInterceptTouchEvent(true);
                    return false;
                }
            });
    复制代码
  • 相关阅读:
    MVC HtmlHelper用法大全
    非常完善的Log4net详细说明
    SQLSERVER2008R2正确使用索引
    DataReader和DataSet区别
    淘宝下单高并发解决方案
    承接小程序外包 微信小程序外包 H5外包 就找北京动点软件
    H5外包 微信小程序外包 小程序外包 就找北京动点开发团队
    NGUI外包开发总结一下今天的收获
    祝大家2018事业有事,大吉大利!
    AR图像识别 AR识别图像 AR摄像头识别 外包开发 AR识别应用开发就找北京动点软件
  • 原文地址:https://www.cnblogs.com/wbp0818/p/5391039.html
Copyright © 2011-2022 走看看