首页 > 其他 > 详细

关于ScrollView中嵌套listview焦点滑动问题 解决

时间:2016-04-14 15:41:12      阅读:190      评论:0      收藏:0      [点我收藏+]

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

在这里我要提出的是,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;
            }
        });

 

关于ScrollView中嵌套listview焦点滑动问题 解决

原文:http://www.cnblogs.com/1426837364qqcom/p/5388902.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!