完美解决因scrollview和recycleview引起的滑动
今天做了这么一个布局,scrollView嵌套一个fragment,fragment里面是一个viewpager,viewpager里面又嵌套fragment,fragment里面有一个横向的recycleview,recycleview里面又是一个纵向的recycleview,同时scrollview还嵌套了Google提供的下拉刷新。没办法,需求就是这个鬼样子,被冲突弄得不要不要的,当然还是有解决办法的;
冲突1:recycleview嵌套recycleview:现象,滑动时错位,并且item之间的距离变大
解决:1.自定义一个FullLinnearLayoutManger:
public class FullyLinearLayoutManager extends LinearLayoutManager {
private static final String TAG = FullyLinearLayoutManager.class.getSimpleName();
public FullyLinearLayoutManager(Context context) {
super(context);
}
public FullyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
private int[] mMeasuredDimension = new int[2];
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
int width = 0;
int height = 0;
for (int i = 0; i < getItemCount(); i++) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
if (getOrientation() == HORIZONTAL) {
width = width + mMeasuredDimension[0];
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
height = height + mMeasuredDimension[1];
if (i == 0) {
width = mMeasuredDimension[0];
}
}
}
switch (widthMode) {
case View.MeasureSpec.EXACTLY:
width = widthSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
setMeasuredDimension(width, height);
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
try {
View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight(), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
recycler.recycleView(view);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}
然后设置:
FullyLinearLayoutManager fullyLinearLayoutManager = new FullyLinearLayoutManager(getActivity());
fullyLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
/**
* 当平滑滚动启用时,滚动条把手的位置和大小基于可见条目的可见像素数来计算。
* 该处里假定所有列表条目具有相同的高度。如果你使用条目高度不同的类表, 滚动条会在用户滚动过程中改变大小。
* 为了避免这种情况,应该禁用该特性。 当平滑滚动被禁用后,滚动条把手的大小和位置只是基于适配器中的条目数, 以及适配器中的可见条目来确定。
* 这样可以为使用可变高条目列表的用户, 提供稳定的滚动条。
/
fullyLinearLayoutManager.setSmoothScrollbarEnabled(true);
//TODO 开启自动测绘
fullyLinearLayoutManager.setAutoMeasureEnabled(true);
recyclerView.setLayoutManager(fullyLinearLayoutManager);
//设置不允许嵌套滑动,因为嵌套滑动的话会导致错乱
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(mAdapter);
2.scrollview与recycleview的冲突
将viewpager放在一个自定义的linearlayout里面,然后重写方法
/*
* 当事件,分发到该层时,调用requestDisallowInterceptTouchEvent,禁止该控件的父控件响应事件
* @param event
* @return
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("@@@", "onTouchEvent: ");
getParent().requestDisallowInterceptTouchEvent(true);
break;
}
return super.onInterceptTouchEvent(event);
}
3.滑动那个纵向recycleview的时候,总是被下拉刷新事件抢占焦点
这个简单了,将scrollview替换成NestscrollView就可以了,因为NestscrollView本来就是Google用来解决冲突事件,而开发的一个控件。
总结:上述方法:看似比较麻烦,还有点重复了,但是胜在稳定,可以完全杜绝冲突的情况发生。
zhey