如何一次性删除RecycleView中的行?

时间:2016-08-14 08:53:08

标签: android android-recyclerview

我是一名学生,对于Android开发更新鲜。

经过大量研究,我已成功为我的项目实施了RecycleView。在我的项目中,当用户长时间点击RecyclerView中的行时,他会获得一个复选框和一个删除按钮,用于删除该行的所有行。但我想要的是当用户长时间点击任何一行时。只有该行的特定复选框或删除按钮才会显示。并且要选择其他行,他可以单击它们,它们将被选中,然后具有全局删除按钮,可以删除用户选择的任何行。我可以看到的任何建议或博客? 的 MyCustomAdapter

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomRecyclerViewHolder>{

    List<RecyclerViewClass> mItems;
    Context mContext;
    boolean onLongPressReceived = false;
    UpdateMainClass updateMainClass;

    /**
     * Contructor to initialize context and list items.
     * @param context Context of the Activity on which RecyclerView is initialised
     * @param items List of POJO object that contains the data to update the rows
     */
    public CustomAdapter(Context context, List<RecyclerViewClass> items){
        mContext = context;
        mItems = items;
        //Check whether the Activity implements UpdateMainClass Interface or not
        if(context instanceof UpdateMainClass){
            updateMainClass = (UpdateMainClass)context;
        }
    }

    /**
     * <p>This method updates the long press status variable when called from the Activity</p>
     * <p>Helpful to prevent any unwanted changes to status variable</p>
     * @param status tell whether whether long press is clicked or not
     */
    public void setOnLongPressStatus(boolean status){
        onLongPressReceived = status;
        notifyDataSetChanged();
    }

    /**
     * Provides the long press status to the Activity
     * @return longPressReceived status
     */
    public boolean getLongPressStatus(){
        return onLongPressReceived;
    }

    @Override
    public CustomRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(mContext).inflate(R.layout.custom_recyclerview_layout, parent, false);
        //set the margin if any, will be discussed in next blog
        return new CustomRecyclerViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final CustomRecyclerViewHolder holder, int position) {
        holder.mAvatarView.setImageDrawable(mItems.get(position).getmImage_url());
        holder.mMsg1.setText(mItems.get(position).getMessage1());
        holder.mMsg2.setText(mItems.get(position).getMessage2());
        /**
         * <p>Enable the row delete and select layout and
         * Colors the background based on check box status if onLongPressReceived variable is set to true</p>
         * <p>Otherwise renders a regular white background list with visible checkboxes that are selected.</p>
         */
        if(onLongPressReceived) {
            holder.checkboxHolder.setVisibility(View.VISIBLE);
            holder.mCheckBox.setChecked(mItems.get(position).getmIsChecked());
            holder.mDeleteRow.setVisibility(View.VISIBLE);
            if(mItems.get(holder.getAdapterPosition()).getmIsChecked()){
                holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_selected));
            }else {
                //holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_long_pressed));
            }
        }
            //Checking whether a particular view is clicked or not
        else{
            holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_unselected));
            holder.mDeleteRow.setVisibility(View.INVISIBLE);
            if (mItems.get(position).getmIsChecked()) {
                holder.checkboxHolder.setVisibility(View.VISIBLE);
                holder.mCheckBox.setChecked(true);
            } else {
                holder.checkboxHolder.setVisibility(View.GONE);
                holder.mCheckBox.setChecked(false);
            }
        }
        //Calls the interface method in Activity to respond to CheckBox changes
        holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                System.out.println("Holder Position is:   " + holder.getAdapterPosition());
                System.out.println("Holder Boolean is  " + b);
                updateMainClass.updateListBackground(holder.getAdapterPosition(), b);
            }
        });
        /**
         * <p>Responds to long press made on any row</p>
         * <p>Checks the item on which long press is made
         * sets the onLongPressReceived status to true and notify the adapter to refresh the list.</p>
         */
        holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                onLongPressReceived = true;
                mItems.get(holder.getAdapterPosition()).setmIsChecked(true);
                notifyDataSetChanged();
                return true;
            }
        });
        //Calls the interface in Activity to remove the item from the List.
        holder.mDeleteRow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateMainClass.updateItemList(holder.getAdapterPosition());
            }
        });

    }

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

    public class CustomRecyclerViewHolder extends RecyclerView.ViewHolder{

        private TextView mMsg1, mMsg2;
        private ImageView mAvatarView;
        private CheckBox mCheckBox;
        private LinearLayout checkboxHolder;
        private ImageView mDeleteRow;
        private CardView cardView;


        /**
         * Initializes all the views of a ViewHolder
         * @param itemView parent view in which all the List Item views are present
         */
        public CustomRecyclerViewHolder(View itemView) {
            super(itemView);
            mMsg1 = (TextView)itemView.findViewById(R.id.text_view1);
            mMsg2 = (TextView)itemView.findViewById(R.id.text_view2);
            mAvatarView = (ImageView)itemView.findViewById(R.id.avatar_holder);
            mCheckBox = (CheckBox)itemView.findViewById(R.id.checkbox);
            checkboxHolder = (LinearLayout)itemView.findViewById(R.id.checkbox_holder);
            mDeleteRow = (ImageView)itemView.findViewById(R.id.delete_row);
            cardView = (CardView)itemView.findViewById(R.id.card_holder);
        }
    }
    public interface UpdateMainClass{
        void updateItemList(int position);
        void updateListBackground(int position, boolean isChecked);
    }
}

1 个答案:

答案 0 :(得分:0)

this类对您的适配器进行子类化。然后在长时间单击一个视图后选择该视图并在单击后切换所选视图,如下所示。 boolean longClicked = false;

@Override
public void OnItemClickListener(View v,int position){
   if(longClicked){ 
      mAdapter.toggleSelection(position);
      return;
   }
  //normal click functionality here
}

@Override
public boolean OnItemLongClick(View v,int position){
   longClicked = true;
   mAdapter.toggleSelection(position);
}

选择要删除的所有项目后,您可以从适配器获取所选位置mAdapter.getSelectedItems()

注意:

完成对selectedItems的操作完成后,不要忘记将longClicked重置为false