简体   繁体   中英

How to update/refresh a specific item of RecyclerView without calling notifiyItemChanged or DiffUtil?

I have a nested RecyclerViews and each row/item of this recyclerView is a recyclerView too,and there is an outer events will update an item in the inner recyclerView. My onBindViewHolder of the outer RecyclerView adapter look like that

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            GiftWrappingHorizontalViewHolder viewHolder = (GiftWrappingHorizontalViewHolder) holder;
            viewHolder.giftWrappingRecyclerView.setLayoutManager(new LinearLayoutManager(viewHolder.giftWrappingRecyclerView.getContext(),
                    RecyclerView.HORIZONTAL, false));
            viewHolder.topTextView.setText(dataList.get(position).getTopTextName());
            if(dataList.get(position).getType() == 1) {
                GiftWrappingHorizontalAdapter adapter = new GiftWrappingHorizontalAdapter(dataList.get(position)
                        .getItems(), position);
                viewHolder.giftWrappingRecyclerView.setAdapter(adapter);
                viewHolder.giftWrappingRecyclerView.setItemAnimator(null);
                adapter.setGiftWrappingHorizontalClickListener(giftWrappingHorizontalClickListener);
            } else if(dataList.get(position).getType() == 0){
                GiftWrappingAddOnsAdapter adapter = new GiftWrappingAddOnsAdapter(dataList.get(position)
                        .getItems(), position);
                viewHolder.giftWrappingRecyclerView.setAdapter(adapter);
                viewHolder.giftWrappingRecyclerView.setItemAnimator(null);
                adapter.setGiftWrappingAddOnEventsListener(giftWrappingAddOnEventsListener);
            }

    }

So as you can see I'm binding the inner recyclerViews to its adapters inside the onBindViewHolder and I want to update a specific item of the inner recyclerView without calling notifiyItemChanged because this will recreate the inner adapters which is paginated and it will call for the first page only because the inner RecyclerViews are using HORIZONTAL LinearLayoutManager.

Is there any to achieve that by accessing an item of the recyclerView by its position and call notifiyItemChanged to the inner RecyclerView?

You can try with notifyItemChanged()

notifyItemChanged(int position, Object payload)

Notify any registered observers that the item at position has changed with an optional payload object.

LOGIC

arrayList.set(updatePosition, yourValue);
adapterOBJ.notifyItemChanged(updatePosition);

LiveData

Fun part is you can use it directly in layout

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable name="myTextLiveData" type="String" />
    </data>

    <TextView
    ...
      android:text="@{myTextLiveData}
    />

</layout>

In adapter onBind() you can set livedata of the object and wherever you update livedata.setValue("new value"), it'll reflect immediately.

I strictly assume you're updating data portion only. If your data is going to modify order or list size then better stick to notifyItemChanged()

You could also consider DiffUtil

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