Android ListView滑動刪除操作(SwipeListView)
新版本的微信和QQ上引入的滑動刪除功能是現(xiàn)在比較流行的一個功能。其實(shí)這個滑動刪除的控件,github上已經(jīng)有了,是一個熱門的開源框架SwipeListView。不過,這個SwipeListView是一個framelayout,即是一個兩層的布局,上面的布局front覆蓋了下面的布局back,滑動的時候則會滑開front,這樣下面的back就顯示出來了。但是看了一下微信的滑動刪除好像不是這樣的,感覺更像是一個超出了屏幕的單層布局,滑動的時候是右邊超出屏幕的button進(jìn)入屏幕,猜測應(yīng)該不是使用SwipeListView控件。QQ的滑動刪除則是在ListView的item右邊隱藏一個button,但檢測到滑動事件的時候,給button一個出現(xiàn)的動畫,使其可見,這個方案應(yīng)該是最好實(shí)現(xiàn)的了。
本篇主要是學(xué)習(xí)SwipeListView這個開源框架。
使用這個框架有兩種方式,一種是導(dǎo)入SwipeListViewLibrary這個工程,將其作為一個android工程的依賴庫。由于SwipeListViewLibrary庫工程自身也依賴另外一個熱門的開源框架NineOldAndroids,這個也很容易就能網(wǎng)上或者github上搜到。
導(dǎo)入這兩個庫工程,對于NineOldAndroids,做如下設(shè)置,其實(shí)主要就是勾選Is Library這個選項(xiàng),這樣就能是NineOldAndroids工程作為別的工程的依賴庫使用:

對于SwipeListViewLibrary,除了要勾選Is Library選項(xiàng),記得在旁邊的Add里面,加上上面的NineOldAndroids作為本庫的依賴庫:

下面就是使用這個庫了,先clean一下上面兩個庫工程,很多時候工程的錯誤,clean一下就好了。然后新建自己的工程,在Add選項(xiàng)里面添加SwipeListViewLibrary工程就行。這樣就能直接使用SwipeListView這個控件了,很簡單,代碼如下:
<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屬性就是指定前面說的framelayout里面上面一層的view的id,app:swipeBackView則是指定下面一層的view的id,在下面自定義BaseAdatpter要使用的item的布局里面可以看到:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://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;
}
然后就ok了,運(yùn)行工程的效果如下圖:

另外一種是用SwipeListView控件的方法就是直接導(dǎo)入官方給出的兩個jar包,上面開篇的地址里可以看到,但是直接導(dǎo)入這兩個jar包,不代表可以立即使用了!首先先把這個包添加到新建工程的build path里面,如果你的工程沒有添加android的支持包android-support-v4.jar記得也添加以下,然后記得從前面已經(jīng)導(dǎo)入過的SwipeListViewLibrary庫工程中的res\values\swipelistview__attrs.xml文件復(fù)制到新建工程的res/values/目錄下,這個文件主要是申明SwipeListView控件里面的各項(xiàng)屬性的,直接導(dǎo)入的jar包是沒有包含申明這些屬性的文件的。然后就是向上面一樣在代碼里面引用了,不過需要注意兩點(diǎn):一,jar包里面SwipeListView的包名和庫工程里面的包名是不一樣的,引用的時候需要注意以下;二,準(zhǔn)備妥當(dāng),確認(rèn)前面步驟無誤后,有時在編譯工程時回報錯,說沒有申明swipeFrontView和swipeBackView兩個屬性,這個問題好像是SwipeListView框架的一個bug,stackoverflow上有人指出過,,大意就是在布局文件里面申明swipeFrontView和swipeBackView這兩個屬性的值得時候,最好不要自定義id的名稱,而是使用swipelistview_backview和swipelistview_frontview。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android RecyclerView側(cè)滑菜單,滑動刪除,長按拖拽,下拉刷新上拉加載
- android RecyclerView實(shí)現(xiàn)條目Item拖拽排序與滑動刪除
- Android中RecyclerView實(shí)現(xiàn)滑動刪除與拖拽功能
- Android ListView實(shí)現(xiàn)上拉加載下拉刷新和滑動刪除功能
- Android仿微信列表滑動刪除 如何實(shí)現(xiàn)滑動列表SwipeListView
- Android使用SwipeListView實(shí)現(xiàn)類似QQ的滑動刪除效果
- Android程序開發(fā)之ListView 與PopupWindow實(shí)現(xiàn)從左向右滑動刪除功能
- Android RecyclerView滑動刪除和拖動排序
- Android實(shí)現(xiàn)ListView左右滑動刪除和編輯
- android自定義View滑動刪除效果
相關(guān)文章
Android開發(fā)可添加頭尾的RecycleView的實(shí)現(xiàn)
這篇文章主要為大家介紹了Android開發(fā)可添加頭尾的RecycleView的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android開發(fā)進(jìn)階自定義控件之滑動開關(guān)實(shí)現(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了Android開發(fā)進(jìn)階自定義控件之滑動開關(guān)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android自定義開關(guān)控件的原理、實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
Android實(shí)現(xiàn)自定義dialog的代碼
這篇文章主要介紹了Android實(shí)現(xiàn)自定義dialog的實(shí)例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-11-11
Android開發(fā)中使用Intent打開第三方應(yīng)用及驗(yàn)證可用性的方法詳解
這篇文章主要介紹了Android開發(fā)中使用Intent打開第三方應(yīng)用及驗(yàn)證可用性的方法,結(jié)合實(shí)例形式分析了Android使用Intent打開第三方應(yīng)用的三種常用方式及使用注意事項(xiàng),需要的朋友可以參考下2017-11-11
Android自定義View實(shí)現(xiàn)雪花特效
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)雪花特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-02-02
Android開發(fā)之利用jsoup解析HTML頁面的方法
這篇文章主要介紹了Android開發(fā)之利用jsoup解析HTML頁面的方法,結(jié)合實(shí)例形式分析了Android基于jsoup jar包來抓取html頁面的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-03-03
Android實(shí)現(xiàn)阿里云oss上傳流程解析
這篇文章主要介紹了Android實(shí)現(xiàn)阿里云oss上傳流程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
Android實(shí)現(xiàn)分享長圖并且添加全圖水印
這篇文章主要介紹了Android實(shí)現(xiàn)分享長圖并且添加全圖水印的相關(guān)資料,需要的朋友可以參考下2017-03-03

