出处 Android开发艺术探索
说明:本文用的方法是(内部拦截法)
首先说下一个普遍的需求:
这个需求很普遍;像今日头条,网易新闻什么的都是这样
1:底部有四个按钮点击可以切换;左右滑可以切换(不用想用viewpager)
2:点击某个底部按钮里面又是需要三个按钮点击可以切换,左右滑可以切换(不用想也是viewpager)
3:然后在第二个viewpager里的某个fragment里的页面布局是:顶部固定某些内容(比如3个button)然后下面是个列表;这个页面我的做法是:NestedScrollView+RecyclerView;为什么用它们两个呢,因为google为我们解决了以前最头疼的一件事:scrollview嵌套listview,listview不能显示全的问题,还是复用问题;用NestedScrollView+RecyclerView首先不用考虑RecyclerView不能显示全的问题;还有RecyclerView功能比Listview功能更强大不多说; 然而问题来了,在这个页面会出现非常严重的页面滑动卡顿的问题,虽然google帮我们干了许多事,但是有些还是需要自己去实现
总结一下总的布局就是:
viewpager+viewpager(NestedScrollView+RecyclerView)
直接看那个卡顿布局的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants">
<org.jokar.eventconflict.widget.MyLinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button3" />
</LinearLayout>
<org.jokar.eventconflict.widget.MyRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</org.jokar.eventconflict.widget.MyLinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
如上代码并没有特别之处;只有三处不同:
1.NestedScrollView 加上descendantFocusability(去掉好像也可以,因为写的demo快速滑动时候不卡但是慢速滑动的时候感觉有时候会卡一下;这是个遗留的问题暂时没有找到解决方法)
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants">
2.就是重写的两个view
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context) {
this(context, null);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int action = ev.getAction();
if (action == MotionEvent.ACTION_DOWN) {
return false;
} else {
return true;
}
}
}
public class MyRecyclerView extends RecyclerView {
private static final String TAG = "MyRecyclerView";
public MyRecyclerView(Context context) {
this(context, null);
}
// 分别记录上次滑动的坐标(onInterceptTouchEvent)
private int mLastXIntercept = 0;
private int mLastYIntercept = 0;
public MyRecyclerView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int x = (int) ev.getX();
int y = (int) ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
getParent().requestDisallowInterceptTouchEvent(true);
break;
}
case MotionEvent.ACTION_MOVE: {
int deltaX = x - mLastXIntercept;
int deltaY = y - mLastYIntercept;
//如果是左右滑动
if (Math.abs(deltaX) > Math.abs(deltaY)) {
getParent().requestDisallowInterceptTouchEvent(false);
}
break;
}
case MotionEvent.ACTION_UP: {
getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
mLastXIntercept = x;
mLastYIntercept = y;
return super.dispatchTouchEvent(ev);
}
}
代码很简单,首先就是重写父view的onInterceptTouchEvent,然后重写子view的dispatchTouchEvent在上下滑动的时候获取滑动事件;这样就行了;
这里要注意一个问题,因为把父view的事件全部拦截了父view不会进行事件分发了,所以子view没有重写dispatchTouchEvent的话是无法进行点击事件的;
效果如下: