2016-08-14 7 views
0

私は学生であり、アンドロイド開発にもっと夢中です。RecycleViewで一度に行を削除するには?

多くの調査の後、私は自分のプロジェクトで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); 
    } 
} 
+0

アダプタのリスト(mItems)から選択した行を削除してから、notifyItemChanged(position)またはnotifyItemRangeChanged()を呼び出してみましたか? RecyclerViewは、アダプタで購読しているリスト内の項目の状態を反映しています。アイテムの状態を変更するときは、新しい状態/変更を反映するためにnotifyDataSetChanged()またはnotifyItemChange()を呼び出す必要があります。 – Gil

+0

私はそれを調べます。また、特定の行をクリックすると。私は、特定の行のチェックボックスを表示したいだけで、リスト全体のチェックボックスは表示しません。どのようにそれを実装するか考えていますか? –

答えて

0

thisクラスからサブクラスアダプタを。次に、1つのビューを長時間クリックした後、そのビューを選択し、このように1回クリックした後に選択したビューを切り替える。 ブール値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()

注意としてアダプターから選択された位置を取得することができます。

はまた、あなたの後falselongClickedをリセットすることを忘れないでくださいにselectedItemsの操作が完了しました。

関連する問題