androidandroid-recyclerviewonclicklistenerexpandablerecyclerview

How to implement onclicklistener on expandable recyclerview


I have a recycler view that was expanded as implemented here. I want to implement onClickListener on the children of the recycler view so that when i click the child, i will get a Toast message displaying the text that is contained in the child textview.

public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    final Item item = data.get(position);
    switch (item.type) {
        case HEADER:
            final MyViewHolder itemController = (MyViewHolder) holder;
            itemController.referalItem = item;
            itemController.alphabet.setText(item.text.toString());
            if (item.invisibleChildren == null) {                   itemController.btn_expand_toggle.setImageResource(R.mipmap.knob_add);
            } else {                   itemController.btn_expand_toggle.setImageResource(R.mipmap.knob_remove);
            }
            itemController.btn_expand_toggle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (item.invisibleChildren == null) {
                        item.invisibleChildren = new ArrayList<Item>();
                        int count = 0;
                        int pos = data.indexOf(itemController.referalItem);
                        while (data.size() > (pos + 1) && data.get(pos + 1).type == CHILD) {
                            item.invisibleChildren.add(data.remove(pos + 1));
                            count++;
                        }
                        notifyItemRangeRemoved(pos + 1, count);
                        itemController.btn_expand_toggle.setImageResource(R.mipmap.knob_add);
                    } else {
                        int pos = data.indexOf(itemController.referalItem);
                        int index = pos + 1;
                        for (Item i : item.invisibleChildren) {
                            data.add(index, i);
                            index++;
                        }
                        notifyItemRangeInserted(pos + 1, index - pos - 1);                           
    itemController.btn_expand_toggle.setImageResource(R.mipmap.knob_remove);
                        item.invisibleChildren = null;
                    }
                }
            });
            break;
        case CHILD:
            final TextView itemTextView = (TextView) holder.itemView;

            try {
                itemTextView.setText(data.get(position).text.toString());

                itemTextView.setOnClickListener(new View.OnClickListener() {  //setting the onclicklistener
                    @Override
                    public void onClick(View v) throws NullPointerException {
                        Toast.makeText(context, itemTextView.getText().toString(), Toast.LENGTH_SHORT).show(); //Gives nullpointer error
                    }
                });
            } catch (NullPointerException e){

            }
break;

But i get NullPointerException whenever i click on the child in the recyclerview.Any help will be appreciated. Thank you.


Solution

  • modify adapter code

    public class ExpandableListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
    //-----add
    interface OnItemClickListener{
        void OnItemClick(int position);
    }
    OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    //------
    
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final Item item = data.get(position);
        switch (item.type) {
            case HEADER:
                final ListHeaderViewHolder itemController = (ListHeaderViewHolder) holder;
                itemController.refferalItem = item;
                itemController.header_title.setText(item.text);
                if (item.invisibleChildren == null) {
                    itemController.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
                } else {
                    itemController.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
                }
                itemController.btn_expand_toggle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (item.invisibleChildren == null) {
                            item.invisibleChildren = new ArrayList<Item>();
                            int count = 0;
                            int pos = data.indexOf(itemController.refferalItem);
                            while (data.size() > pos + 1 && data.get(pos + 1).type == CHILD) {
                                item.invisibleChildren.add(data.remove(pos + 1));
                                count++;
                            }
                            notifyItemRangeRemoved(pos + 1, count);
                            itemController.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
                        } else {
                            int pos = data.indexOf(itemController.refferalItem);
                            int index = pos + 1;
                            for (Item i : item.invisibleChildren) {
                                data.add(index, i);
                                index++;
                            }
                            notifyItemRangeInserted(pos + 1, index - pos - 1);
                            itemController.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
                            item.invisibleChildren = null;
                        }
                    }
                });
                break;
            case CHILD:
                TextView itemTextView = (TextView) holder.itemView;
                itemTextView.setText(data.get(position).text);
                //-----add
                itemTextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(onItemClickListener!=null){
                            onItemClickListener.OnItemClick(position);
                        }
                    }
                });
                //------
                break;
        }
    }
    

    use like

        ExpandableListAdapter adapter = new ExpandableListAdapter(data);
        recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new ExpandableListAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(int position) {
                Toast.makeText(MainActivity.this, data.get(position).text,Toast.LENGTH_SHORT).show();
            }
        });