zETO
zETO

Reputation: 191

Cannot call selected child RecycleView

I have created an Expandable View with RecycleView not ListView in android. I had some problems at first but now everything works fine. So now I am trying to select from the group list a child and make some action with the clickListener. I have tried OnChildClickListener but that does not work with the RecycleView. The only piece of code that works for me is this :

            textviewitem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("child", "works");

                }
            });

While textviewitem is: Context context = parent.getContext(); final TextView textviewitem= new TextView(context);

Now the thing is even though that works I have no way of telling which child was clicked and that's a problem. By the way I have searched a lot and seen many relevant links but I could not find my answer. In case you want me to post my whole ViewHolder please say so.

edit:

Here is my whole ViewHolder:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type){
    View view = null;
    Context context = parent.getContext();
    float dp = context.getResources().getDisplayMetrics().density;
    int subItemPaddingLeft = (int) (18 * dp);
    int subItemPaddingTopAndBottom = (int) (5 * dp);

    switch (type) {
        case HEADER:
            LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_header, parent, false);
            ListHeaderViewHolder header = new ListHeaderViewHolder(view);
            return header;
        case CHILD:
            final TextView textviewitem = new TextView(context);
            textviewitem.setPadding(subItemPaddingLeft, subItemPaddingTopAndBottom, 0, subItemPaddingTopAndBottom);
            textviewitem.setTextColor(0x88000000);
            textviewitem.setLayoutParams(
                    new ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT));

            textviewitem.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Log.d("child", "works");

                }
            });

            return new RecyclerView.ViewHolder(textviewitem) {
            };
    }
    return null;

}

Upvotes: 0

Views: 120

Answers (1)

akodiakson
akodiakson

Reputation: 779

You could set the click listener in onBindViewHolder, or you can set a tag on that TextView based on whatever unique thing that data item has (i.e., an index number, ID, etc.).

In onBindViewHolder, you could do: viewHolder.itemView.setTag(myKey)

Upvotes: 1

Related Questions