2016-12-21 4 views
1

In meiner Anwendung habe ich eine recycleView, die viele cardView-Steuerelemente hat. Ich möchte dem Benutzer erlauben, die cardView-Steuerelemente bei langem Drücken auszuwählen. In diesem Vorgang kann der Benutzer lange auf cardViews drücken und mehrere cardViews auswählen. Wenn der Benutzer eine cardView mit einem langen Druckvorgang auswählt, sollte die FloatingAction-Schaltfläche zum Löschen geändert werden und dem Benutzer ermöglichen, die ausgewählten Elemente aus der Sammlung zu löschen. Wie kann ich diesen Vorgang durchführen? Unten ist der Code, den ich verwende.RecycleView CardView lange drücken und löschen

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

Im Folgenden finden Sie Adapter und Halter Klasse:

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

Antwort

0

Dieses Ereignis für Long Press: Sie können löschen oder Verbergen RecyclerView

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

        Log.v("long clicked"); 

        return true; 
       } 
      }); 
Verwandte Themen