gpt4 book ai didi

Android:RecyclerView onClick Listener 未获取

转载 作者:行者123 更新时间:2023-12-02 16:46:42 25 4
gpt4 key购买 nike

我有一个包含 600 个项目的数组。我列出了 RecyclerView 中的所有数组项并滚动良好。但是当我尝试访问 onClick 监听器时,它没有得到。我尝试实现 View.OnClickListener 并覆盖 onClick。但它不起作用。

当用户单击 View 中的行项目之一时,我想 Toast 列表项目中的项目

适配器代码是

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>  {

public static class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private String mItem;
private TextView mTextView;

public ViewHolder(View view) {
super(view);
view.setOnClickListener(this);
mTextView = (TextView) view.findViewById(R.id.item_title);
}

public void setItem(String item) {
mItem = item;
mTextView.setText(item);
}

@Override
public void onClick(View view) {
Log.d("TAG", "onClick " + getPosition() + " " + mItem);
}
}

private String[] mDataset;

public MyAdapter(String[] dataset) {
mDataset = dataset;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setItem(mDataset[position]);
}

@Override
public int getItemCount() {
return mDataset.length;
}

}

谁能帮帮我吗预先感谢:)

最佳答案

我已经使用您的 onClickListener 代码创建了一个示例项目,工作正常。就像当我单击项目时向我显示日志中单击的项目位置。

MainActivity.java

public class MainActivity extends AppCompatActivity {

private MyAdapter mAdapter;
private RecyclerView mRecyclerView;
private String[] dataSet = {"Waleed", "Sarwar", "Yousuf"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(dataSet);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(mAdapter);


}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private String[] mDataset;

public MyAdapter(String[] dataset) {
mDataset = dataset;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setItem(mDataset[position]);
}

@Override
public int getItemCount() {
return mDataset.length;
}

public static class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private String mItem;
private TextView mTextView;

public ViewHolder(View view) {
super(view);
view.setOnClickListener(this);
mTextView = (TextView) view.findViewById(R.id.item_title);
}

public void setItem(String item) {
mItem = item;
mTextView.setText(item);
}

@Override
public void onClick(View view) {
Log.d("TAG", "onClick " + getPosition() + " " + mItem);
}
}
}

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="56dp"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

如果您仍然遇到问题,请从下面的链接下载示例项目。

https://www.dropbox.com/s/xf3y8oxok0zlgxk/RecyclerViewOnClick.zip?dl=0

关于Android:RecyclerView onClick Listener 未获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33731588/

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