Android使用SwipeListView實現(xiàn)類似QQ的滑動刪除效果
QQ的滑動刪除效果很不錯,要實現(xiàn)這種效果,可以使用SwipeListView。
1. 下載com.fortysevendeg.swipelistview這個項目(以前GitHub上有,現(xiàn)在GitHub上沒有了,百度了很多次才下載到的),導(dǎo)入Eclipse,右鍵單擊,選擇Properties->Android,選中Library下面的IsLibrary。

2. 新建一個項目MySwipeListView,加入SwipeListView這個庫。

3. 在主窗體里面放入一個SwipeListView控件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://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="http://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,即不滑動時顯示的View。
swipe:swipeBackView:下面的View,即滑動后顯示的View。
這兩個View都定義在SwipeListView的行布局文件里面:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://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>
SwipeListView的行布局文件使用FrameLayout布局,F(xiàn)rameLayout里面所有的所有子元素都堆疊在FrameLayout的左上角。
4. SwipeListView和其他ListView一樣,也需要Adapter,使用方法也是一樣的。這里就不詳細(xì)講了。
5. 在主窗體Java文件中實現(xiàn)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 +"條數(shù)據(jù)!");
}
}
最主要的代碼即mSwipeListView.setSwipeListViewListener(new BaseSwipeListViewListener(){}),通過這行代碼,為SwipeListView控件設(shè)置了Listener,可以根據(jù)自己的需要重載BaseSwipeListViewListener的各種方法。
運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中NavigationView的使用與相關(guān)問題解決
大家都知道NavigationView的引入讓 Android側(cè)邊欄實現(xiàn)起來相當(dāng)方便,最近公司項目中也使用這個新的控件完成了側(cè)邊欄的改版。在使用過程中遇到一些問題所以記錄一下。本文分為兩個部分,一是基本使用,二是相關(guān)問題的解決,感興趣的朋友們下面來一起看看吧。2016-10-10
Android 帶進(jìn)度條的WebView 示例代碼
本文主要介紹Android WebView,這里提供實例代碼,和效果圖供大家參考,希望能幫助有需要的小伙伴2016-07-07
Android開發(fā)之自定義view實現(xiàn)通訊錄列表A~Z字母提示效果【附demo源碼下載】
這篇文章主要介紹了Android開發(fā)之自定義view實現(xiàn)通訊錄列表A~Z字母提示效果,結(jié)合完整實例形式分析了Android獲取通訊錄列表及采用自定義view排列顯示的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Android跳轉(zhuǎn)到系統(tǒng)聯(lián)系人及撥號或短信界面
現(xiàn)在開發(fā)中的功能需要直接跳轉(zhuǎn)到撥號、聯(lián)系人、短信界面等等,查找了很多資料,自己整理了一下特此分享到腳本之家平臺供大家參考2016-12-12
Android實現(xiàn)加載廣告圖片和倒計時的開屏布局
這篇文章主要介紹了Android實現(xiàn)加載廣告圖片和倒計時的開屏布局,需要的朋友可以參考下2014-07-07
橫豎屏切換導(dǎo)致頁面頻繁重啟screenLayout解析
這篇文章主要為大家介紹了橫豎屏切換導(dǎo)致頁面頻繁重啟screenLayout解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

