我有一个RecyclerView和相应的适配器,在其中创建了OnItemClickListener接口。
我尝试将(onclicked)视图本身传递给Activity中的实现,它会获得多个项目。实际上,我要对某个视图项进行的更改会影响“回收者”视图列表中的多个项。我想更改列表中某个项目的背景,但是另一个项目的背景也发生了变化。
这是适配器:
public class TranslationAdapter extends RecyclerView.Adapter<TranslationAdapter.TranslationHolder> {
private List<String> translations = new ArrayList<>();
private OnItemClickListener listener;
@NonNull
@Override
public TranslationHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.translation_item, viewGroup, false);
return new TranslationHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull TranslationHolder translationHolder, int i) {
...
}
public void setTranslations(List<String> _translations){
this.translations = _translations;
notifyDataSetChanged();
}
class TranslationHolder extends RecyclerView.ViewHolder{
private TextView tvTranslation;
public TranslationHolder(@NonNull final View itemView) {
super(itemView);
tvTranslation = itemView.findViewById(R.id.text_view_translation);
itemView.setOnClickListener(v -> {
int position = getAdapterPosition();
if (listener != null && position != RecyclerView.NO_POSITION){
listener.onItemClick(v, translations.get(position));
}
});
}
}
public interface OnItemClickListener{
void onItemClick(View view, String string);
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
}
这是Activity中的实现:
public class TranslationActivity extends AppCompatActivity {
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_translation);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.setOnItemClickListener((v, translation) -> {
if (arrResultOfTranslation.contains(translation)){
arrResultOfTranslation.remove(translation);
v.setBackgroundColor(Color.BLUE);
}
else {
v.setBackgroundColor(Color.YELLOW);
arrResultOfTranslation.add(translation);
}
});
}
}
结果,尽管我只得到一个字符串项目(翻译),但多个项目的背景颜色却改变了。调试时,我只能看到一个View实例(变量v)。
最佳答案
尝试这个
RecyclerViewAct.java
public class RecyclerViewAct extends Activity {
private static final String TAG = "RecyclerViewAct";
private RecyclerView recyclerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler_main);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
MyAdapter adapter = new MyAdapter();
List<String> data = new ArrayList<>();
for (int i = 0; i < 30; i++) {
data.add("item" + i);
}
adapter.setData(data);
recyclerView.setAdapter(adapter);
adapter.setOnClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onItemClick(List<String> results) {
Log.e(TAG, "onItemClick: " + results.size());
}
});
}
}
MyAdapter.java
class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
private List<String> data = new ArrayList<>();
private List<String> results = new ArrayList<>();
public void setData(List<String> data) {
this.data = data;
notifyDataSetChanged();
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler, parent, false);
return new MyHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final MyHolder holder, final int position) {
holder.textView.setText(data.get(position));
if (results.contains(data.get(position))) {
holder.textView.setBackgroundColor(Color.BLUE);
} else {
holder.textView.setBackgroundColor(Color.WHITE);
}
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = data.get(position);
if (results.contains(data.get(position))) {
results.remove(str);
holder.textView.setBackgroundColor(Color.WHITE);
} else {
holder.textView.setBackgroundColor(Color.BLUE);
results.add(str);
}
// selected list
if (onClickListener != null) {
onClickListener.onItemClick(results);
}
}
});
}
@Override
public int getItemCount() {
return data.size();
}
OnItemClickListener onClickListener;
public interface OnItemClickListener {
void onItemClick(List<String> results);
}
public void setOnClickListener(OnItemClickListener onClickListener) {
this.onClickListener = onClickListener;
}
class MyHolder extends RecyclerView.ViewHolder {
TextView textView;
public MyHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_item);
}
}
}