SwipeListView是Github上的一個開源框架,地址:https://github.com/47deg/android-swipelistviewjava
SwipeListView was born out of the need to add swipe gestures to ListView on Android.android
由於開發須要整一個相似QQ側滑刪除的ListView,一開始準備本身寫一個自定義layout做爲item佈局,依稀記得有個叫swipelistview的開源框架,結果還真能夠拿來用,效果略有差異,但也知足項目要求了。那麼,問題也來了,Github上最後的push的SwipelistView使用的是Gradle來構建,無奈鄙人幾個月來一直在用es開始開發,考慮到可能要查看或者修改它的源碼,就只好手工轉爲ecplise下的工程。下面先上Demo說明基本使用方法,方法都很簡單,主要是網上搜索的資料都不是很完整,本身寫一篇備忘,也供新手朋友們參考,再說說Gradle構建的swipelistview Module如何導入eclipse工程。git
1、引用swipelistview庫開發帶側滑+按鈕的ListViewgithub
swipelistview的使用同listview的使用方法差很少,由於swipelistview是直接繼承於ListView的,只是擴展了一些方法。框架
官方給出了再XML佈局文件中的基本寫法:eclipse
1 <com.fortysevendeg.swipelistview.SwipeListView 2 xmlns:swipe="http://schemas.android.com/apk/res-auto" 3 android:id="@+id/example_lv_list" 4 android:listSelector="#00000000" 5 android:layout_width="fill_parent" 6 android:layout_height="wrap_content" 7 swipe:swipeFrontView="@+id/front" 8 swipe:swipeBackView="@+id/back" 9 swipe:swipeActionLeft="[reveal | dismiss]" 10 swipe:swipeActionRight="[reveal | dismiss]" 11 swipe:swipeMode="[none | both | right | left]" 12 swipe:swipeCloseAllItemsWhenMoveList="[true | false]" 13 swipe:swipeOpenOnLongPress="[true | false]" 14 swipe:swipeAnimationTime="[miliseconds]" 15 swipe:swipeOffsetLeft="[dimension]" 16 swipe:swipeOffsetRight="[dimension]" 17 />
各屬性說明以下:佈局
swipeFrontView - Required - front view id.
swipeBackView - Required - back view id.
swipeActionLeft - Optional - left swipe action Default: 'reveal'
swipeActionRight - Optional - right swipe action Default: 'reveal'
swipeMode - Gestures to enable or 'none'. Default: 'both'
swipeCloseAllItemsWhenMoveList - Close revealed items on list motion. Default: 'true'
swipeOpenOnLongPress - Reveal on long press Default: 'true'
swipeAnimationTime - item drop animation time. Default: android configuration
swipeOffsetLeft - left offset
swipeOffsetRight - right offset
Demo地址,效果圖:gradle
咱們本身定義swipelistview的item佈局:ui
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" > 6 7 <LinearLayout 8 android:id="@+id/backview" 9 android:layout_width="match_parent" 10 android:layout_height="70dp" 11 android:background="#eee" 12 android:tag="back" > 13 14 <Button 15 android:id="@+id/example_1" 16 android:layout_width="0dp" 17 android:layout_height="wrap_content" 18 android:layout_weight="2" 19 android:background="@null" /> 20 21 <Button 22 android:id="@+id/edit" 23 android:layout_width="0dp" 24 android:layout_height="60dp" 25 android:layout_gravity="center" 26 android:layout_weight="1" 27 android:background="@drawable/edit" 28 android:text="編輯" /> 29 30 <Button 31 android:id="@+id/del" 32 android:layout_width="0dp" 33 android:layout_height="60dp" 34 android:layout_gravity="center" 35 android:layout_marginLeft="10dp" 36 android:layout_weight="1" 37 android:background="@drawable/del" 38 android:text="刪除" /> 39 </LinearLayout> 40 41 <RelativeLayout 42 android:id="@+id/frontview" 43 android:layout_width="match_parent" 44 android:layout_height="70dp" 45 android:background="#ffffff" 46 android:orientation="vertical" 47 android:tag="front" > 48 49 <TextView 50 android:id="@+id/example_row_tv_title" 51 android:layout_width="fill_parent" 52 android:layout_height="wrap_content" 53 android:layout_centerInParent="true" 54 android:textSize="16sp" /> 55 </RelativeLayout> 56 57 </FrameLayout>
在佈局swipeview所在佈局文件中的定義spa
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 xmlns:swipe="http://schemas.android.com/apk/res-auto" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".MainActivity" > 7 8 <com.fortysevendeg.swipelistview.SwipeListView 9 android:id="@+id/example_lv_list" 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 swipe:swipeActionLeft="reveal" 13 swipe:swipeActionRight="reveal" 14 swipe:swipeAnimationTime="0" 15 swipe:swipeBackView="@+id/backview" 16 swipe:swipeCloseAllItemsWhenMoveList="true" 17 swipe:swipeFrontView="@+id/frontview" 18 swipe:swipeMode="both" 19 swipe:swipeOffsetLeft="0dp" 20 swipe:swipeOffsetRight="0dp" 21 swipe:swipeOpenOnLongPress="false" /> 22 23 </RelativeLayout>
使用注意點:
爲了讓每次顯示滑動一條數據,須要在你本身實現的BaseSwipeListViewListener中重寫onStartOpen方法,在裏面調用mSwipeListView.closeOpenedItems()。
2、導入swipelistview到eclipse工程
也許有人一看是gradle構建的android項目就不知道怎麼辦了,覺得as構建的項目是不能之間導入eclipse工程的。幸運的是,swipelistview工程極其簡潔,手工導入eclipse也是比較的容易的。
1. 新建一個android工程,勾選標記爲libraray,且不用建立activity。
2. 將下載下來的android-swipelistview-master工程中\android-swipelistview-master\swipelistview\src\main\java下的com文件下直接粘貼到新建工程的src目錄下。
同時把\android-swipelistview-master\swipelistview\src\main目錄下的res資源放到eclipse下新建的Android庫工程對應目錄下。
3. 作完上面兩部尚未完,由於swipelistview依賴nineoldandroids庫,且目前的swipelistview版本依賴nineoldandroids-2.4.0+,下載地址https://github.com/JakeWharton/NineOldAndroids,能夠只下載
nineoldandroids-2.4.0.jar,直接放在新建android庫工程的libs目錄下便可。至此導入工做完成。