简体   繁体   English

更新 ViewHolder 而不重新填充 RecyclerView

[英]Update ViewHolder Without Re-Populating RecyclerView

I am using the FirebaseRecyclerAdapter to populate views dynamically as data is added to Firebase.我正在使用FirebaseRecyclerAdapter在数据添加到 Firebase 时动态填充视图。

However, I also want to modify the views that the RecyclerView populates as data changes in Firebase .但是,我还想修改RecyclerViewFirebase数据更改时填充的视图。 For instance, I have a counter on each view that is populated, and as people vote on that topic, I want the counter to increase:例如,我在每个填充的视图上都有一个计数器,当人们对该主题进行投票时,我希望计数器增加:

My idea is to set a tag to each view that is populated in the RecyclerView , find a way to get the current views that are on the screen, and then update those views dynamically.我的想法是为RecyclerView填充的每个视图设置一个标签,找到一种方法来获取屏幕上的当前视图,然后动态更新这些视图。 I am kind of lost how I would proceed, but here is my PollHolder:我有点不知所措,但这是我的 PollHolder:

    public static class PollHolder extends RecyclerView.ViewHolder {

    TextView mPollQuestion;
    TextView mVoteCount;
    ImageView mPollImage;
    View mView;
    String mTag;


    public PollHolder(View itemView) {
        super(itemView);

        mPollQuestion = (TextView) itemView.findViewById(R.id.latest_item_question);
        mPollImage = (ImageView) itemView.findViewById(R.id.pollThumbNailImage);
        mVoteCount = (TextView) itemView.findViewById(R.id.latest_item_poll_count);
        this.mView = itemView;
    }

    public void setVoteCount (String voteCount){
        mVoteCount.setText(voteCount);
    }

    public void setTag(String tag){
        this.mTag = tag;
    }

    public View getViewByTag(String tag){
        return mView;
    }

}

Here are the views in the RecyclerView .这是RecyclerView中的视图。 I want the counter in the lower right corner to update, but again I do not want to completely recreate/repopulate the RecyclerView .我希望右下角的计数器更新,但我又不想完全重新创建/重新填充RecyclerView

在此处输入图片说明

when you insert new get the position of that row.Then you can refresh specific row by using当您插入新获取该行的位置。然后您可以使用刷新特定行

notifyItemInserted (int position);

Its not refresh or recreate all data in RecyclerView but its alternate the count of list and refresh the particular position.它不是刷新或重新创建 RecyclerView 中的所有数据,而是交替列表计数并刷新特定位置。

Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.数据集中其他现有项目的表示仍然被认为是最新的,不会反弹,尽管它们的位置可能会改变。

More details refer RecyclerView.Adapter更多细节参考RecyclerView.Adapter

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

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