gpt4 book ai didi

android - RecycleView 中的按钮在单击时没有反应

转载 作者:搜寻专家 更新时间:2023-11-01 08:27:26 26 4
gpt4 key购买 nike

我在 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/

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