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

用 SwipeListView 實作類 QQ 的滑動刪除效果

編輯:關於 Android 程式設計

QQ 的滑動刪除互動相當順手,想做出類似效果,可以借助 SwipeListView 這個開源庫。

1. 下載 com.fortysevendeg.swipelistview 專案(以往 GitHub 上有,如今已下架,得四處搜尋才能找到),匯入 Eclipse 後右鍵選擇 Properties -> Android,勾選 Library 下方的 IsLibrary。

2. 新建一個專案 MySwipeListView,並加入 SwipeListView 這個庫。

3. 在主視窗放入一個 SwipeListView 元件:

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.hzhi.myswipelistview.MainActivity" > <com.fortysevendeg.swipelistview.SwipeListView xmlns:swipe="https://schemas.android.com/apk/res-auto" android:id="@+id/exampleSwipeListView" android:listSelector="#00000000" android:layout_width="fill_parent" android:layout_height="wrap_content" swipe:swipeBackView="@+id/back" swipe:swipeCloseAllItemsWhenMoveList="true" swipe:swipeDrawableChecked="@drawable/choice_selected" swipe:swipeDrawableUnchecked="@drawable/choice_unselected" swipe:swipeFrontView="@+id/front" swipe:swipeMode="both" swipe:swipeActionLeft="reveal" swipe:swipeActionRight="dismiss" swipe:swipeOpenOnLongPress="true" /> </LinearLayout>

其中兩個關鍵屬性:
swipe:swipeFrontView:上層 View,也就是未滑動時顯示的內容。
swipe:swipeBackView:下層 View,也就是滑動後才顯示的內容。
這兩個 View 都定義在 SwipeListView 的列版面檔中:

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:id="@+id/back" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffcccccc" android:gravity="right" android:tag="back" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_delete" android:text="刪除"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_update" android:text="更新"/> </LinearLayout> <RelativeLayout android:orientation="vertical" android:id="@+id/front" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffffff" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/example_row_iv_image"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/example_row_iv_image" android:id="@+id/example_row_tv_title"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/example_row_iv_image" android:layout_below="@id/example_row_tv_title" android:id="@+id/example_row_tv_description"/> </RelativeLayout> </FrameLayout>

列版面檔採用 FrameLayout,其子元素會層疊在左上角。

4. SwipeListView 和其他 ListView 一樣需要 Adapter,用法相同,此處不多贅述。

5. 在主視窗 Java 檔中實作 SwipeListView 的功能:

 package com.hzhi.myswipelistview; import android.support.v7.app.ActionBarActivity; import android.util.Log; import java.util.ArrayList; import com.fortysevendeg.swipelistview.BaseSwipeListViewListener; import com.fortysevendeg.swipelistview.SwipeListView; import android.os.Bundle; @SuppressWarnings("deprecation") public class MainActivity extends ActionBarActivity { protected static final String TAG = "MySwipeListView"; private ArrayList<String> mList; private MyAdapter mAdapter; private SwipeListView mSwipeListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); mSwipeListView = (SwipeListView) findViewById(R.id.exampleSwipeListView); mAdapter = new MyAdapter(this, mList, mSwipeListView); mSwipeListView.setAdapter(mAdapter); mSwipeListView.setSwipeListViewListener(new BaseSwipeListViewListener(){ @Override public void onChoiceChanged(int position, boolean selected) { Log.d(TAG, "onChoiceChanged:" + position + ", " + selected); } @Override public void onChoiceEnded() { Log.d(TAG, "onChoiceEnded"); } @Override public void onChoiceStarted() { Log.d(TAG, "onChoiceStarted"); } @Override public void onClickBackView(int position) { Log.d(TAG, "onClickBackView:" + position); } @Override public void onClickFrontView(int position) { Log.d(TAG, "onClickFrontView:" + position); } @Override public void onClosed(int position, boolean fromRight) { Log.d(TAG, "onClosed:" + position + "," + fromRight); } @Override public void onDismiss(int[] reverseSortedPositions) { Log.d(TAG, "onDismiss"); } @Override public void onFirstListItem() { Log.d(TAG, "onFirstListItem"); } @Override public void onLastListItem() { Log.d(TAG, "onLastListItem"); } @Override public void onListChanged() { Log.d(TAG, "onListChanged"); mSwipeListView.closeOpenedItems(); } @Override public void onMove(int position, float x) { Log.d(TAG, "onMove:" + position + "," + x); } @Override public void onOpened(int position, boolean toRight) { Log.d(TAG, "onOpened:" + position + "," + toRight); } @Override public void onStartClose(int position, boolean right) { Log.d(TAG, "onStartClose:" + position + "," + right); } @Override public void onStartOpen(int position, int action, boolean right) { Log.d(TAG, "onStartOpen:" + position + "," + action + "," + right); } }); } private void initData(){ mList = new ArrayList<String>(); for (int i = 0; i <= 10; i++) mList.add("這是第" + i +"條數據!"); } }

核心就是 mSwipeListView.setSwipeListViewListener(new BaseSwipeListViewListener(){}),透過它為元件掛上監聽器,並依需求重寫 BaseSwipeListViewListener 的各種方法。

運行結果如下:

以上就是本文全部內容,希望對大家的學習有所幫助。

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