我们做listview左右滑动操作时,一般中情况下,都是像QQ那样,左滑弹出操作菜单(删除、编辑),然后选择菜单操作; 这样的效果不可谓不好,算是非常经典。 另外,有少数的APP,尤其是任务管理类的APP,更加注重listview的操作交互,例如ToDoList及滴答清单,这两个APP对任务的操作是直接通过滑动列表进行操作的;效果图如下:

gtihub上有一个开源项目,已经很好的实现了对该效果:https://github.com/wdullaer/SwipeActionAdapter ; 此处,就简单下介绍该开源项目。

1.  使用Android Studio新建项目导入该开源库:

导入该库,可以在build.gradle中添加:

dependencies {
compile 'com.wdullaer:swipeactionadapter:2.0.0'
}

当然,我更建议直接把该项目的library直接导入到项目中,这样会更加方便对代码进行修改。

2.   首先,设置ListView显示:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Create an Adapter for your content
String[] content = new String[20];
for (int i=0;i<20;i++) content[i] = "Row "+(i+1);
ArrayAdapter<String> stringAdapter = new ArrayAdapter<String>(
this,
R.layout.row_bg,
R.id.text,
new ArrayList<String>(Arrays.asList(content))
); // Wrap your content in a SwipeActionAdapter
mAdapter = new SwipeActionAdapter(stringAdapter); // Pass a reference of your ListView to the SwipeActionAdapter
mAdapter.setListView(getListView()); // Set the SwipeActionAdapter as the Adapter for your ListView
setListAdapter(mAdapter);
}

上面的代码很简单,只是比正常使用ListView多了一步:SwipeActionAdapter mAdapter = new SwipeActionAdapter(stringAdapter); 就是在普通Adapter的基础上包裹上一层SwipeActionAdapter。

3. 为ListView的Item添加滑动时的背景色

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Create an Adapter for your content
String[] content = new String[20];
for (int i=0;i<20;i++) content[i] = "Row "+(i+1);
ArrayAdapter<String> stringAdapter = new ArrayAdapter<String>(
this,
R.layout.row_bg,
R.id.text,
new ArrayList<String>(Arrays.asList(content))
); // Wrap your content in a SwipeActionAdapter
mAdapter = new SwipeActionAdapter(stringAdapter); // Pass a reference of your ListView to the SwipeActionAdapter
mAdapter.setListView(getListView()); // Set the SwipeActionAdapter as the Adapter for your ListView
setListAdapter(mAdapter); // Set backgrounds for the swipe directions
mAdapter.addBackground(SwipeDirection.DIRECTION_FAR_LEFT,R.layout.row_bg_left_far)
.addBackground(SwipeDirection.DIRECTION_NORMAL_LEFT,R.layout.row_bg_left)
.addBackground(SwipeDirection.DIRECTION_FAR_RIGHT,R.layout.row_bg_right_far)
.addBackground(SwipeDirection.DIRECTION_NORMAL_RIGHT,R.layout.row_bg_right);
}

上述代码最后,既是添加背景的代码,代码很明了,在为不同的滑动过程,添加不同的背景,该背景其实就是一个布局,例如row_bg_left_far.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="?android:listPreferredItemHeight"
android:background="@android:color/holo_blue_bright">
</LinearLayout>

此处,可以查看下SwipeDirection.java类源码:

public enum SwipeDirection {
// Constants
DIRECTION_NORMAL_LEFT,
DIRECTION_FAR_LEFT,
DIRECTION_NORMAL_RIGHT,
DIRECTION_FAR_RIGHT,
DIRECTION_NEUTRAL; @NonNull
public static List<SwipeDirection> getAllDirections(){
return Arrays.asList(
DIRECTION_FAR_LEFT,
DIRECTION_FAR_RIGHT,
DIRECTION_NEUTRAL,
DIRECTION_NORMAL_LEFT,
DIRECTION_NORMAL_RIGHT
);
} public boolean isLeft() {
return this.equals(DIRECTION_NORMAL_LEFT) || this.equals(DIRECTION_FAR_LEFT);
} public boolean isRight() {
return this.equals(DIRECTION_NORMAL_RIGHT) || this.equals(DIRECTION_FAR_RIGHT);
}
}

