思路一:通过下方RecyclerView滑动带动整体布局滑动
mRecyclerView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
totalY -= oldScrollY;
if (Math.abs(totalY) <= mHeadLayout.getHeight()) {
mRootView.scrollTo(0, totalY);
}
}
});
可以看出画面产生抖动现象,这是因为当滑动事件在RecyclerView中处理时,除了RecyclerView的本身滑动还有整体View的滑动,使其滑动事件(Event)产生叠加,简单说就是当点击滑动列表为1时,整体平移为1+1=2,此时事件会进行自动修正,即产生抖动现象。
思路二:将滑动事件在外部统一处理
将事件交由最外部View处理,使滑动事件在最外部消费并联动滑行。
此时,由于外部事件的拦截事件无法传入内部,而我们下拉刷新(SmartRefreshLayout)显示是由内部滑动反馈处理,所以内部则无法出现刷新效果。
以上两种思路都是基于Android基础事件传递进行简单改造,实现效果都不是很完美,后期也对这两种方式进行结合,可以初步完成上方滑动和下方列表滑动可相互不影响的情况下完成滑动,但也产生一种缺陷就是在同一事件序列中(即ACTION_DOWN、ACTION_MOVE、ACTION_UP)只可在同一控件上处理,即一次滑动动作只可被一个控件消费(父View或者RecyclerView),无法实现连续对列表进行滑动操作。
思路三:NestedScrollingParent 和 NestedScrollingChild 解决滑动冲突
NestedScrollingParent 和 NestedScrollingChild 都是接口,NestedScrollingParent 接口用于外部View实现, NestedScrollingChild 用于内部View实现。
NestedScrollingParent 和 NestedScrollingChild 解决滑动冲突的机制:实现了NestedScrollingChild接口的内部View在滑动的时候,首先将滑动距离dx和dy交给实现了NestedScrollingParent接口的外部View(可以不是直接父View),外部View可对其进行部分消耗,剩余的部分还给内部View。
NestedScrollingParent接口
public interface NestedScrollingParent {
//准备开始滑动。参数nestedScrollAxes为getNestedScrollAxes方法返回的值。返回值表示是否接收内部View(可以是非直接子View)滑动时的参数。
boolean onStartNestedScroll(View child, View target, int nestedScrollAxes);
//接收内部View(可以是非直接子View)滑动
void onNestedScrollAccepted(View child, View target, int nestedScrollAxes);
//停止接收内部View(可以是非直接子View)滑动
void