简体   繁体   English

在Android中长按一个项目时,如何更改所有RecyclerView项目的可见性?

[英]How to change the visibility of all RecyclerView items when a single item is long clicked in Android?

I'm making a multiple selection RecyclerView in Android in order to delete the selected items. 我要在Android中选择多个RecyclerView ,以便删除所选项目。

Checkboxes are initially invisible, the main logic is when a single view is long clicked all checkboxes are visible. 复选框最初是不可见的,主要逻辑是当长时间单击单个视图时,所有复选框都是可见的。

I'm performing to long click in onBindViewHolder and only the one clicked is visible. 我正在执行长按onBindViewHolder ,只有被单击的一个可见。

Here is my adapter : 这是我的适配器:

  private Context mContext ;
private ArrayList<Picture> mData ;
private ArrayList<Picture> mDataFiltered;

public CoffretPicturesAdapter(Context mContext, ArrayList<Picture> mData) {
    this.mContext = mContext;
    this.mData = mData;
    this.mDataFiltered = mData;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view ;
    LayoutInflater mInflater = LayoutInflater.from(mContext);
    view = mInflater.inflate(R.layout.cardview_item_coffret,parent,false);
    return new MyViewHolder(view);
}

@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            String charString = charSequence.toString();
            if (charString.isEmpty()) {
                mDataFiltered = mData;
            } else {
                ArrayList<Picture> filteredList = new ArrayList<>();
                for (Picture row : mData) {

                    // name match condition. this might differ depending on your requirement
                    // here we are looking for name or phone number match
                    if (row.getTitle().toLowerCase().contains(charString.toLowerCase())) {
                        filteredList.add(row);
                    }
                }

                mDataFiltered = filteredList;
            }

            FilterResults filterResults = new FilterResults();
            filterResults.values = mDataFiltered;
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            mDataFiltered = (ArrayList<Picture>) filterResults.values;
            notifyDataSetChanged();
        }
    };
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    holder.coffret_title.setText(mDataFiltered.get(position).getTitle());
    Picasso.with(mContext).load(mDataFiltered.get(position).getUri()).placeholder(R.drawable.placeholder).resize(500,500).centerCrop().into(holder.coffret_thumbnail);
    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mContext, "" + mDataFiltered.get(position).getTitle(), Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(mContext, SelectedPicPreviewActivity.class);
            intent.putExtra("image_url", mDataFiltered.get(position).getUri().toString());
            mContext.startActivity(intent);
        }
    });

    holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            holder.checkBox.setVisibility(View.VISIBLE);
            return true;
        }
    });
}

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

public static class MyViewHolder extends RecyclerView.ViewHolder {

    TextView coffret_title;
    ImageView coffret_thumbnail;
    LinearLayout cardView ;
    CircleCheckBox checkBox;

    public MyViewHolder(View itemView) {
        super(itemView);

        coffret_title = (TextView) itemView.findViewById(R.id.coffret_title_id) ;
        coffret_thumbnail = (ImageView) itemView.findViewById(R.id.coffret_img_id);
        cardView = (LinearLayout) itemView.findViewById(R.id.cardview_id);
        checkBox = (CircleCheckBox) itemView.findViewById(R.id.circle_check_box);
    }
}

I wish you guys can help me find the problem and maybe tell me about the way adapter methods work. 希望你们能帮助我找到问题,并告诉我适配器方法的工作方式。

I wish you create a boolean flag which will indicate visibility of all the checkboxes and call notifyDataSetChanged() to rebind items: 我希望您创建一个布尔标志,该标志将指示所有复选框的可见性,并调用notifyDataSetChanged()来重新绑定项目:

private boolean mAreCheckboxesVisible = false;
// ...
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    // ...
    holder.checkBox.setVisibility(mAreCheckboxesVisible ? View.VISIBLE : View.GONE);
    holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            mAreCheckboxesVisible = true;
            notifyDataSetChanged();
            return true;
        }
    });
}

And if you want to escape selection mode: 如果要退出选择模式:

mAreCheckboxesVisible = false;
notifyDataSetChanged();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android-如何更改RecyclerView的小部件的可见性并将其显示在recyclerView的所有项目中? - Android - How to change the visibility of a widget of and show it in all items in recyclerView? 更改RecyclerView所有项目的View可见性 - Change the View visibility of all items of RecyclerView 如何更改 RecyclerView 隐藏项的可见性? - How to change visibility of hidden items of RecyclerView? 以编程方式选择指定项目时,如何在RecyclerView中更改项目的属性? - How to change items' properties in RecyclerView when chosing a specified item programatically? 如何在Android RecyclerView中获取单击项的文本 - How to get the text of clicked item in android recyclerview 单击android中的gridview的特定项目时如何刷新其他项目 - How to refresh other items when clicked a specific item of gridview in android 如何一次更改JFrame中所有项目的可见性? - How to change visibility of ALL items in a JFrame at once? 如何在RecyclerView中更改单个项目的颜色? - How to change color of a single item in RecyclerView? RecyclerView项目不会一次全部更改可见性 - RecyclerView items not changing visibility all at once 使用导航组件单击 RecyclerView 项目时如何从 RecyclerView OnClick 侦听器更改片段 - How to change fragment from the Recycler View OnClick Listener when RecyclerView item is clicked using Navigation Components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM