Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve method setOnChildClickListener

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

like image 939
We're All Mad Here Avatar asked Oct 31 '22 19:10

We're All Mad Here


1 Answers

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
    }
}
like image 150
Kane O'Riley Avatar answered Nov 02 '22 10:11

Kane O'Riley