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
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With