前言:
在實際的項目開發中,項目需求所需,需要將ScrollView和RecyclerView嵌套使用,在使用過程中發現兩者配合使用存在滑動沖突問題,現將解決辦法記錄在冊,以便后續查看。話不多少,直接上,能動手絕不瞎嗶嗶。
1.最暴力的辦法就是直接禁止recyclerView滑動;
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2) {
@Override
public boolean canScrollVertically() {
return false;
}
};
2. 重寫LayoutManager,重寫canScrollVertically()這個方法。
public class ScrollGridLayoutManagerw extends GridLayoutManager {
//是否支持滑動
private boolean isScrollEnabled = true;
public ScrollGridLayoutManagerw(Context context, int spanCount) {
super(context, spanCount);
}
public ScrollGridLayoutManagerw(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public ScrollGridLayoutManagerw(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}
/**
* 是否支持滑動
*
* @param flag
*/
public void setScrollEnabled(boolean flag) {
this.isScrollEnabled = flag;
}
@Override
public boolean canScrollVertically() {
//isScrollEnabled:recyclerview 是否支持滑動
//super.canScrollVertically():是否為豎直方向滾動
return isScrollEnabled && super.canScrollVertically();
}
}
使用的時候在代碼中設置:
LayoutManager.setScrollEnabled(false);
3. 新建一個類繼承LayoutManager,通過重寫onMeasure()方法重新測量RecyclerView寬高,在代碼中設置RecyclerView.setNestedScrollingEnabled(false)來解決。
public class ScrollGridLayoutManager extends GridLayoutManager {
private static final String TAG = ScrollGridLayoutManager.class.getSimpleName();
public ScrollGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}
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);
Log.i(TAG, "onMeasure called. \nwidthMode " + widthMode
+ " \nheightMode " + heightSpec
+ " \nwidthSize " + widthSize
+ " \nheightSize " + heightSize
+ " \ngetItemCount() " + getItemCount());
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 {
}
}
}
使用的時候在代碼中設置:
ScrollGridLayoutManager gridLayoutManager = new ScrollGridLayoutManager(getContext(),2);
recyclerView.setNestedScrollingEnabled(false);