当前位置:   article > 正文

Android Paging数据刷新及原理解析_pagingdataadapter.refresh 正在刷新调用次方法有效吗

pagingdataadapter.refresh 正在刷新调用次方法有效吗

Paging

刷新数据以及解析

从刷新讲起

流程

Paging的确很好用,省了很多步骤,但跟以往简单的刷新不同的是,Paging比较麻烦,不能直接清空Adapter里面的数据了和直接请求第一页的数据。以前清除列表,再重新请求第一页数据就可以了。那么Paging如何刷新呢?大致上也和以前用法一样,无外乎就是清除数据并加载新数据

清除Adapter里面的数据

submitList()通过传入null值,如下源码:

public void submitList(final PagedList<T> pagedList) {
   
    ...

    // incrementing generation means any currently-running diffs are discarded when they finish
    final int runGeneration = ++mMaxScheduledGeneration;

    if (pagedList == null) {
   
        int removedCount = getItemCount();
        if (mPagedList != null) {
   
            mPagedList.removeWeakCallback(mPagedListCallback);
            mPagedList = null;
        } else if (mSnapshot != null) {
   
            mSnapshot = null;
        }
        // dispatch update callback after updating mPagedList/mSnapshot
        mUpdateCallback.onRemoved(0, removedCount);
        if (mListener != null) {
   
            mListener.onCurrentListChanged(null);
        }
        return;
    }

    ...
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

也就是在传入值为null的情况下,最终会执行

mUpdateCallback.onRemoved(0, removedCount);
  • 1

ListUpdateCallback是一个接口,其实现类如下:

/**
 * ListUpdateCallback that dispatches update events to the given adapter.
 *
 * @see DiffUtil.DiffResult#dispatchUpdatesTo(RecyclerView.Adapter)
 */
public final class AdapterListUpdateCallback implements ListUpdateCallback {
   
    @NonNull
    private 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} */
    @Override
    public void onInserted(int position, int count) {
   
        mAdapter.notifyItemRangeInserted(position, count);
    }

    /** {@inheritDoc} */
    @Override
    public void onRemoved(int position, int count) {
   
        mAdapter.notifyItemRangeRemoved(position, count);
    }

    /** {@inheritDoc} */
    @Override
    public void onMoved(int fromPosition, int toPosition) {
   
        mAdapter.notifyItemMoved(fromPosition, toPosition);
    }

    /** {@inheritDoc} */
    @Override
    public void onChanged(int position, int count, Object payload) {
   
        mAdapter.notifyItemRangeChanged(position, count, payload);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

也就是onRemoved(0, removedCount)还是调用了RecyclerView.AdapternotifyItemRangeRemoved(),因此,如果在submitList()传入null值,最终会让Adapter移除从位置0开始的getItemCount()个数据,即删除全部数据

请求第一页的数据

那么还有一个问题,就是加载第一页数据。Paging并没有根据页数请求数据的方法。即使接口是有提供这个方法,那么只能看DataSourceloadInitial()是在什么情况下被调用的了

在本项目中,DataSource继承自PageKeyedDataSource,查看在该类中loadInitial()被调用情况

@Override
final void dispatchLoadInitial(@Nullable Key key, int initialLoadSize, int pageSize,
                               boolean enablePlaceholders, @NonNull Executor mainThreadExecutor,
                               @NonNull PageResult.Receiver<Value> receiver) {
   
    LoadInitialCallbackImpl<Key, Value> callback =
        new LoadInitialCallbackImpl<>(this, enablePlaceholders, receiver);
    loadInitial(new LoadInitialParams<Key>(initialLoadSize, enablePlaceholders), callback);

    // If initialLoad's callback is not called within the body, we force any following calls
    // to post to the UI thread. This constructor may be run on a background thread, but
    // after constructor, mutation must happen on UI thread.
    callback.mCallbackHelper.setPostExecutor(mainThreadExecutor)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/286501
推荐阅读
相关标签
  

闽ICP备14008679号