仅单击的项目可见,并在RecyclerView中隐藏以前的项目

时间:2019-05-27 18:28:35

标签: java android android-recyclerview show-hide onitemclicklistener

我在ImageView中的项目中有RecyclerView。每次单击一个项目时,我都希望图像变得可见,并且上次单击的项目的图像不可见。

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

MyHolder类的item.setOnClick中,我将被单击的项目图像设置为可见,但是每次单击另一个项目时,新项目的图像将变为可见,而前一个项目的图像将变为可见。不可见。

2 个答案:

答案 0 :(得分:0)

您需要在适配器中使用另一个整数数组,以跟踪单击的项目。首先,用全1初始化整数数组。

// 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.

然后用全1初始化数组。尝试在适配器中具有这样的功能。

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

现在在您的onBindViewHolder中,根据ImageView数组中的值设置RecyclerViewselectedItems项目中的可见性。

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

onClickListener中,对selectedItems数组进行修改,以使只有选中的项的值为1,所有其他项的值为零。然后致电notifyDataSetChanged使更改生效。

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

希望有帮助!

答案 1 :(得分:0)

我的代码用于设置回收者视图项目的可见性,当您点击回收者视图项目时,它会在该特定项目上设置可见性,在其他项目上设置不可见性。

此代码适用于我设置点击项目的可见性和隐藏以前的图标。

初始化

     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();


       }
   });

我上传了基本想法的图片。

enter image description here