开源项目地址http://blog.csdn.net/w7822938/article/details/47173047
本人项目用到了这个http://blog.csdn.net/w7822938/article/details/47173047提供的库,但是发现在嵌套RecyclerView存在不完善的地方,
也就是在RecyclerView下滑的时候会存在头部布局与RecyclerView一起滑动的情况,本来的需求应该是:当RecyclerView下拉到顶部的时候头部布局才能滑动;
经过研究发现其实是ScrollableLayout判断RecyclerView是否滑动到最上面的时候有问题,
ScrollableLayout是通过成员变量mHelper的引用类ScrolableHelper来判断RecyclerView是否在顶部,
ScrolableHelper中关于RecyclerView是否在顶部的实现有问题
我的做法是:创建
MyScrollableHelper extends ScrollableHelper
实现如下:
public class MyScrollableHelper extends ScrollableHelper { private ScrollableContainer container; @Override public void setCurrentScrollableContainer(ScrollableContainer scrollableContainer) { this.container = scrollableContainer; } @Override public boolean isTop() { if (container.getScrollableView()==null)return true; if (container.getScrollableView() instanceof FindRecyclerView) { FindRecyclerView scrollableView = (FindRecyclerView) container.getScrollableView(); if (scrollableView.isTop()) return true; else return false; } return super.isTop(); } }
其中FindRecylerView实现如下
public class FindRecyclerView extends RecyclerView { public FindRecyclerView(Context context) { this(context, null); } public FindRecyclerView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public boolean isTop() { RecyclerView.LayoutManager layoutManager = getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { int firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition(); if (firstVisibleItemPosition == 0) { return true; } } return false; } }
然后通过反射将该类注入到ScrollableLayout中实现如下
private void initView() { try {//由于该框架ScrollableLayout存在不完善的地方,所以重写了ScrollableHelper通反射注入到了框架中 Field field = scrollableLayout.getClass().getDeclaredField("mHelper"); field.setAccessible(true); MyScrollableHelper myScrollableHelper = new MyScrollableHelper(); myScrollableHelper.setCurrentScrollableContainer(this); field.set(scrollableLayout,myScrollableHelper); } catch (Exception e) { e.printStackTrace(); } }
这样问题就解决了;
当然你可以依赖他的工程直接修改他的源代码,但这样会造成编译器编译缓慢,不利于库的版本的管理以及同步