I am showing one list of transactions in recyclerview. I now want to show another view on click of the parent layout of first view type.
I have created different layouts and different holders for both view types.
I want to know how can we show the 2nd view type onClick of parent layout of first view type.
Here is my adapter:
public class TransactionHistoryListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Transaction> transactionList;
private Context mContext;
//static var
static final int TYPE_LOAD_TRANS = 0, TYPE_LOAD_TRANS_DETAIL = 1;
public TransactionHistoryListAdapter(List<Transaction> transactionsList, Context context) {
this.mContext = context;
this.transactionList = transactionsList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (viewType) {
case TYPE_LOAD_TRANS:
View v_header = inflater.inflate(R.layout.transaction_item_layout, parent, false);
viewHolder = new TransactionHistoryListHolder(v_header);
break;
case TYPE_LOAD_TRANS_DETAIL:
View v_header1 = inflater.inflate(R.layout.transaction_detail_layout, parent, false);
viewHolder = new TransactionDetailHolder(v_header1);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == TYPE_LOAD_TRANS) {
TransactionHistoryListHolder transsHolder = (TransactionHistoryListHolder) holder;
retriveAllTrans(transsHolder, position);
} else {
}
}
public void retriveAllTrans(final TransactionHistoryListHolder holder, final int position) {
final Transaction data = transactionList.get(position);
holder.txt_id.setText(data.getId());
holder.txt_date.setText(data.getDate());
holder.txt_trans_type.setText(data.getType());
holder.txt_balance.setText(data.getBalance());
holder.lay_row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getItemViewType(position);
}
});
}
@Override
public int getItemViewType(int position) {
Object obj = transactionList.get(position);
if (obj instanceof Transaction) {
return TYPE_LOAD_TRANS;
} else if (obj instanceof Transaction) {
return TYPE_LOAD_TRANS_DETAIL;
}
return super.getItemViewType(position);
}
@Override
public int getItemCount() {
return transactionList.size();
}
}
Please help. Thank you.
I think the problem is with your getItemView type method :
You should do it like this :
Transaction obj = transactionList.get(position);
if (obj.typeToDisp == 0) {
return TYPE_LOAD_TRANS;
} else if (obj.typeToDisp == 1) {
return TYPE_LOAD_TRANS_DETAIL;
}
return super.getItemViewType(position);
Then in your onClickListener, you will just have to change the type, and then call notifyItemChanged, like this :
holder.lay_row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int type = transaction.get(holder.getAdapterPosition()).typeToDisp;
if (type == 0)
{
transaction.get(holder.getAdapterPosition()).typeToDisp = 1;
notifyItemChanged(holder.getAdapterPosition())
}
}
});
Please note that I use holder.getAdapterPosition
, as recommanded from google, you should not use position as final, but have the holder final and get the position like this in a listener