Select only one image in RecyclerView

时间:2019-01-07 13:22:13

标签: java android android-recyclerview

I have a RecyclerView.Adapter which has some Arrays there.

ArrayList with Strings and ArrayList with Integer. Strings are like url and Integer is the photo.

When the app is open for first time the first item is selected. I have another method for click which makes another item as selected and this works, but the problem is that the first item stays as selected and so for every image click makes as selected, I want only one item to be selected and take a color.

This is my code.

Adapter of 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);
        }
    }
}

And this is the 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);
}

2 个答案:

答案 0 :(得分:2)

全局初始化所选项目

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"));      
            }
        });

答案 1 :(得分:1)

只需从onBindViewHolder删除此行

selectedItem = 0;

,然后在背景条件中添加一个else,例如:

if (selectedItem == i) {
    viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
}else{
    viewHolder.image.setBackgroundColor(“YOUR_DEFAULT_COLOR”);
}

并更新onClick:

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