简体   繁体   中英

Update ViewHolder Without Re-Populating RecyclerView

I am using the FirebaseRecyclerAdapter to populate views dynamically as data is added to Firebase.

However, I also want to modify the views that the RecyclerView populates as data changes in Firebase . 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. I am kind of lost how I would proceed, but here is my 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 . I want the counter in the lower right corner to update, but again I do not want to completely recreate/repopulate the 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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