我们做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. Hackerrank--Team Formation

    题目链接 For an upcoming programming contest, Roy is forming some teams from the n students of his unive ...

  2. c#多线程通信之委托(事件)

    在研究c# 线程之间通信时,发现传统的方法大概有三种 ①全局变量,由于同一进程下的多个进程之间共享数据空间,所以使用全局变量是最简单的方法,但要记住使用volatile进行限制. ②线程之间发送消息( ...

  3. [jnhs]netbeans使用debug模式频繁出现java.lang.OutOfMemoryError: PermGen space内存不足

    netbeans赠送的tomcat7 windows解决方法: 修改C:\Program Files\Apache Software Foundation\Apache Tomcat 8.0.27\b ...

  4. iOS -NSOperation并发编程

    http://www.cocoachina.com/game/20151201/14517.html http://blog.csdn.net/qinlicang/article/details/42 ...

  5. 【模板】倍增LCA [2017年5月计划 清北学堂51精英班 Day3]

    P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...

  6. MySQL常见数据库引擎及对比

    一:MySQL存储引擎简介 MySQL有多种存储引擎,每种存储引擎有各自的优缺点,大家可以择优选择使用:MyISAM.InnoDB.MERGE.MEMORY(HEAP).BDB(BerkeleyDB) ...

  7. windows 操作系统下git报filename too long 处理方法

    两种方法解决: 一是通过修改配置文件 [core] repositoryformatversion = filemode = false bare = false logallrefupdates = ...

  8. vuecli3取消eslint

    1.之前好好的项目,今天运行npm run serve.忽然报错运行不了了. 2.原因 在生成项目时候没有忽略 ESlint选项配置,其实建议创建项目的时候还是忽略掉这个选项比较好,因为后期写项目多空 ...

  9. linux下对拍

    #!/bin/bash t=0; while true; do let "t = $t + 1" printf $t printf ":\n" ./rand & ...

  10. CAS小总结

      11                 if (log.isTraceEnabled()) { 12                     log.trace ("Logout requ ...