4

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.

Sid
  • 2,792
  • 9
  • 55
  • 111
  • Isn't there a problem ? in your getItemViewTypê the if and the else if seem to be the same condition. – liltof Jul 28 '17 at 09:34
  • My object is same for the both view types.can I keep same object or not for both views?@liltof – Sid Jul 28 '17 at 09:35
  • Yes, but in your object you should have a property telling you what type of view should be displayed. then when you click on the parent view, just change the property value, and call notifyItemChanged, this will redraw the item and so change the view type displayed – liltof Jul 28 '17 at 09:37
  • can you please help me with the code?@liltof – Sid Jul 28 '17 at 09:40

1 Answers1

1

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

liltof
  • 2,153
  • 2
  • 14
  • 23
  • great it worked.. Now its getting show for all the items and many times, I want to show only once when clicked on item, if shown then hide it,and if item a is clicked and view type is shown then if next item is clicked it should hide from a and show to b. – Sid Jul 28 '17 at 10:00
  • I don't understand. If you just want to go back to the previous type when you click again, then you just have to, in your onClickListener, do : if (type == 1) {transaction.get(holder.getAdapterPosition()).typeToDisp = 0;} and then call notifyItemChanged again. If you want to remove it from the list, then if type == 1 remove it from the transactionlist object, and call notifyItemRemoved for the position. If this answer is ok for you, can you mark it as accepted ? – liltof Jul 28 '17 at 10:20
  • I noticed now with above code, its hiding view 1 and showing view 2 at its position, I want to show both. on click of view 1 it should show view 2 below view 1. – Sid Jul 28 '17 at 10:35
  • Oh! ok! then you have to insert a transaction after the clicked one in the transactionlist (the same transaction, but with type 1, and then call notifyItemInserted(holder.getAdapterPosition() + 1). Do this in your click listener and of course don't call transaction.get(holder.getAdapterPosition()).typeToDisp = 1; notifyItemChanged(holder.getAdapterPosition()) – liltof Jul 28 '17 at 10:39
  • It will get insert on every click. – Sid Jul 28 '17 at 10:41
  • Yes so just put another property in your transaction : a boolean isOpend and don't insert if it is opened :) – liltof Jul 28 '17 at 10:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150395/discussion-between-liltof-and-sid). – liltof Jul 28 '17 at 10:48