我们可以看出,主要的操作有四个: DIRECTION_NORMAL_LEFT(左滑一小段距离),DIRECTION_FAR_LEFT(左滑较长距离), DIRECTION_NORMAL_RIGHT(右滑一小段距离),DIRECTION_FAR_RIGHT(右滑较长距离)。

4.  添加ListView左右滑动监听:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Create an Adapter for your content
String[] content = new String[20];
for (int i=0;i<20;i++) content[i] = "Row "+(i+1);
ArrayAdapter<String> stringAdapter = new ArrayAdapter<String>(
this,
R.layout.row_bg,
R.id.text,
new ArrayList<String>(Arrays.asList(content))
); // Wrap your content in a SwipeActionAdapter
mAdapter = new SwipeActionAdapter(stringAdapter); // Pass a reference of your ListView to the SwipeActionAdapter
mAdapter.setListView(getListView()); // Set the SwipeActionAdapter as the Adapter for your ListView
setListAdapter(mAdapter); // Set backgrounds for the swipe directions
mAdapter.addBackground(SwipeDirection.DIRECTION_FAR_LEFT,R.layout.row_bg_left_far)
.addBackground(SwipeDirection.DIRECTION_NORMAL_LEFT,R.layout.row_bg_left)
.addBackground(SwipeDirection.DIRECTION_FAR_RIGHT,R.layout.row_bg_right_far)
.addBackground(SwipeDirection.DIRECTION_NORMAL_RIGHT,R.layout.row_bg_right); // Listen to swipes
mAdapter.setSwipeActionListener(new SwipeActionListener(){
@Override
public boolean hasActions(int position, SwipeDirection direction){
if(direction.isLeft()) return true; // Change this to false to disable left swipes
if(direction.isRight()) return true;
return false;
} @Override
public boolean shouldDismiss(int position, SwipeDirection direction){
// Only dismiss an item when swiping normal left
return direction == SwipeDirection.DIRECTION_NORMAL_LEFT;
} @Override
public void onSwipe(int[] positionList, SwipeDirection[] directionList){
for(int i=0;i<positionList.length;i++) {
int direction = directionList[i];
int position = positionList[i];
String dir = ""; switch (direction) {
case SwipeDirection.DIRECTION_FAR_LEFT:
dir = "Far left";
break;
case SwipeDirection.DIRECTION_NORMAL_LEFT:
dir = "Left";
break;
case SwipeDirection.DIRECTION_FAR_RIGHT:
dir = "Far right";
break;
case SwipeDirection.DIRECTION_NORMAL_RIGHT:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Test Dialog").setMessage("You swiped right").create().show();
dir = "Right";
break;
}
Toast.makeText(
this,
dir + " swipe Action triggered on " + mAdapter.getItem(position),
Toast.LENGTH_SHORT
).show();
mAdapter.notifyDataSetChanged();
}
}
});
}

·  public boolean hasActions(int position, SwipeDirection direction) : 该方法主要判断滑动方向:左滑还是右滑。

·  public boolean shouldDismiss(int position, SwipeDirection direction):该方法主要判断list item滑动后是否有消失的动画。

·  public void onSwipe(int[] positionList, SwipeDirection[] directionList): 主要在该方法中处理滑动逻辑。

以上,就是SwipeActionAdapter的基本使用方式,相信善用该库,一定会给APP增添几分色彩,提高用户体验!

