RecycleView CardView长按并删除

时间:2016-12-21 21:30:18

标签: android android-layout android-cardview

在我的应用程序中,我有一个具有许多cardView控件的recycleView。我希望允许用户长按选择cardView控件。在此操作中,用户可以在cardViews上进行长按操作并选择多个cardViews。当用户使用长按操作选择cardView时,FloatingAction按钮应更改为删除,并允许用户从集合中删除所选元素。 我该如何执行此操作?以下是我正在使用的代码。

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

2 个答案:

答案 0 :(得分:0)

此链接提供了对我的要求的一个很好的解释:

https://enoent.fr/blog/2015/01/18/recyclerview-basics/

答案 1 :(得分:0)

长按此事件:您可以删除或隐藏RecyclerView

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

                    Log.v("long clicked");

                    return true; 
                } 
            });