gpt4 book ai didi

java - 在 RecyclerView 中仅选择一张图像

转载 作者:行者123 更新时间:2023-12-01 19:44:13 28 4
gpt4 key购买 nike

我有一个RecyclerView.Adapter,其中有一些数组。

带有字符串的 ArrayList 和带有整数的 ArrayList。字符串就像 url,整数就是照片。

首次打开应用程序时,将选择第一个项目。我有另一种单击方法,可以使另一个项目成为选定状态,并且该方法有效,但问题是第一个项目保持选定状态,因此对于每个图像单击作为选定状态,我只想选择一个项目并采用颜色。

这是我的代码。

RecyclerView的适配器

public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder>{
private int selectedItem;

private ArrayList<Integer> mImages = new ArrayList<>();
private ArrayList<String> mSearchUrl = new ArrayList<>();
private Context mContext;

public ListViewAdapter(ArrayList<Integer> images, ArrayList<String> SearchUrl, Context context) {
mImages = images;
mContext = context;
mSearchUrl = SearchUrl;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.s_engine_item, viewGroup, false);

return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
selectedItem = 0;
if (selectedItem == i) {
viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
}
Glide.with(mContext).load(mImages.get(i))
.into(viewHolder.image);
viewHolder.searchUrl.setText(mSearchUrl.get(i));

viewHolder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
selectedItem = i;

}
});

}

@Override
public int getItemCount() {
return mImages.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{

ImageView image;
TextView searchUrl;

public ViewHolder(@NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.ivEngine);
searchUrl = itemView.findViewById(R.id.ivEngineText);
}
}
}

这是 MainActivity.class

public void intSearch() {
mImages.add(R.drawable.s_bing);
mSearchUrl.add("https://www.bing.com/search?q=");

mImages.add(R.drawable.s_google);
mSearchUrl.add("https://www.google.com/search?q=");

mImages.add(R.drawable.s_yahoo);
mSearchUrl.add("www.yahoo.com");

mImages.add(R.drawable.amazon_white256);
mSearchUrl.add("www.amazon.com");

mImages.add(R.drawable.amazon_white256);
mSearchUrl.add("www.amazon.com");

mImages.add(R.drawable.amazon_white256);
mSearchUrl.add("www.amazon.com");

initRecyclerView();
}
private void initRecyclerView() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = findViewById(R.id.lvEngines);
recyclerView.setLayoutManager(layoutManager);
ListViewAdapter adapter = new ListViewAdapter(mImages, mSearchUrl, this);
recyclerView.setAdapter(adapter);
}

最佳答案

全局初始化您选择的项目

public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder>{
private int selectedItem = 0;
.....

然后,每当您单击新图像时,就会在 onBindViewHolder 内通知您的适配器最后选定的项目单元格中的更改。

   viewHolder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int previousSelectedItem = selectedItem;
selectedItem = i;
notifyItemChanged(previousSelectedItem);

viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
}
});

关于java - 在 RecyclerView 中仅选择一张图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54075260/

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