【发布时间】:2016-08-18 04:10:24
【问题描述】:
我开始在我的新学校项目中使用RecyclerView 并使用RealmDB 作为数据源。到目前为止,在我刷新列表视图之前还可以。这里是demo。由于notifyDataSetChanged(),出现黑屏闪烁。如果我想让它不那么明显,我可以设置白色背景而不是黑色。
但是我应该使用什么机制来完全消除明显的屏幕闪烁?如果我能获得像 here 一样的结果,我将不胜感激
到目前为止,我如何刷新列表视图?
- 步骤 1. 我调用 web api 来接收帖子
-
步骤 2. 获取结果并更新或添加到#realm
mRealm.executeTransactionAsync(new Realm.Transaction() { @Override public void execute(Realm realm) { for (Post post : posts) { //create post realm.copyToRealmOrUpdate(post); //create common result row CommonResult cr = new CommonResult(); int id = module.hashCode() + post.getId(); cr.setId(id); cr.setPost(post); cr.setPostid(post.getId()); cr.setTag(module.hashCode()); realm.copyToRealmOrUpdate(cr); } } }, new Realm.Transaction.OnSuccess() { @Override public void onSuccess() { RealmResults<CommonResult> cr = mRealm.where(CommonResult.class) .equalTo("tag", module.hashCode()) .findAll(); mPostView.showListingView(cr, hasNext()); } }); 步骤 3. Realm 将通知适配器的更改,并且通知块将在回收器适配器上调用
notifyDataSetChanged()。
在我的适配器中 (RecyclerView.Adapter)
this.data.addChangeListener(new RealmChangeListener<RealmResults<CommonResult>>() {
@Override
public void onChange(RealmResults<CommonResult> element) {
notifyDataSetChanged();
}
});
更新
我已将notifyDataSetChanged() 更改为notifyItemChanged(i, element)。闪烁消失了,动画变得更加流畅:-)
【问题讨论】:
-
你如何以及在哪里调用监听器的 onChange(---) 方法
-
嗨@Rah,如果该表内部发生变化,RealmDB 会自动调用此方法。
-
您能否在 onChange() 方法中添加一些日志并检查日志触发/打印的次数,因为我认为您有这么多记录并且此方法被多次调用
-
notifyDataSetChanged 被
for (Post post : posts)中的每个帖子调用。最好在 onSuccess 块中手动调用 notifyDataSetChanged
标签: android performance android-recyclerview realm swiperefreshlayout