gpt4 book ai didi

java - 无法解析方法 setOnChildClickListener

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:02:42 25 4
gpt4 key购买 nike

这是我的整个 Activity 代码,如果需要我也可以发布我的 xml。

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;
}

});

代码在没有 clickListener 的情况下也能完美运行,但我希望在单击数组的某个子项时发生操作,但现在它显示 无法解析方法 setOnChildClickListener(anonymous android. widget.ExpendableListView.OnChildClickListener) 虽然 recycleView 是我的 Adapter,但我们将不胜感激

最佳答案

简而言之,RecyclerView 不支持 OnChildClickListener,因为它不打算成为 ListView 的直接一对一替代品。

有关原因的很好解释,请查看 this stackoverflow answer

您应该改为在您的 ViewHolder 类中实现 View.OnClickListener,如下所示:

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
}
}

关于java - 无法解析方法 setOnChildClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32555707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com