5

Here is my whole activity code, if nessery I can post my xml as well.

public class Main_Activityextends AppCompatActivity {
private RecyclerView recyclerview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
    recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    List<ExpandableListViewAdapter.Item> data = new ArrayList<>();

    data.add(new ExpandableListViewAdapter.Item(ExpandableListViewAdapter.HEADER, "Terms of usage"));
    data.add(new ExpandableListViewAdapter.Item(ExpandableListViewAdapter.CHILD, "never eat"));
    data.add(new ExpandableListViewAdapter.Item(ExpandableListViewAdapter.CHILD, "bluhhhhh"));

    recyclerview.setAdapter(new ExpandableListViewAdapter(data));

     recyclerview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            return true;
        }

    });

The code works perfectly without the clickListener, but I want an action to happen when clicking on a certain child of the array but right now it says Cannot resolve method setOnChildClickListener(anonymous android.widget.ExpendableListView.OnChildClickListener) although The recycleView is my Adapter, any help will be appreciated

We're All Mad Here
  • 1,544
  • 3
  • 18
  • 46

1 Answers1

2

Put simply, RecyclerView does not support an OnChildClickListener because it is not intended to be a direct one for one replacement for a ListView.

For a very good explanation of why, check out this stackoverflow answer

You should instead implement View.OnClickListener in your ViewHolder class like so:

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    // Your view fields here

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);

        // Assign view fields

        itemLayoutView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // Do your thing here
    }
}
Community
  • 1
  • 1
Kane O'Riley
  • 2,482
  • 2
  • 18
  • 27
  • I am sorry to ask this so late but with your class now how can I call a specific child, because I have not found any way – We're All Mad Here Sep 14 '15 at 12:08
  • Don't know what you mean by "call a specific child", but you can use `getAdapterPosition()` to find the index of the view that has been clicked on. – Kane O'Riley Sep 14 '15 at 12:13