android中RecyclerView+CardView实现左滑删除和交换功能

本文介绍了如何在Android应用中使用RecyclerView结合CardView实现左滑删除及元素交换的功能。通过设置布局文件、定义Listener接口、自定义Adapter和实现ItemTouchHelper.Callback,最后在Activity中初始化相关代码,可以达成这一目标。
摘要由CSDN通过智能技术生成

语言无法具体表达做出来的效果,这里请读者先看一下演示

左滑删除功能演示

第一步,布局文件代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView android:id="@+id/card_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:clickable="true"
    android:foreground="?attr/selectableItemBackground"
    app:cardBackgroundColor="@color/colorAccent"
    app:cardCornerRadius="2dp"
    app:cardElevation="4dp">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="140dp">

        <ImageView
            android:id="@+id/item_img"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="21dp"
            android:background="@drawable/ic_22"
            android:scaleType="center" />


        <RelativeLayout

            android:layout_marginLeft="20dp"
            android:gravity="center_vertical"
            android:orientation="vertical"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:paddingBottom="5dp">
            <TextView
                android:text="nihao"
                android:layout_marginTop="5dp"
                android:id="@+id/tv_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="4dp"
                android:textColor="@color/color_white"
                android:textSize="16sp"
                 />
            <TextView
                android:text="nihao"
                android:layout_marginTop="5dp"
                android:id="@+id/tv_sex"
                android:layout_below="@+id/tv_name"
                android:layout_wid
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值