Android 使用SwipeActionAdapter开源库实现简单列表的左右滑动操作的更多相关文章

  1. Android SwipeActionAdapter结合Pinnedheaderlistview实现复杂列表的左右滑动操作

    在上一篇博客<Android 使用SwipeActionAdapter开源库实现简单列表的左右滑动操作>里,已经介绍了利用SwipeActionAdapter来左右滑动操作列表: 然,有时 ...

  2. 我的Android进阶之旅】GitHub 上排名前 100 的 Android 开源库进行简单的介绍

    GitHub Android Libraries Top 100 简介 本文转载于:https://github.com/Freelander/Android_Data/blob/master/And ...

  3. GitHub 上排名前 100 的 Android 开源库进行简单的介绍

    若有任何疑问可通过邮件或微博联系我 项目名称 项目简介 1. react-native 这个是 Facebook 在 React.js Conf 2015 大会上推出的基于 JavaScript 的开 ...

  4. Android主流UI开源库整理(转载)

    http://www.jianshu.com/p/47a4a7b99364 标题隐含了两个层面的意思,一个是主流,另一个是UI.主流既通用,一些常规的按钮.Switch.进度条等控件都是通用控件,因此 ...

  5. Android强大的开源库与系统架构工具

    后来加上的,因为太强大了,android上百个可立即使用的开源库介绍:https://github.com/Trinea/android-open-project 一款功能强大且实用的开发工具可以为开 ...

  6. Android Library 发布开源库 JCenter & JitPack 攻略

    对于Android 的开源库,一般通过 JCenter 或者 JitPack 发布开源.两种方式均可~ 当你造了一个好玩有用的东西想要分享给大家时,开源出来便是一种好方式~ 一. 上传开源库到 JCe ...

  7. android WebP解析开源库-支持高清无损

    在我们的项目中需要支持WebP高清无损图片,推荐一个我们已经使用的解析开源库给大家:https://github.com/keshuangjie/WebpExample/tree/master/lib ...

  8. Android 开发:开源库Speex支持arm64的动态库文件

    随着处理器制造工艺的不断进步,和Android系统的不断发展,最近出了arm64-v8a的架构,由于项目中用到了speex的第三方语音编解码的动态库,其他架构的处理器暂不用说,一切正常,唯独到arm6 ...

  9. 开源库Fab-Transformation简单使用解析

    转载请注明出处王亟亟的大牛之路 相似于IPhone的悬浮按钮的操作,仅仅只是是固定的,当然经过自己的改动也能够动.这边仅仅是给伸手党一个福祉,外加加上一些自己的理解.让大家能够拿来就用.看了就懂,废话 ...

随机推荐

  1. ACMer之歌

    <<死了都要编>> 死了都要编 不动态规划不痛快 算法多深只有这样 才足够表白 死了都要编 不A星算法不痛快 宇宙毁灭星还在 把每天当成是比赛来编程 一分一秒都编到汗水掉下来 ...

  2. Linq 之 Where操作

    适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句. Where操作包括3种形式,分别为简单形式.关系条件形式 ...

  3. 直接删除mysql的日志导致mysql无法启动

    --02T08::.750000Z [Warning] [MY-] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION ...

  4. eclipse svn提交忽略文件及文件夹,ignore设置无效..

    如果之前提交过此文件,就不能设置忽略该文件了.所以第一次提交的时候要搞清楚再提交. [亲测,的确如此,用 Windows -> Preferences -> Team -> Igno ...

  5. 洛谷P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler

    P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler 题目描述 Farmer John has purchased the world's most l ...

  6. 阿里云杨敬宇:5G时代,边缘计算将发挥更大价值

    “5G时代,边缘计算将发挥更大价值.”3月8日,阿里云边缘计算技术负责人杨敬宇向媒体表示,边缘计算作为5G时代的一项关键技术,未来将成为不可或缺的基础设施之一. 5G时代万物智联将真正成为现实,但对计 ...

  7. Yii 1.0 升级 Yii 2.0

    //命名空间 use app\components\HttpException; model废除方法relations(),scopes() Yii::app() 修改成 Yii::$app $thi ...

  8. 合唱队形 ( 双向LIS )

    #include <iostream> #include <stdio.h> #include <algorithm> using namespace std; ] ...

  9. eclipse修改中文注释的字体(亲测有用!)

    Window –> Preferences –> General –> Appearance –> Colors and Fonts –> Basic –> Tex ...

  10. java jsp j2ee

    1. JavaScript用于编写嵌入在网页文档中的程序,它由浏览器负责解释和执行,可以在网页上产生动态的显示效果和实现与用户交互的功能,譬如,让一串文字跟着鼠标移动,让一个图标在网页漂浮移动,验证用 ...