简体   繁体   English

如何正确刷新我的回收商列表视图

[英]How to properly refresh my Recycler List View

I start using RecyclerView on my new school projects and RealmDB as data source. 我开始在新的学校项目上使用RecyclerView ,并将RealmDB用作数据源。 So far it is quite okay until i refresh the list view. 到目前为止,在我刷新列表视图之前还可以。 here is demo . 这是演示 There is black screen flashing happening because of notifyDataSetChanged() . 由于notifyDataSetChanged()notifyDataSetChanged()黑屏闪烁。 I can set white background instead of black if i want to make it less obvious. 如果要使其不那么明显,我可以将白色背景设置为黑色。

But what mechanism should i be using to totally eliminate that obvious screen flashing ? 但是我应该使用什么机制来完全消除明显的屏幕闪烁? I would be very much appreciate if i would obtain same result like here 如果我能获得像这里一样的结果,我将非常感谢

How do i refresh the list view so far ? 到目前为止,我如何刷新列表视图?

  • STEP 1. I called web api to received posts 步骤1.我调用了Web api来接收帖子
  • STEP 2. Obtain the result and update or add it to #realm 步骤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()); } }); 
  • STEP 3. Realm will notify the changes to adapter and the notify block will call notifyDataSetChanged() on recycler adapter. 第3 notifyDataSetChanged() 。领域将通知对适配器的更改,通知块将在回收站适配器上调用notifyDataSetChanged()

In My Adapter ( RecyclerView.Adapter ) 在我的适配器( RecyclerView.Adapter )中

this.data.addChangeListener(new RealmChangeListener<RealmResults<CommonResult>>() {
    @Override
    public void onChange(RealmResults<CommonResult> element) {
        notifyDataSetChanged();
    }
});

UPDATE I had changed notifyDataSetChanged() to notifyItemChanged(i, element) . 更新我已经将notifyDataSetChanged()更改为notifyItemChanged(i, element) The flashing is gone and animation become more smooth :-) 闪烁消失了,动画变得更加流畅了:-)

Guys thanks for the comments and I get it solved by eliminating the use of notifyDataSetChanged() . 伙计们感谢您的评论,我通过消除使用notifyDataSetChanged()解决了它。

Changed from 从改变

this.data.addChangeListener(new RealmChangeListener<RealmResults<CommonResult>>() {
    @Override
    public void onChange(RealmResults<CommonResult> element) {
        notifyDataSetChanged();
    }
});

to

 this.data.addChangeListener(new RealmChangeListener<RealmResults<CommonResult>>() {
            @Override
            public void onChange(RealmResults<CommonResult> element) {
                notifyItems(element);
                Timber.i("onChange() called with: element = [" + element + "]");
            }
        });


private void notifyItems(RealmResults<CommonResult> element){
        for (int i = 0; i < element.size(); i++) {
            notifyItemChanged(i, element.get(i));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM