首頁>科技>

客服:“xxx,退貨的按鈕在哪啊,我怎麼找不到啊?”我:“像Android 手機QQ一樣側滑就出來了。。。”。類似這樣的對話重複上演了N次

最早收到的要實現Item側滑刪除的需求是在一個餐飲專案,當時剛入坑沒多久,還以為SDK就有現成的API,畢竟像QQ這種超級App都用的這麼騷了。於是一頓搜尋之後,失望了,這尼瑪是IOS上的效果啊。Android上實現了這種效果的App都是自定義View的

當時小白的自己心血來潮想著自己寫個試試,一陣搗鼓下來結果弄了個四不像。最後還是尷尬的打開了GayHub,找了很多都是基於ListView或RecyclerView自定義的,這種感覺入侵性太強,所以都放棄了。最後找到了今天的主角SwipeMenuLayout(https://github.com/mcxtzhang/SwipeDelMenuLayout),先看效果圖

用法非常簡單,當成普通的ViewGroup使用即可,首先我們先引到自己的專案,可以在app module的gralde引入

dependencies { implementation 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0'}

也可以直接下載整個lib,這樣的好處的是方便自己修改。其實只有一個類,還有就是style了

<?xml version="1.0" encoding="utf-8"?><com.one.hope.widget.SwipeMenuLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:clickable="true"    android:focusable="true">    <LinearLayout        android:id="@+id/ll_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#fff"        android:orientation="vertical"        android:paddingStart="10dp"        android:paddingEnd="10dp">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="27dp"            android:orientation="horizontal">            <TextView                android:id="@+id/tv_name"                style="@style/add_text"                tools:text="姓名" />            <TextView                android:id="@+id/tv_phone"                style="@style/add_text"                android:layout_weight="2"                tools:text="聯絡方式" />        </LinearLayout>        <TextView            android:id="@+id/tv_address"            android:layout_width="match_parent"            android:layout_height="27dp"            android:gravity="center_vertical"            tools:text="地址" />    </LinearLayout>    <Button        android:id="@+id/btnUpdate"        android:layout_width="60dp"        android:layout_height="match_parent"        android:background="@color/colorPrimary"        android:text="修改"        android:textColor="@android:color/white" />    <Button        android:id="@+id/btnDelete"        android:layout_width="60dp"        android:layout_height="match_parent"        android:background="#ff4a57"        android:text="刪除"        android:textColor="@android:color/white" /></com.one.hope.widget.SwipeMenuLayout>

然後編寫Activity,列表我選了RecyclerView,當然也可以用ListView(這就是SwipeMenuLayout的強大之處,0耦合)。Adapter使用的是BaseRecyclerViewAdapterHelper(https://github.com/CymChad/BaseRecyclerViewAdapterHelper)

此篇章旨在介紹有這個麼強大又好用的東西,具體使用介紹和實現原理可以看作者的分析《側滑刪除選單 SwipeMenuLayout》(/file/2019/12/25/20191225154506_18759.jpg

最新評論
  • 整治雙十一購物亂象,國家再次出手!該跟這些套路說再見了
  • 亞馬遜貼標又有新要求,不做可能影響發貨