最新业务需求变化,一个页面多个Recycleview+Viewpager,viewpager实现左右滑动,且可以手动滑动,页面逻辑简单,就是数据比较大,最初的时候实现有滑动冲突,后边使用NestedScrollView可以实现滑动,但是Viewpager不能实现手动滑动,Recycleview的item事件冲突(这个只在华为7.0手机上出现,华为8.0及三星手机上未发现问题)网上也是各种找,后边看是NestedScrollView里面的onInterceptTouchEvent给拦截了
下面贴一下封装完成的NestedScrollView的类,完美实现
public class JudgeNestedScrollView extends NestedScrollView {
private boolean isNeedScroll = true;
private float xDistance, yDistance, xLast, yLast;
private int scaledTouchSlop;
public JudgeNestedScrollView(@NonNull Context context) {
super(context);
}
public JudgeNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public JudgeNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
xLast = ev.getX();
yLast = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
final float curX = ev.getX();
final float curY = ev.getY();
xDistance += Math.abs(curX - xLast);
yDistance += Math.abs(curY - yLast);
xLast = curX;
yLast = curY;
Log.e("SiberiaDante", "xDistance :" + xDistance + "---yDistance:" + yDistance);
return !(xDistance >= yDistance || yDistance < scaledTouchSlop) && isNeedScroll;
}
return super.onInterceptTouchEvent(ev);
}
/*
改方法用来处理NestedScrollView是否拦截滑动事件
*/
public void setNeedScroll(boolean isNeedScroll) {
this.isNeedScroll = isNeedScroll;
}
}
参考:http://www.cnblogs.com/shen-hua/p/8052459.html
贴一下布局:
<com.xxxx.app.widget.JudgeNestedScrollView android:id="@+id/scorll" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:focusableInTouchMode="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:visibility="gone"> <com.zhy.autolayout.AutoLinearLayout android:id="@+id/lin_content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F1F1F1" android:orientation="vertical"> <!--轮播图--> <FrameLayout android:id="@+id/view_lbt" android:layout_width="wrap_content" android:layout_height="590px" /> <!--详情--> <TextView android:id="@+id/tv_header_word" android:layout_width="match_parent" android:layout_height="60px" android:layout_alignTop="@+id/rv" android:layout_marginRight="55px" android:background="@color/gainsboro" android:paddingLeft="40px" android:paddingTop="10px" android:text="A" android:textColor="@color/black" android:textSize="14sp" android:visibility="visible" /> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:focusable="false" android:layout_height="wrap_content" ></android.support.v7.widget.RecyclerView> </com.zhy.autolayout.AutoLinearLayout> </com.xxxx.app.widget.JudgeNestedScrollView>
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233641.html原文链接:https://javaforall.net