I have a RecyclerView with different Views in each item: different number of views, different type and different positions. They come from a database.
Item 1
View type A, View type B, View type E
Item 2
View type B, View type B, View type J, View type C
. . .
Item n
View type F, View type S
A is for example a TextView, B for example a CheckBox ...
In the Holder constructor I get the Layout of the item:
public ViewHolderXXX(final View itemView) {
super(itemView);
mainLayout = itemView.findViewById(R.id.ly_main_layout);
}
Then, in the bindViews (), depending on the type of the view, I add another layout to the mainLayout and add the corresponding logic.
if(customObject == TextView){
mainLayout.addView(layoutForTextView);
}
if(customObject == CheckBox){
mainLayout.addView(layoutForCheckBox);
}
.
.
.
TextView textView = layoutForTextView.findViewById(R.id.tv_text_view);
textView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
final Intent intent = new Intent(context, OtherActivity.class);
context.startActivity(intent);
}
}
Also, in the Holder I have a static class that is called from OtherActivity.
public static void notifyXXX(){
//do something
}
In OtherActivity:
ViewHolderXXX.notifyXXX();
finish();
When I return to the Holder the items do "weird things". In the last item there are layouts that I have not added. I can not find the pattern with which they appear.
I'm pretty confused. I do not know if I designed Adapter and the ViewHolder well or if a RecyclerView is not suitable for this particular task. Also, I have had to solve other quite complicated problems.