I have created a RecyclerView with Cards as its primary viewtype. What I am trying to achieve is that when user clicks on a item the ViewType of that item is changed from cards to another Viewtype(for example a list textviews placed horizontally scrollable).
My code for RecyclerView Adapter is as follows :
In the below piece of code I have created a arrraylist of enum types to keep track of the clicked state of each row in recyclerview, then I got the size of my dataset and initialized my arraylist for each row with SHOW_PRIMARY_CONTENT
public class DisplayItemsAdapter extends RecyclerView.Adapter<DisplayItemsAdapter.ViewHolder> {
private static ArrayList<clickedState> itemClickedState;
private enum clickedState {
SHOW_PRIMARY_CONTENT,
SHOW_SECONDARY_CONTENT
}
private ArrayList<ItemData> mDataset = new ArrayList<>();
public DisplayItemsAdapter(ArrayList<ItemData> dataset) {
mDataset = dataset;
itemClickedState = new ArrayList<>();
for (int i = 0; i < mDataset.size(); i++) {
itemClickedState.add(i, clickedState.SHOW_PRIMARY_CONTENT);
}
}
Below is my ViewHolder Class which holds references to all my child views, It Implements View.OnClickListener. Why it implements View.OnClickListener is so that it can toggle between clicked states to change viewTypes accordingly.
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvItemName;
TextView tvItemNumber;
public ViewHolder(View itemView) {
super(itemView);
tvItemName = (TextView) itemView.findViewById(R.id.tvItemName);
tvItemNumber = (TextView) itemView.findViewById(R.id.tvItemNumber);
tvTicketClass = (TextView) itemView.findViewById(R.id.tvTicketClass);
}
@Override
public void onClick(View v) {
itemClickedState.add(getAdapterPosition(), clickedState.SHOW_SECONDARY_CONTENT);
}
}
This is where I am getting the value of viewType returned by getItemViewType and returning inflated layout
@Override
public DisplayItemsAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.display_Items_row, viewGroup, false);
View v2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.display_Items_ticket_class, viewGroup, false);
if (viewType == 0) {
return new ViewHolder(v1);
} else {
return new ViewHolder(v2);
}
}
Then here I m binding the views with values according to the viewType returned by the viewholderusing viewholder.getItemViewType
@Override
public void onBindViewHolder(DisplayItemsAdapter.ViewHolder viewHolder, final int position) {
if (viewHolder.getItemViewType() == 0) {
viewHolder.tvItemName.setText(mDataset.get(position).strItemName);
viewHolder.tvItemNumber.setText(mDataset.get(position).strItemNumber);
} else if (viewHolder.getItemViewType() == 1) {
for (int i = 0; i < mDataset.get(position).strClass.length; i++) {
viewHolder.tvTicketClass.setText(mDataset.get(position).strClass[i]);
}
}
}
@Override
public int getItemCount() {
return mDataset.size();
}
And finally my getItemViewType logic
@Override
public int getItemViewType(int position) {
if (itemClickedState.get(position) == clickedState.SHOW_SECONDARY_CONTENT)
return 1;
return 0;
}
}
What I could not understand is that why is not anything show when I run the code ,,,Everything seems valid to me . Help Me !!!!
see my example:
// RecycleItemObject.java
public class RecycleItemObject {
public static int LAYOUT_ITEM_PORTRAIN = 0;
public static int LAYOUT_ITEM_LANDSCAPE = 1;
private int type;
....................
/**
* @return get layout type of item
*/
public int getType() {
return type;
}
/**
* set layout type of item
* @param type
*/
public void setType(int type) {
this.type = type;
}
}
// adapter
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == RecycleItemObject.LAYOUT_ITEM_PORTRAIN) {
view = LayoutInflater.from(context).inflate(R.layout.item_recycle_view_main, parent, false);
} else {
view = LayoutInflater.from(context).inflate(R.layout.item_recycle_view_main_land, parent, false);
}
}
// event click item
@onClick....
curentObject.setType(RecycleItemObject.LAYOUT_ITEM_LANDSCAPE);
// notify item only
notifyItemChanged(itemPosition);
with me it work fine
Hope you have figured out your problem. If not then,I have done it f or you. The buggy line is
@Override
public void onClick(View v) {
itemClickedState.add(getAdapterPosition(), clickedState.SHOW_SECONDARY_CONTENT);
}
Here what you are doing is adding a new Item to
itemClickedState.add(getAdapterPosition(), clickedState.SHOW_SECONDARY_CONTENT);
This will insert a new item and size of mdataList and itemClickState differs and hence mis matching happens.
Use itemClickedState.set(getAdapterPosition(), clickedState.SHOW_SECONDARY_CONTENT);
instead. And second thing I does not understand what tvTicketClass is . If it is a single TextView, then you should concatenate the string first and them do tvTicketClass.settext(concatenatedstrClass)
. Hope you get helped from this post.If have any further issue , please do comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With