-
Notifications
You must be signed in to change notification settings - Fork 183
Migration to 2.5.0
Thank you @TheJuki for it!
In this version, your model needed to implement ItemModel and override getType().
public class MyModel implements ItemModel {
private String text;
public MyModel(String text)
{
this.text = text;
}
@Override
public int getType() {
return 1;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
In this version, your model needs to implement ViewModel. Provide an Id for optional ViewState handling.
public class MyModel implements ViewModel {
private int id;
private String text;
public MyModel(int id, String text)
{
this.id = id;
this.text = text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
In this version, you needed to extend RecyclerView.ViewHolder to hold your view.
public class MyViewHolder extends RecyclerView.ViewHolder {
public AppCompatTextView myTextView;
public MyViewHolder(View v) {
super(v);
myTextView = v.findViewById(R.id.myTextView);
}
public static MyViewHolder createInstance(View v) {
return new MyViewHolder(v);
}
}
In this version, you needed to extend ViewRenderer<MyModel, MyViewHolder> to bind your view.
public class MyViewRenderer extends ViewRenderer<MyModel, MyViewHolder> {
public MyViewRenderer(int type, Context context) {
super(type, context);
}
@Override
public void bindView(MyModel model, MyViewHolder holder) {
holder.myTextView.setText(model.getText());
}
@NonNull
@Override
public MyViewHolder createViewHolder(@Nullable ViewGroup parent) {
return MyViewHolder.createInstance(inflate(R.layout.element_text, parent));
}
}
See Quick Start with ViewState for information on how to setup a ViewState Provider.
In this version, you need to create new instance of ViewBinder and provide the view, model class, and an optional ViewState Provider. Note that context is no longer needed here.
public class MyViewBinder {
public ViewBinder getMyBinder() {
return new ViewBinder<>(
R.layout.element_text,
MyModel.class,
(model, finder, payloads) -> {
// AppCompatTextView myTextView = finder.find(R.id.myTextView);
finder.setText(R.id.myTextView, model.getText());
},
new MyViewStateProvider()
);
}
}
In this version, you needed to register your View Renderers.
public class MainActivity extends AppCompatActivity {
public RendererRecyclerViewAdapter adapter;
private List<MyModel> items = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.adapter = new RendererRecyclerViewAdapter();
this.adapter.registerRenderer(new MyViewRenderer(1, this));
final RecyclerView recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
linearLayoutManager.setStackFromEnd(false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
items.add(new MyModel("Test 1"));
items.add(new MyModel("Test 2"));
this.adapter.setItems(this.items);
this.adapter.notifyDataSetChanged();
}
}
In this version, you need to pass context to new RendererRecyclerViewAdapter() and register your View Binders.
public class MainActivity extends AppCompatActivity {
public RendererRecyclerViewAdapter adapter;
private List<MyModel> items = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.adapter = new RendererRecyclerViewAdapter(this /* context */);
this.adapter.registerRenderer(new MyViewBinder().getMyBinder());
final RecyclerView recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
linearLayoutManager.setStackFromEnd(false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
items.add(new MyModel(1, "Test 1"));
items.add(new MyModel(2, "Test 2"));
this.adapter.setItems(this.items);
this.adapter.notifyDataSetChanged();
}
}