简体   繁体   中英

Only clicked item visible and hide the pervious items in RecyclerView

I have ImageView inside of an item in a RecyclerView . Every time I click on an item, I want the image to become visible and the previous clicked item's image becomes invisible.

public static class MyHolder extends RecyclerView.ViewHolder {

    private ImageView image_view;
    private TextView text_view_name;
    private TextView text_view_abilities;
    private ImageView heart_image_view;

    public MyHolder(@NonNull final View itemView, final OnItemClickListener listener) {
        super(itemView);

        image_view = itemView.findViewById(R.id.image_view);
        text_view_name = itemView.findViewById(R.id.text_view_name);
        text_view_abilities = itemView.findViewById(R.id.text_view_abilities);
        heart_image_view = itemView.findViewById(R.id.heart_image_view);
        heart_image_view.setImageResource(R.drawable.heart);
        heart_image_view.setVisibility(View.GONE);

        itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (listener != null) {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION) {
                    listener.onItemClick(position);
                    heart_image_view.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}

@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_item_layout, viewGroup, false);
    MyHolder holder = new MyHolder(v, mlistener);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int position) {

    Item item = myList.get(position);
    myHolder.text_view_name.setText(item.getTitle());

    stringArray = item.getAbilitiesObj();
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < stringArray.size(); i++) {
        try {
            builder.append(stringArray.get(i));
            builder.append(", ");

        } catch (NumberFormatException ex) {
            ex.printStackTrace();
        }
        myHolder.text_view_abilities.setText(builder);
    }
    if (myHolder.image_view != null) {
        String photoUrl = item.getImageUrl();
        Picasso.with(mContext).load(photoUrl).into(myHolder.image_view);
    }
}

In MyHolder class where item.setOnClick , I set the clicked item image visible but then every time I click on another item, the image of the new item becomes visible and the image of the previous item does not get invisible.

You need to have another array of integers in your adapter to keep track of the items which is clicked. At first, initialize the array of integers with all ones.

// Define an array like the following in your adapter 
private int[] selectedItems = new int[yourDataList.size()]; // Initialize the array to have the same size as your data list.

Then initialize the array with all ones. Try having a function like this in your adapter.

private void initializeSeledtedItems() {
    for(int item : selectedItems) item = 1;
} 

Now in your onBindViewHolder , set the visibility of the ImageView in your RecyclerView items based on the value found in the selectedItems array.

if(selectedItems[position] == 1) heart_image_view.setVisibility(View.VISIBLE);
else heart_image_view.setVisibility(View.GONE);

In the onClickListener modify the selectedItems array as such that, only the item selected has the value one and all the others have zeros. Then call notifyDataSetChanged to take your changes into effect.

private void setSelectedItem(int position) {
    for(int i = 0; i < selectedItems.length(); i++) {
        if(i == position) selectedItems[i] = 1;
        else selectedItems[i] = 0;
    }
} 

itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        if (listener != null) {
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {
                setSelectedItem(position);
                notifyDataSetChanged();
            }
        }
    }
});

Hope that helps!

My code is for setting Visibility on recycler view's item , when you click on recycler views item it set VISIBLE on that particular item and INVISIBLE on other items.

this code works for me for setting visibility on clicked item and hiding previous icon.

initialize

     int selecteditem =0;

onBindViewHolder

   if(selecteditem==position){
        holder.icSelect.setVisibility(View.VISIBLE);
    }
    else{
        holder.icSelect.setVisibility(View.INVISIBLE);

    }



     holder.itemView.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           selecteditem=position;
           holder.icselect.setVisibility(View.VISIBLE);
           notifyDataSetChanged();


       }
   });

i uploaded image for basic idea .

enter image description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM