Android RecyclerView + Paging Library 添加头部刷新会自动滚动的问题分析及解决

本文主要是介绍Android RecyclerView + Paging Library 添加头部刷新会自动滚动的问题分析及解决,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、前言
最近在做一款应用,因为 api 涉及到分页的,所以选择用 RecyclerView + Paging 去做,能省去向下滚动时去处理加载下一页的数据的逻辑,Paging 会自动去加载下一页的内容,首页要做个 Banner, 所以把 Banner 作为 RecyclerView 的 头添加进去,可是添加后下面的数据加载完就会自动滚到下面去,如下图,一下拉刷新,加载完数据后就滚到下面去了

二、问题分析

当我去掉头部刷新,一切是正常的,

所以原因应该就是添加了 Header,导致 position 不准确, RecyclerView 不知道有 Header 的存在,一刷新插入、删除等操作是从 0 开始的,但 Header 在前面霸占着位置了(陈独秀同学在前面站着不坐下),所以插入、删除等操作不要动到 Header 的位置,使其操作正确的 position, 那怎么让其不去动到 Heasder 的位置呢?只有看看源码能不能找到解决的办法了, RecyclerView + Paging Library 用的 Adapter 用的是继承 PagedListAdapter 的,

PagedListAdapter 是使用了 DiffUtil 的,DiffUtil 里的插入、删除等操作是用到 AdapterListUpdateCallback 这个类

AdapterListUpdateCallback 这个类里插入、删除等操作调用的是 RecyclerView.Adapter mAdapter 的,

/*** ListUpdateCallback that dispatches update events to the given adapter.** @see DiffUtil.DiffResult#dispatchUpdatesTo(RecyclerView.Adapter)*/
public final class AdapterListUpdateCallback implements ListUpdateCallback {@NonNullprivate final RecyclerView.Adapter mAdapter;/*** Creates an AdapterListUpdateCallback that will dispatch update events to the given adapter.** @param adapter The Adapter to send updates to.*/public AdapterListUpdateCallback(@NonNull RecyclerView.Adapter adapter) {mAdapter = adapter;}/** {@inheritDoc} */@Overridepublic void onInserted(int position, int count) {mAdapter.notifyItemRangeInserted(position, count);}/** {@inheritDoc} */@Overridepublic void onRemoved(int position, int count) {mAdapter.notifyItemRangeRemoved(position, count);}/** {@inheritDoc} */@Overridepublic void onMoved(int fromPosition, int toPosition) {mAdapter.notifyItemMoved(fromPosition, toPosition);}/** {@inheritDoc} */@Overridepublic void onChanged(int position, int count, Object payload) {mAdapter.notifyItemRangeChanged(position, count, payload);}
}



RecyclerView.Adapter 里用的是 mObservable 的插入、删除等操作,

这个 mObservable 是 AdapterDataObservable 类型的,

AdapterDataObservable 是继承 Observable 的

