Ale*_*ber 1 android android-recyclerview fastadapter android-livedata
对于这个问题,我在 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;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:回收者视图闪烁一次 - 每次下载数据时。
即使前 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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我试图在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);
});
Run Code Online (Sandbox Code Playgroud)
我的问题的简短摘要:
我的真实应用程序FastAdapter出于多种原因使用,我正在寻找一种方法将它与DiffUtil “结合”,以消除通过 HTTPS 下载数据并在 Room 中更新数据时的一次性闪烁。
避免闪烁的一个简单方法是禁用RecyclerView's ItemAnimator。
mRecyclerView.setItemAnimator(null);
Run Code Online (Sandbox Code Playgroud)
在 FastAdapter 作者的帮助下,我已经能够通过向 RecyclerView 中显示的TopItem添加标识符来消除一次性闪烁:
@Override
public long getIdentifier() {
return uid;
}
Run Code Online (Sandbox Code Playgroud)
并通过在我的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);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1134 次 |
| 最近记录: |