2016-12-21 46 views
1

在我的應用程序中,我有一個recycleView,它有許多cardView控件。我想讓用戶長按選擇cardView控件。在此操作中,用戶可以對cardViews執行長按操作並選擇多個cardView。當用戶使用長按操作選擇cardView時,FloatingAction按鈕應該更改爲刪除,並且應允許用戶從集合中刪除選定的元素。 如何執行此操作?以下是我正在使用的代碼。RecycleView CardView長按並刪除

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardUseCompatPadding="true" 
    card_view:cardCornerRadius="8dp" 
    android:layout_marginBottom="16dp"> 

    <LinearLayout android:orientation="vertical" 
     android:id="@+id/display_card_view_linearLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/display_name" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textSize="13sp" 
      android:text="@string/app_name" 
      android:textColor="#ffffff" 
      android:paddingBottom="8dp" 
      android:paddingTop="8dp" 
      android:gravity="center_horizontal" 
      android:layout_alignParentBottom="true" 
      android:background="#1976D2"/> 
     <ImageView 
      android:id="@+id/display_image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/action_settings" 
      android:scaleType="fitCenter" /> 
     <TextView 
      android:id="@+id/display_information" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:textSize="13sp" 
      android:text="@string/app_name" 
      android:textColor="#ffffff" 
      android:layout_below="@+id/display_image" 
      android:paddingBottom="8dp" 
      android:paddingTop="8dp" 
      android:gravity="center_horizontal" 
      android:layout_alignParentBottom="true" 
      android:background="#1976D2"/> 



    </LinearLayout> 

</android.support.v7.widget.CardView> 

下面是適配器和支持類:

import android.content.Context; 
import android.content.Intent; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class DisplayItemViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener { 

    public TextView name; 
    public TextView detailedInformation; 
    public ImageView image; 

    //private final Context context; 

    public DisplayItemViewHolders(View itemView) { 
     super(itemView); 
     itemView.setOnClickListener(this); 

     //context = itemView.getActivity(); 

     name = (TextView) itemView.findViewById(R.id.display_name); 
     image = (ImageView) itemView.findViewById(R.id.display_image); 
    } 

    @Override 
    public void onClick(View view) { 

    } 
} 


public class DisplayItemRecyclerViewAdapter extends RecyclerView.Adapter<DisplayItemViewHolders> { 

    private List<DisplayItemInformation> itemList; 
    private Context context; 

    public DisplayItemRecyclerViewAdapter(Context context, List<DisplayItemInformation> itemList) { 
     this.itemList = itemList; 
     this.context = context; 
    } 

    @Override 
    public DisplayItemViewHolders onCreateViewHolder(ViewGroup parent, int viewType) { 

     View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.display_card, parent, false); 
     layoutView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT)); 
     DisplayItemViewHolders rcv = new DisplayItemViewHolders(layoutView); 
     return rcv; 
    } 

    @Override 
    public void onBindViewHolder(DisplayItemViewHolders holder, int position) { 
     holder.name.setText(itemList.get(position).getName()); 
     holder.image.setImageResource(itemList.get(position).getPhoto()); 
    } 

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

回答

0

此事件爲長按:您可以刪除或Hide RecyclerView

@Override 
       public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 
         int pos, long id) { 
        // TODO Auto-generated method stub 

        Log.v("long clicked"); 

        return true; 
       } 
      });