static class AdapterDataObservable extends Observable<AdapterDataObserver> {public boolean hasObservers() {return !mObservers.isEmpty();}public void notifyChanged() {// since onChanged() is implemented by the app, it could do anything, including// removing itself from {@link mObservers} - and that could cause problems if// an iterator is used on the ArrayList {@link mObservers}.// to avoid such problems, just march thru the list in the reverse order.for (int i = mObservers.size() - 1; i >= 0; i--) {mObservers.get(i).onChanged();}}public void notifyItemRangeChanged(int positionStart, int itemCount) {notifyItemRangeChanged(positionStart, itemCount, null);}public void notifyItemRangeChanged(int positionStart, int itemCount,@Nullable Object payload) {// since onItemRangeChanged() is implemented by the app, it could do anything, including// removing itself from {@link mObservers} - and that could cause problems if// an iterator is used on the ArrayList {@link mObservers}.// to avoid such problems, just march thru the list in the reverse order.for (int i = mObservers.size() - 1; i >= 0; i--) {mObservers.get(i).onItemRangeChanged(positionStart, itemCount, payload);}}public void notifyItemRangeInserted(int positionStart, int itemCount) {// since onItemRangeInserted() is implemented by the app, it could do anything,// including removing itself from {@link mObservers} - and that could cause problems if// an iterator is used on the ArrayList {@link mObservers}.// to avoid such problems, just march thru the list in the reverse order.for (int i = mObservers.size() - 1; i >= 0; i--) {mObservers.get(i).onItemRangeInserted(positionStart, itemCount);}}public void notifyItemRangeRemoved(int positionStart, int itemCount) {// since onItemRangeRemoved() is implemented by the app, it could do anything, including// removing itself from {@link mObservers} - and that could cause problems if// an iterator is used on the ArrayList {@link mObservers}.// to avoid such problems, just march thru the list in the reverse order.for (int i = mObservers.size() - 1; i >= 0; i--) {mObservers.get(i).onItemRangeRemoved(positionStart, itemCount);}}public void notifyItemMoved(int fromPosition, int toPosition) {for (int i = mObservers.size() - 1; i >= 0; i--) {mObservers.get(i).onItemRangeMoved(fromPosition, toPosition, 1);}}
}

而 Observable 是个典型的观察者模式被观察者的写法

/*** Provides methods for registering or unregistering arbitrary observers in an {@link ArrayList}.** This abstract class is intended to be subclassed and specialized to maintain* a registry of observers of specific types and dispatch notifications to them.** @param T The observer type.*/
public abstract class Observable<T> {/*** The list of observers.  An observer can be in the list at most* once and will never be null.*/protected final ArrayList<T> mObservers = new ArrayList<T>();/*** Adds an observer to the list. The observer cannot be null and it must not already* be registered.* @param observer the observer to register* @throws IllegalArgumentException the observer is null* @throws IllegalStateException the observer is already registered*/public void registerObserver(T observer) {if (observer == null) {throw new IllegalArgumentException("The observer is null.");}synchronized(mObservers) {if (mObservers.contains(observer)) {throw new IllegalStateException("Observer " + observer + " is already registered.");}mObservers.add(observer);}}/*** Removes a previously registered observer. The observer must not be null and it* must already have been registered.* @param observer the observer to unregister* @throws IllegalArgumentException the observer is null* @throws IllegalStateException the observer is not yet registered*/public void unregisterObserver(T observer) {if (observer == null) {throw new IllegalArgumentException("The observer is null.");}synchronized(mObservers) {int index = mObservers.indexOf(observer);if (index == -1) {throw new IllegalStateException("Observer " + observer + " was not registered.");}mObservers.remove(index);}}/*** Remove all registered observers.*/public void unregisterAll() {synchronized(mObservers) {mObservers.clear();}}
}

那么是谁及什么时候注册到这个 mObservers 呢?什么时候就是在 RecyclerView setAdapter 或 swapAdapter 的时候

/*** Swaps the current adapter with the provided one. It is similar to* {@link #setAdapter(Adapter)} but assumes existing adapter and the new adapter uses the same* {@link ViewHolder} and does not clear the RecycledViewPool.* <p>* Note that it still calls onAdapterChanged callbacks.** @param adapter The new adapter to set, or null to set no adapter.* @param removeAndRecycleExistingViews If set to true, RecyclerView will recycle all existing*                                      Views. If adapters have stable ids and/or you want to*                                      animate the disappearing views, you may prefer to set*                                      this to false.* @see #setAdapter(Adapter)*/
public void swapAdapter(Adapter adapter, boolean removeAndRecycleExistingViews) {// bail out if layout is frozensetLayoutFrozen(false);setAdapterInternal(adapter, true, removeAndRecycleExistingViews);processDataSetCompletelyChanged(true);requestLayout();
}/*** Set a new adapter to provide child views on demand.* <p>* When adapter is changed, all existing views are recycled back to the pool. If the pool has* only one adapter, it will be cleared.** @param adapter The new adapter to set, or null to set no adapter.* @see #swapAdapter(Adapter, boolean)*/
public void setAdapter(Adapter adapter) {// bail out if layout is frozensetLayoutFrozen(false);setAdapterInternal(adapter, false, true);processDataSetCompletelyChanged(false);requestLayout();
}/*** Replaces the current adapter with the new one and triggers listeners.* @param adapter The new adapter* @param compatibleWithPrevious If true, the new adapter is using the same View Holders and*                               item types with the current adapter (helps us avoid cache*                               invalidation).* @param removeAndRecycleViews  If true, we'll remove and recycle all existing views. If*                               compatibleWithPrevious is false, this parameter is ignored.*/
private void setAdapterInternal(Adapter adapter, boolean compatibleWithPrevious,boolean removeAndRecycleViews) {if (mAdapter != null) {mAdapter.unregisterAdapterDataObserver(mObserver);mAdapter.onDetachedFromRecyclerView(this);}if (!compatibleWithPrevious || removeAndRecycleViews) {removeAndRecycleViews();}mAdapterHelper.reset();final Adapter oldAdapter = mAdapter;mAdapter = adapter;if (adapter != null) {adapter.registerAdapterDataObserver(mObserver);adapter.onAttachedToRecyclerView(this);}if (mLayout != null) {mLayout.onAdapterChanged(oldAdapter, mAdapter);}mRecycler.onAdapterChanged(oldAdapter, mAdapter, compatibleWithPrevious);mState.mStructureChanged = true;
}


Adapter 的 registerAdapterDataObserver 方法

/*** Register a new observer to listen for data changes.** <p>The adapter may publish a variety of events describing specific changes.* Not all adapters may support all change types and some may fall back to a generic* {@link android.support.v7.widget.RecyclerView.AdapterDataObserver#onChanged()* "something changed"} event if more specific data is not available.</p>** <p>Components registering observers with an adapter are responsible for* {@link #unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver)* unregistering} those observers when finished.</p>** @param observer Observer to register** @see #unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver)*/
public void registerAdapterDataObserver(@NonNull AdapterDataObserver observer) {mObservable.registerObserver(observer);
}

那 adapter.registerAdapterDataObserver(mObserver) 这句中的 mObserver 是谁?

原来 mObserver 是 RecyclerViewDataObserver 类型的

private class RecyclerViewDataObserver extends AdapterDataObserver {RecyclerViewDataObserver() {}@Overridepublic void onChanged() {assertNotInLayoutOrScroll(null);mState.mStructureChanged = true;processDataSetCompletelyChanged(true);if (!mAdapterHelper.hasPendingUpdates()) {requestLayout();}}@Overridepublic void onItemRangeChanged(int positionStart, int itemCount, Object payload) {assertNotInLayoutOrScroll(null);if (mAdapterHelper.onItemRangeChanged(positionStart, itemCount, payload)) {triggerUpdateProcessor();}}@Overridepublic void onItemRangeInserted(int positionStart, int itemCount) {assertNotInLayoutOrScroll(null);if (mAdapterHelper.onItemRangeInserted(positionStart, itemCount)) {triggerUpdateProcessor();}}@Overridepublic void onItemRangeRemoved(int positionStart, int itemCount) {assertNotInLayoutOrScroll(null);if (mAdapterHelper.onItemRangeRemoved(positionStart, itemCount)) {triggerUpdateProcessor();}}@Overridepublic void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {assertNotInLayoutOrScroll(null);if (mAdapterHelper.onItemRangeMoved(fromPosition, toPosition, itemCount)) {triggerUpdateProcessor();}}void triggerUpdateProcessor() {if (POST_UPDATES_ON_ANIMATION && mHasFixedSize && mIsAttached) {ViewCompat.postOnAnimation(RecyclerView.this, mUpdateChildViewsRunnable);} else {mAdapterUpdateDuringMeasure = true;requestLayout();}}
}

RecyclerViewDataObserver 是继承 AdapterDataObserver 的

/*** Observer base class for watching changes to an {@link Adapter}.* See {@link Adapter#registerAdapterDataObserver(AdapterDataObserver)}.*/
public abstract static class AdapterDataObserver {public void onChanged() {// Do nothing}public void onItemRangeChanged(int positionStart, int itemCount) {// do nothing}public void onItemRangeChanged(int positionStart, int itemCount, @Nullable Object payload) {// fallback to onItemRangeChanged(positionStart, itemCount) if app// does not override this method.onItemRangeChanged(positionStart, itemCount);}public void onItemRangeInserted(int positionStart, int itemCount) {// do nothing}public void onItemRangeRemoved(int positionStart, int itemCount) {// do nothing}public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {// do nothing}
}

RecyclerViewDataObserver 里用的是 mAdapterHelper 的插入、删除等操作,mAdapterHelper 是 AdapterHelper 类型的

AdapterHelper 是包访问权限的

突破口在 Adapter 的 registerAdapterDataObserver 方法,这个方法是 public 的且不是 final 的,我们可以在自己的 Adapter 覆盖 registerAdapterDataObserver 这个方法,由于 RecyclerViewDataObserver 这个类是 private 的,但 AdapterDataObserver 是 public 的,而且 registerAdapterDataObserver 要的是 AdapterDataObserver 类型的参数,我们可以写个类继承 AdapterDataObserver, 把传进 registerAdapterDataObserver 的 observer 参数和 Header 的个数传进去,在对目标 AdapterDataObserver 操作之前进行偷梁换柱,把 Header 的个数加进去,分析到这里问题就已经可以解决了。

三、问题解决
写个 AdapterDataObserverProxy 类继承 RecyclerView.AdapterDataObserver

class AdapterDataObserverProxy extends RecyclerView.AdapterDataObserver {RecyclerView.AdapterDataObserver adapterDataObserver;int headerCount;public ArticleDataObserver(RecyclerView.AdapterDataObserver adapterDataObserver, int headerCount) {this.adapterDataObserver = adapterDataObserver;this.headerCount = headerCount;}@Overridepublic void onChanged() {adapterDataObserver.onChanged();}@Overridepublic void onItemRangeChanged(int positionStart, int itemCount) {adapterDataObserver.onItemRangeChanged(positionStart + headerCount, itemCount);}@Overridepublic void onItemRangeChanged(int positionStart, int itemCount, @Nullable Object payload) {adapterDataObserver.onItemRangeChanged(positionStart + headerCount, itemCount, payload);}@Overridepublic void onItemRangeInserted(int positionStart, int itemCount) {adapterDataObserver.onItemRangeInserted(positionStart + headerCount, itemCount);}@Overridepublic void onItemRangeRemoved(int positionStart, int itemCount) {adapterDataObserver.onItemRangeRemoved(positionStart + headerCount, itemCount);}@Overridepublic void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {super.onItemRangeMoved(fromPosition + headerCount, toPosition + headerCount, itemCount);}
}

因为 registerAdapterDataObserver 是针对 AdapterDataObserver 进行处理的,所以这里用 AdapterDataObserverProxy 去做代理,对真正的 AdapterDataObserver 操作前把 Header 的个数加进去,所以覆盖后的 registerAdapterDataObserver 就可以这样写,

@Override
public void registerAdapterDataObserver(@NonNull RecyclerView.AdapterDataObserver observer) {super.registerAdapterDataObserver(new AdapterDataObserverProxy(observer, getHeaderCount()));
}

getHeaderCount() 是 Header 的个数,具体怎么写就看自己的情况了,当然覆盖后的 registerAdapterDataObserver 这样写的话就要在 RecyclerView setAdapter 之前先把 Header 添加到 Adapter 去,毕竟 registerAdapterDataObserver 是在 setAdapter 里调用的。

四、后记
RecyclerView + Paging Library 用于做分页虽然好,但还是有很多坑的,现在还有坑没填,用了 RecyclerView + Paging Library 想做个添加 item 和删除 item 的功能还没有头绪,PagedList 虽然继承 AbstractList,但 add 和 remove 方法没有去实现,调用会出现 UnsupportedOperationException,脑阔疼。
 

这篇关于Android RecyclerView + Paging Library 添加头部刷新会自动滚动的问题分析及解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/754987

相关文章

Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单

《Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单》:本文主要介绍Springboot的ThreadPoolTaskScheduler线... 目录ThreadPoolTaskScheduler线程池实现15分钟不操作自动取消订单概要1,创建订单后

详谈redis跟数据库的数据同步问题

《详谈redis跟数据库的数据同步问题》文章讨论了在Redis和数据库数据一致性问题上的解决方案,主要比较了先更新Redis缓存再更新数据库和先更新数据库再更新Redis缓存两种方案,文章指出,删除R... 目录一、Redis 数据库数据一致性的解决方案1.1、更新Redis缓存、删除Redis缓存的区别二

oracle数据库索引失效的问题及解决

《oracle数据库索引失效的问题及解决》本文总结了在Oracle数据库中索引失效的一些常见场景,包括使用isnull、isnotnull、!=、、、函数处理、like前置%查询以及范围索引和等值索引... 目录oracle数据库索引失效问题场景环境索引失效情况及验证结论一结论二结论三结论四结论五总结ora

element-ui下拉输入框+resetFields无法回显的问题解决

《element-ui下拉输入框+resetFields无法回显的问题解决》本文主要介绍了在使用ElementUI的下拉输入框时,点击重置按钮后输入框无法回显数据的问题,具有一定的参考价值,感兴趣的... 目录描述原因问题重现解决方案方法一方法二总结描述第一次进入页面,不做任何操作,点击重置按钮,再进行下

解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题

《解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题》本文主要讲述了在使用MyBatis和MyBatis-Plus时遇到的绑定异常... 目录myBATis-plus-boot-starpythonter与mybatis-spring-b

python实现自动登录12306自动抢票功能

《python实现自动登录12306自动抢票功能》随着互联网技术的发展,越来越多的人选择通过网络平台购票,特别是在中国,12306作为官方火车票预订平台,承担了巨大的访问量,对于热门线路或者节假日出行... 目录一、遇到的问题?二、改进三、进阶–展望总结一、遇到的问题?1.url-正确的表头:就是首先ur

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实

锐捷和腾达哪个好? 两个品牌路由器对比分析

《锐捷和腾达哪个好?两个品牌路由器对比分析》在选择路由器时,Tenda和锐捷都是备受关注的品牌,各自有独特的产品特点和市场定位,选择哪个品牌的路由器更合适,实际上取决于你的具体需求和使用场景,我们从... 在选购路由器时,锐捷和腾达都是市场上备受关注的品牌,但它们的定位和特点却有所不同。锐捷更偏向企业级和专

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

Spring使用@Retryable实现自动重试机制

《Spring使用@Retryable实现自动重试机制》在微服务架构中,服务之间的调用可能会因为一些暂时性的错误而失败,例如网络波动、数据库连接超时或第三方服务不可用等,在本文中,我们将介绍如何在Sp... 目录引言1. 什么是 @Retryable?2. 如何在 Spring 中使用 @Retryable