How do I load a different view type on click?

I am showing one list of transactions in recyclerview. Now I want to show another view on click of the parent layout of the first view type.

I created different layouts and different holders for both types.

I want to know how we can show the second view type onClick of the parent layout of the 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.

+3


source to share


1 answer


I think the problem is with your method like getItemView:

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 just need to change the type and then call notifyItemChanged, like so:

    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())
                } 

        }
    });

      

Note that I am using holder.getAdapterPosition

as recommended from google, you should not use position as final, but have a final holder and get a position like this in the listener

+1


source







All Articles