您的位置:首页 > 其它

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

2016-04-14 14:39 459 查看
(第三种,第四种简单推荐使用)

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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: