2

对于这个问题,我在 Github 准备了一个简单且有效的示例

应用截图

我的示例应用程序使用 okhttp 下载了一个包含游戏中前 30 名玩家的 JSON 数组,并将它们存储到 SQLite Room 中。在片段中,我观察了相应的LiveData<List<TopEntity>>对象并更新了 FastAdapter 实例:

public class TopFragment extends Fragment {
    private final ItemAdapter<TopItem> mItemAdapter = new ItemAdapter<>();
    private final FastAdapter<TopItem> mFastAdapter = FastAdapter.with(mItemAdapter);

    private TopViewModel mViewModel;
    private ProgressBar mProgressBar;
    private RecyclerView mRecyclerView;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {

        mViewModel = ViewModelProviders.of(this).get(TopViewModel.class);
        mViewModel.getTops().observe(this, tops -> {
            mItemAdapter.clear();
            for (TopEntity top: tops) {
                TopItem item = new TopItem(top);
                mItemAdapter.add(item);
            }
        });

        View v = inflater.inflate(R.layout.top_fragment, container, false);
        mProgressBar = v.findViewById(R.id.progressBar);
        mRecyclerView = v.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(mFastAdapter);

        fetchJsonData();

        return v;
    }

我的问题是:每次下载数据时,回收站视图都会闪烁一次。

尽管前 30 名并不经常变化,即数据保持不变。

当我查看 Penz 先生出色的FastAdapter库中的示例应用程序时,我看不到那里有任何闪烁。

更新:

我有一个提示,闪烁显然是由调用方法引起mItemAdapter.clear();onChanged,我应该改用 DiffUtil 类。

所以我添加了一个新类:

public class DiffCallback extends DiffUtil.Callback {
    private final List<TopItem> mOldList;
    private final List<TopItem> mNewList;

    public DiffCallback(List<TopItem> oldStudentList, List<TopItem> newStudentList) {
        this.mOldList = oldStudentList;
        this.mNewList = newStudentList;
    }

    @Override
    public int getOldListSize() {
        return mOldList.size();
    }

    @Override
    public int getNewListSize() {
        return mNewList.size();
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        TopItem oldItem = mOldList.get(oldItemPosition);
        TopItem newItem = mNewList.get(newItemPosition);

        return oldItem.uid == newItem.uid;
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        TopItem oldItem = mOldList.get(oldItemPosition);
        TopItem newItem = mNewList.get(newItemPosition);

        return oldItem.elo == newItem.elo &&
                oldItem.given.equals(newItem.given) &&
                //oldItem.photo != null && oldItem.photo.equals(newItem.photo) &&
                oldItem.avg_time != null && oldItem.avg_time.equals(newItem.avg_time) &&
                oldItem.avg_score == newItem.avg_score;
    }

    @Nullable
    @Override
    public Object getChangePayload(int oldItemPosition, int newItemPosition) {
        return super.getChangePayload(oldItemPosition, newItemPosition);
    }
}

然后我试图在onChanged方法中使用它而不是仅仅调用mItemAdapter.clear()但是 RecyclerView 保持为空,即使tops有元素:

mViewModel = ViewModelProviders.of(this).get(TopViewModel.class);
mViewModel.getTops().observe(this, tops -> {
    List<TopItem> oldList = mItemAdapter.getAdapterItems();
    List<TopItem> newList = new ArrayList<>();
    for (TopEntity top: tops) {
        TopItem item = new TopItem(top);
        newList.add(item);
    }

    DiffCallback diffCallback = new DiffCallback(oldList, newList);
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

    mItemAdapter.getAdapterItems().clear();
    mItemAdapter.getAdapterItems().addAll(newList);
    diffResult.dispatchUpdatesTo(mFastAdapter);
});

我的问题的简短摘要:

我的真实应用程序使用FastAdapter有几个原因,我正在寻找一种方法将其与DiffUtil “结合” ,以消除通过 HTTPS 下载数据并在 Room 中更新数据时的一次性闪烁。

4

3 回答 3

5

避免闪烁的一个简单方法是禁用RecyclerView's ItemAnimator

mRecyclerView.setItemAnimator(null);
于 2018-11-02T12:22:09.320 回答
4

在 FastAdapter 作者的帮助下,我能够通过在 RecyclerView 中显示的TopItem添加一个标识符来消除一次性闪烁:

@Override
public long getIdentifier() {
    return uid;
}

通过在我的 TopFragment 中使用FastAdapterDiffUtil

mViewModel = ViewModelProviders.of(this).get(TopViewModel.class);
mViewModel.getTops().observe(this, tops -> {
    List<TopItem> newList = new ArrayList<>();
    for (TopEntity top: tops) {
        TopItem item = new TopItem(top);
        newList.add(item);
    }

    DiffUtil.DiffResult diffResult = FastAdapterDiffUtil.calculateDiff(mItemAdapter, newList);
    FastAdapterDiffUtil.set(mItemAdapter, diffResult);
});
于 2018-11-02T12:40:55.727 回答
1

尝试从 getChangePayload() 方法返回一些标记对象。

@Nullable
@Override
public Object getChangePayload(final int oldItemPosition, final int newItemPosition) {
    return mNewList.get(newItemPosition);
}
于 2018-11-02T11:21:36.713 回答