解决ScrollView嵌套RecyclerView的显示及滑动问题

时间:2023-03-08 19:58:36

项目中时常需要实现在ScrollView中嵌入一个或多个RecyclerView。这一做法通常会导致如下几个问题

  • 页面滑动卡顿
  • ScrollView高度显示不正常
  • RecyclerView内容显示不全

本文将利用多种方式分别解决上述问题

滑动卡顿解决方案

若只存在滑动卡顿这一问题,可以采用如下两种简单方式快速解决

利用RecyclerView内部方法

recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);

其中,setHasFixedSize(true)方法使得RecyclerView能够固定自身size不受adapter变化的影响;而setNestedScrollingeEnabled(false)方法则是进一步调用了RecyclerView内部NestedScrollingChildHelper对象的setNestedScrollingeEnabled(false)方法,如下

public void setNestedScrollingEnabled(boolean enabled) {
getScrollingChildHelper().setNestedScrollingEnabled(enabled);
}

进而,NestedScrollingChildHelper对象通过该方法关闭RecyclerView的嵌套滑动特性,如下

public void setNestedScrollingEnabled(boolean enabled) {
if (mIsNestedScrollingEnabled) {
ViewCompat.stopNestedScroll(mView);
}
mIsNestedScrollingEnabled = enabled;
}

如此一来,限制了RecyclerView自身的滑动,整个页面滑动仅依靠ScrollView实现,即可解决滑动卡顿的问题

重写LayoutManager

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this) {
@Override
public boolean canScrollVertically() {
return false;
}
};

这一方式使得RecyclerView的垂直滑动始终返回false,其目的同样是为了限制自身的滑动

综合解决方案

若是需要综合解决上述三个问题,则可以采用如下几种方式

插入LinearLayout/RelativeLayout

在原有布局中插入一层LinearLayout/RelativeLayout,形成如下布局

解决ScrollView嵌套RecyclerView的显示及滑动问题

重写LayoutManager

该方法的核心思想在于通过重写LayoutManager中的onMeasure()方法,即

@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
super.onMeasure(recycler, state, widthSpec, heightSpec);
}

重新实现RecyclerView高度的计算,使得其能够在ScrollView中表现出正确的高度,具体重写方式可参考这篇文章

http://www.cnblogs.com/tianzh...

重写ScrollView

该方法的核心思想在于通过重写ScrollView的onInterceptTouchEvent(MotionEvent ev)方法,拦截滑动事件,使得滑动事件能够直接传递给RecyclerView,具体重写方式可参考如下

/**
* Created by YH on 2017/10/10.
*/ public class RecyclerScrollView extends ScrollView {
private int slop;
private int touch; public RecyclerScrollView(Context context) {
super(context);
setSlop(context);
} public RecyclerScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
setSlop(context);
} public RecyclerScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setSlop(context);
} /**
* 是否intercept当前的触摸事件
* @param ev 触摸事件
* @return true:调用onMotionEvent()方法,并完成滑动操作
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// 保存当前touch的纵坐标值
touch = (int) ev.getRawY();
break;
case MotionEvent.ACTION_MOVE:
// 滑动距离大于slop值时,返回true
if (Math.abs((int) ev.getRawY() - touch) > slop) return true;
break;
} return super.onInterceptTouchEvent(ev);
} /**
* 获取相应context的touch slop值(即在用户滑动之前,能够滑动的以像素为单位的距离)
* @param context ScrollView对应的context
*/
private void setSlop(Context context) {
slop = ViewConfiguration.get(context).getScaledTouchSlop();
}
}

事实上,尽管我们能够采用多种方式解决ScrollView嵌套RecyclerView所产生的一系列问题,但由于上述解决方式均会使得RecyclerView在页面加载过程中一次性显示所有内容,因此当RecyclerView下的条目过多时,将会对影响整个应用的运行效率。基于此,在这种情况下我们应当尽量避免采用ScrollView嵌套RecyclerView的布局方式