从LiveData加载后,即使数据保持不变,FastAdapter也会闪烁 [英] FastAdapter flickers after loading from LiveData even though the data stays same
问题描述
针对这个问题,我已经在Github上准备了一个简单而有效的示例:
For the question I have prepared a simple and working example at Github:
我的示例应用程序使用okhttp下载了一个包含游戏中前30名玩家的JSON数组,并将它们存储到SQLite Room中.在片段我观察到相应的LiveData<List<TopEntity>>
对象并更新FastAdapter实例:
My example app downloads a JSON array containing top 30 players in a game using okhttp and stores them into SQLite Room. In the fragment I observe the corresponding LiveData<List<TopEntity>>
object and update the FastAdapter instance:
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;
}
我的问题是:每次下载数据时,回收站视图都会闪烁一次.
My problem is: the recycler view flickers once - every time the data is downloaded.
即使前30名的变动并不频繁,也就是数据保持不变.
Even though the top 30 does not change often, i.e. the data stays same.
当我查看Penz先生出色的 FastAdapter 库中的示例应用程序时,我会做在那里看不到任何闪烁.
When I look at the example app in the excellent FastAdapter library by Mr. Penz, I do not see any flickering there.
更新:
我有一个提示,该闪烁显然是由于调用mItemAdapter.clear();
在onChanged
方法中,我应该改用DiffUtil类.
I've got a hint that the flickering is obviously caused by calling mItemAdapter.clear();
in the onChanged
method and I should use DiffUtil class instead.
所以我添加了一个新类:
So I have added a new class:
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()
,但是即使tops
具有元素,RecyclerView仍为空:
And then I am trying to use it in the onChanged
method instead of just calling mItemAdapter.clear()
but the RecyclerView stays empty even though the tops
has elements:
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中更新数据时的一次闪烁.
My real app uses FastAdapter
for several reasons and I am looking for a way to "marry" it with DiffUtil in order to eliminate the one-time flickering when the data is downloaded over HTTPS and updated in Room.
推荐答案
在FastAdapter的帮助下作者通过将标识符添加到我的
With the help of FastAdapter author I have been able to eliminate the one-time flickering by adding an identifier to my TopItem being displayed in the RecyclerView:
@Override
public long getIdentifier() {
return uid;
}
,然后在我的 TopFragment :
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);
});
这篇关于从LiveData加载后,即使数据保持不变,FastAdapter也会闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!