我在 LinearLayout
中创建了一个 RecycleView
组件,还有其他一些组件。问题是,每当我单击 RecycleView
组件中的元素时,它应该在控制台中创建一个 TAG
,但事实并非如此。我添加了 onClickListiner
但它根本没有反应。
下面是一段如何在 Activity
类和 Adapter
中创建 RecycleView
的代码。
Activity 类别:
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mAdapter = new MyAdapter(washLocations, this);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
适配器:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<WashLocation> washLocations;
private LayoutInflater layoutInflater;
public MyAdapter(List<WashLocation> washLocations, Context context) {
layoutInflater = LayoutInflater.from(context);
this.washLocations = washLocations;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.row_recycleview, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(context, contactView);
return viewHolder;
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
// Get the data model based on position
WashLocation w = washLocations.get(position);
String info = w.getWashName();
// Set item views based on your views and data model
TextView textView = holder.info;
textView.setText(info);
Integer fav = w.getFav();
Boolean favorite = fav == 1 ? true : false;
Button addToFav = holder.favorite;
addToFav.setText(favorite == true ? "Usuń z ulubionych" : "Dodaj do ulubionych");
}
@Override
public int getItemCount() {
return washLocations.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private Context context;
public TextView info;
public Button favorite;
public ViewHolder(Context context, View itemView) {
super(itemView);
itemView.setOnClickListener(this);
this.context = context;
info = (TextView) itemView.findViewById(R.id.textView);
favorite = (Button) itemView.findViewById(R.id.addToFav);
}
@Override
public void onClick(View v) {
int position = getAdapterPosition(); // gets item position
if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
// We can access the data within the views
Toast.makeText(context, info.getText(), Toast.LENGTH_SHORT).show();
Log.d("myTag", "This is my message");
}
}
}
}
最佳答案
我通常使用onBindViewHolder
来设置Listener
,因为在这个方法中我们有位置。尝试将您的方法更改为:
@覆盖
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
// Get the data model based on position
final WashLocation w = washLocations.get(position);
final String info = w.getWashName();
// Set item views based on your views and data model
TextView textView = holder.info;
textView.setText(info);
Integer fav = w.getFav();
Boolean favorite = fav == 1 ? true : false;
Button addToFav = holder.favorite;
addToFav.setText(favorite == true ? "Usuń z ulubionych" : "Dodaj do ulubionych");
holder.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//Here you have all the data that you want
//you can use w variable : w.getWashName();
}
});
}
为每个按钮添加 2 个不同的监听器:
holder.yourButton1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//Click for button 1
}
});
}
holder.yourButton2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//Click for button 2
}
});
}
关于android - RecycleView 内的按钮在单击时没有反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43457685/