Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用開源框架 SwipeListView 快速做出列表滑動刪除

編輯:關於 Android 程式設計

近幾年微信、QQ 相繼帶起「滑動刪除」的風潮,這個互動幾乎成了清單類頁面的標準配備。網路上做過一次橫向比較:微信的實作看起來更像一個超出螢幕的單層布局,滑動時右側那顆原本在畫面外的按鈕滑進來;而 QQ 的做法是在 ListView 的 item 右側預埋一顆隱藏按鈕,偵測到滑動時給它一段進場動畫。後者顯然更好落地。

先談談 SwipeListView 的結構

GitHub 上早有成熟方案,最有名的莫過於 SwipeListView。它的本質是一個 FrameLayout 的兩層堆疊:上層 front 蓋住下層 back,滑動時把 front 推開,底下的 back(放刪除、取消等按鈕)就露出來了。本文就以這個框架為主角,走一遍完整的接入流程。

路線一:以 Library 方式引入

把 SwipeListViewLibrary 當成 Android 工程的依賴庫使用。要注意它自身又依賴另一個知名框架 NineOldAndroids(負責向下相容屬性動畫),這個在網上和 GitHub 上都很容易找到。

先把 NineOldAndroids 匯入,勾選 Is Library,它就具備了被其他工程依賴的資格:

接著匯入 SwipeListViewLibrary,除了同樣勾選 Is Library,別忘了在右側的 Add 裡把 NineOldAndroids 掛上去當作它的依賴:

兩個庫都進來之後先 clean 一次——很多莫名其妙的錯誤 clean 一下就消失。然後建立自己的專案,在 Add 裡掛上 SwipeListViewLibrary 即可。布局中直接寫:

 <com.fortysevendeg.swipelistview.SwipeListView
    android:id="@+id/swipe_lv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:swipeMode="left"
    app:swipeAnimationTime="300"
    app:swipeOffsetLeft="200dp"
    app:swipeFrontView="@+id/front"
    app:swipeBackView="@+id/back"
    app:swipeActionLeft="reveal"/>

其中 app:swipeFrontView 指向上層 View 的 id,app:swipeBackView 指向下層。這兩個 id 就寫在 Adapter 使用的 item 布局裡:

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout android:id="@+id/back"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button android:id="@+id/close_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/delete_btn"
            android:text="Close"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:focusable="false"
            android:focusableInTouchMode="false"/>
        <Button android:id="@+id/delete_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:text="Delete"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:focusable="false"
            android:focusableInTouchMode="false"/>
    </RelativeLayout>

    <RelativeLayout android:id="@+id/front"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white">
        <TextView android:id="@+id/content_tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="hello world"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/black"/>
    </RelativeLayout>
</FrameLayout>

Activity 中的初始化相當直觀:

 arrays = new ArrayList<String>(Arrays.asList(Util.arrays));
mSwipeLv = (SwipeListView) findViewById(R.id.swipe_lv);
mAdapter = new MyAdapter(this, arrays);
mSwipeLv.setAdapter(mAdapter);
mSwipeLv.setSwipeListViewListener(new BaseSwipeListViewListener() {
    @Override
    public void onClosed(int position, boolean fromRight) {
    }
});

自定義 BaseAdapter 的 getView():

 @Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(mContext).inflate(
                R.layout.layout_swipe_list_item, null);
        holder.mContentTv = (TextView) convertView.findViewById(R.id.content_tv);
        holder.mCloseBtn = (Button) convertView.findViewById(R.id.close_btn);
        holder.mDeleteBtn = (Button) convertView.findViewById(R.id.delete_btn);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.mContentTv.setText(arrays.get(position));
    holder.mCloseBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((SwipeListView) parent).closeAnimate(position);
        }
    });
    holder.mDeleteBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((SwipeListView) parent).closeOpenedItems();
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mArrays.remove(position);
                    mAdapter.notifyDataSetChanged();
                }
            }, 350);
        }
    });
    return convertView;
}

寫到這裡就大功告成,運行結果如下:

路線二:直接引入 jar 包

開篇的專案頁面裡也能找到官方提供的兩個 jar。但把 jar 丟進 build path 並不等於立刻能用,還有幾件事要做:

  1. 確認專案已加入 Android 支援庫 android-support-v4.jar。
  2. 從 SwipeListViewLibrary 的 res/values/swipelistview__attrs.xml 複製到新專案的 res/values/ 目錄——jar 包裡並不包含這份屬性宣告檔,少了它布局 XML 就無法識別那些 app:swipeXxx 屬性。

另外還有兩個實務上的坑:

其一,jar 中的 SwipeListView 套件名稱與庫工程版本不同,import 時要留意別走錯。

其二,準備都到位後,編譯時偶爾仍會抱怨找不到 swipeFrontView 與 swipeBackView 這兩個屬性宣告。這被認為是框架本身的一個 bug,Stack Overflow 上給出的解法是:宣告這兩個屬性時不要自訂 id 名稱,改用框架預設的 swipelistview_backview 與 swipelistview_frontview。

兩條路線怎麼選

若專案會持續維護、需要跟著框架升級,走 Library 依賴更省心;若只是快速驗證效果,jar 包更輕巧,但務必記得補上 attrs 檔與支援庫。無論哪種,都建議先把 NineOldAndroids 這層依賴關係理清,否則低版本裝置上的動畫會直接失效。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved