MikkoP
MikkoP

Reputation: 5092

Passing changing variables to RecyclerView Adapter

I have a custom RecyclerView.Adapter that is used to render the layout of my list. In the parent Activity I have two variables that can be changed from the UI. These parameters are used to render part of the RecyclerView item.

I know I can pass some data via the Adapter constructor, but this is a one time thing. How can I pass data to the Adapter and change it on the fly?

Upvotes: 2

Views: 3353

Answers (5)

Brian Yencho
Brian Yencho

Reputation: 2958

You can always add any public methods you want to your adapter and call them directly. You can either save a reference to your adapter in your Activity or cast the result of recyclerView.getAdapter():

mAdapter = new YourAdapter();
mRecyclerView.setAdapter(mAdapter);
...
mAdapter.yourCustomMethod(data);

or

YourAdapter adapter = (YourAdapter) recyclerView.getAdapter();
...
adapter.yourCustomMethod(data);

Upvotes: 1

Jameido
Jameido

Reputation: 1354

Inside your adapter you can add a method like this:

public void setPropX(yourType propxVal){
    mPropX = propXVal; //Set the value of you property
    notifyDataSetChanged(); //Redraw all the elements
}

In your activity store a global reference of the adapter then call the function above when needed

Upvotes: 0

 Ekalips
Ekalips

Reputation: 1491

Best approach

Use DataBinding. With DataBinding you can mark fields in your class as @Bindable and then call notifyPropertyChanged(BR.field_name) to update diplaying of just that property. Here is little tutorial:

class Test extends BaseObservable{
@Bindable
String testString;

...

    public void setTestString(String newStr){
        this.testString = newStr;
        notifyPropertyChanged(BR.testString);
    }
...
}

And in your layout

<layout
 xmlns:android="http://schemas.android.com/apk/res/android">

<data>
    <variable
        name="test"
        type="com.yout.package.name.Test"/>
</data>
<FrameLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="@{test.testString}"/>
</FrameLayout>
</layout>

By this way you can simply go through your List of Test objects and call setTestString to update all views (only related TextView will be updated). Here is guide to how to begin with DataBinding

Decent approach

In your RecyclerView.Adapter you have method notifyItemChanged(int position, Object payload). Just call this method and pass your update as payload parameter. And then there is an onBindViewHolder(VH holder, int position, List<Object> payloads) where you'll can update your view.

Upvotes: 3

DaveNOTDavid
DaveNOTDavid

Reputation: 1803

That's when you use RecyclerView.Adapter's notifyDataSetChanged() method (as described here in the docs) to instantly update the data in the RecyclerView via your UI.

Upvotes: 0

khusrav
khusrav

Reputation: 5307

Make your adapter implement a custom interface, define methods there for passing/getting data.

Upvotes: 1

Related Questions