解决6.0以上ScrollView嵌套RecyclerView能同时滑动的问题。

本文介绍了如何处理Android 6.0及以上版本中ScrollView内嵌RecyclerView时出现的滑动冲突问题。通过重写LinearLayoutManager和ScrollView的特定方法,可以实现两者同时滑动并保持滑动惯性。
摘要由CSDN通过智能技术生成

在4.X的版本中ScrollView嵌套RecyclerView只需要重写LinearLayoutManager即可,同时还解决了显示不全的问题,具体实现如下:

import java.lang.reflect.Field;
import android.content.Context;
import android.graphics.Rect;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class FullyLinearLayoutManager extends LinearLayoutManager {

	private static boolean canMakeInsetsDirty = true;
	private static Field insetsDirtyField = null;

	private static final int CHILD_WIDTH = 0;
	private static final int CHILD_HEIGHT = 1;
	private static final int DEFAULT_CHILD_SIZE = 100;

	private final int[] childDimensions = new int[2];
	private final RecyclerView view;

	private int childSize = DEFAULT_CHILD_SIZE;
	private boolean hasChildSize;
	private int overScrollMode = ViewCompat.OVER_SCROLL_ALWAYS;
	private final Rect tmpRect = new Rect();

	public FullyLinearLayoutManager(Context context) {
		super(context);
		this.view = null;
	}

	public FullyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
		super(context, orientation, reverseLayout);
		this.view = null;
	}

	public FullyLinearLayoutManager(RecyclerView view) {
		super(view.getContext());
		this.view = view;
		this.overScrollMode = ViewCompat.getOverScrollMode(view);
	}

	public FullyLinearLayoutManager(RecyclerView view, int orientation, boolean reverseLayout) {
		super(view.getContext(), orientation, reverseLayout);
		this.view = view;
		this.overScrollMode = ViewCompat.getOverScrollMode(view);
	}

	public void setOverScrollMode(int overScrollMode) {
		if (overScrollMode < ViewCompat.OVER_SCROLL_ALWAYS || overScrollMode > ViewCompat.OVER_SCROLL_NEVER)
			throw new IllegalArgumentException("Unknown overscroll mode: " + overScrollMode);
		if (this.view == null) throw new IllegalStateException("view == null");
		this.overScrollMode = overScrollMode;
		ViewCompat.setOverScrollMode(view, overScrollMode);
	}

	public static int makeUnspecifiedSpec() {
		return View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
	}

	@Override
	public voi
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值