关于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;
            }
        });
复制代码
原文地址:https://www.cnblogs.com/wbp0818/p/5391039.html