[英]Passing changing variables to RecyclerView Adapter
I have a custom RecyclerView.Adapter
that is used to render the layout of my list. 我有一个自定义的RecyclerView.Adapter
,用于呈现列表的布局。 In the parent Activity I have two variables that can be changed from the UI. 在父活动中,我有两个可以从UI更改的变量。 These parameters are used to render part of the RecyclerView item. 这些参数用于呈现RecyclerView项的一部分。
I know I can pass some data via the Adapter constructor, but this is a one time thing. 我知道我可以通过Adapter构造函数传递一些数据,但这是一次性的事情。 How can I pass data to the Adapter and change it on the fly? 如何将数据传递到适配器并即时更改它?
使您的适配器实现自定义接口,在其中定义用于传递/获取数据的方法。
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()
: 您可以保存到您的适配器参考您的Activity
或铸铁的结果recyclerView.getAdapter()
mAdapter = new YourAdapter();
mRecyclerView.setAdapter(mAdapter);
...
mAdapter.yourCustomMethod(data);
or 要么
YourAdapter adapter = (YourAdapter) recyclerView.getAdapter();
...
adapter.yourCustomMethod(data);
Use DataBinding
. 使用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. 使用DataBinding
您可以将类中的字段标记为@Bindable
,然后调用notifyPropertyChanged(BR.field_name)
来更新该属性的显示方式。 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). 通过这种方式,您可以简单地遍历Test
对象的List
并调用setTestString
来更新所有视图(仅更新相关的TextView
)。 Here is guide to how to begin with DataBinding 这是有关如何开始使用DataBinding的指南
In your RecyclerView.Adapter
you have method notifyItemChanged(int position, Object payload)
. 在您的RecyclerView.Adapter
您有方法notifyItemChanged(int position, Object payload)
。 Just call this method and pass your update as payload
parameter. 只需调用此方法并将更新作为payload
参数传递即可。 And then there is an onBindViewHolder(VH holder, int position, List<Object> payloads)
where you'll can update your view. 然后是一个onBindViewHolder(VH holder, int position, List<Object> payloads)
,您可以在其中更新视图。
那就是当您使用RecyclerView.Adapter的notifyDataSetChanged()方法(如docs此处所述 )通过您的UI 立即更新RecyclerView中的数据时。
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 在您的活动中存储适配器的全局引用,然后在需要时调用上面的函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.