java - How to load different view type on click of view? -
i showing 1 list of transactions in recyclerview. want show view on click of parent layout of first view type.
i have created different layouts , different holders both view types.
i want know how can show 2nd view type onclick of parent layout of first view type.
here 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 problem getitemview type method :
you should :
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 onclicklistener, have change type, , call notifyitemchanged, :
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 use holder.getadapterposition
, recommanded google, should not use position final, have holder final , position in listener
Comments
Post a Comment