转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/28508769

今天看别人项目,看到别人使用了SwipeListView,Google一把,果然github上的,也参考了csdn上的几篇文章,然后自己写了个例子,分享给大家。

效果图:

嗯,看一眼SwipeListView的参数的设置:

If you decide to use SwipeListView as a view, you can define it in your xml layout like this:

<com.fortysevendeg.swipelistview.SwipeListView
xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:id="@+id/example_lv_list"
android:listSelector="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
swipe:swipeFrontView="@+id/front"
swipe:swipeBackView="@+id/back"
swipe:swipeActionLeft="[reveal | dismiss]"
swipe:swipeActionRight="[reveal | dismiss]"
swipe:swipeMode="[none | both | right | left]"
swipe:swipeCloseAllItemsWhenMoveList="[true | false]"
swipe:swipeOpenOnLongPress="[true | false]"
swipe:swipeAnimationTime="[miliseconds]"
swipe:swipeOffsetLeft="[dimension]"
swipe:swipeOffsetRight="[dimension]"
/>

  

  • swipeFrontView - Required - front view id. 即ListView Item正常显示的控件Id,且必须与Item的布局文件中的控件id一样
  • swipeBackView - Required - back view id.  手指滑动时显示的,隐藏在FrontView后面,且必须与item的布局文件中控件Id一样
  • swipeActionLeft - Optional - left swipe action Default: 'reveal'  左滑的动作,默认reveal,即显示BackView,还有dismiss,choice会触发响应的方法。
  • swipeActionRight - Optional - right swipe action Default: 'reveal' 同上
  • swipeMode - Gestures to enable or 'none'. Default: 'both' 设置左滑、右滑、都支持
  • swipeCloseAllItemsWhenMoveList - Close revealed items on list motion. Default: 'true' 当滚动listview时,关闭所有展开的Item,最好不要设置为false,由于item的复用,false存在一些问题。
  • swipeOpenOnLongPress - Reveal on long press Default: 'true' 长按时触发显示
  • swipeAnimationTime - item drop animation time. Default: android configuration 动画时间长度
  • swipeOffsetLeft - left offset 左偏移量
  • swipeOffsetRight - right offset 右偏移量
基本属性都介绍了,下面上例子:
1、布局文件
<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:background="@color/white"
android:clickable="true"
android:orientation="vertical" xmlns:swipe="http://schemas.android.com/apk/res/com.example.zhy_swipelistview02"> <include
layout="@layout/main_title"
android:focusable="true" /> <FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" > <com.fortysevendeg.swipelistview.SwipeListView
android:id="@+id/id_swipelistview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
swipe:swipeActionLeft="reveal"
swipe:swipeBackView="@+id/id_back"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeFrontView="@+id/id_front"
swipe:swipeMode="left"
swipe:swipeOffsetLeft="200dip"
swipe:swipeOpenOnLongPress="false" /> <TextView
android:id="@+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableTop="@drawable/no_chat"
android:text="木有联系人"
android:textColor="#ffb7b7b7"
android:textSize="14.0sp"
android:visibility="gone" />
</FrameLayout> <requestFocus /> </LinearLayout>

  item的布局文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dp" > <LinearLayout
android:id="@+id/id_back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffcccccc"
android:gravity="center|right" > <Button
android:id="@+id/id_remove"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="4dp"
android:background="@drawable/red_button"
android:text="Delete"
android:textColor="#fff" >
</Button>
</LinearLayout> <LinearLayout
android:id="@+id/id_front"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff" > <TextView
android:id="@+id/id_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000"
android:textSize="25sp" >
</TextView>
</LinearLayout> </FrameLayout>

  

注意对应布局的id和swipeListView中的frontView和backView的Id一致。
 
2、MainActivity
package com.example.zhy_swipelistview02;

import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window; import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;
import com.fortysevendeg.swipelistview.SwipeListView; public class MainActivity extends Activity
{ protected static final String TAG = "Activity";
private SwipeListView mSwipeListView;
private DataAdapter mAdapter;
private List<String> mDatas; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); initDatas(); mSwipeListView = (SwipeListView) findViewById(R.id.id_swipelistview);
mAdapter = new DataAdapter(this, mDatas , 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 initDatas()
{
mDatas = new ArrayList<String>();
for (int i = 'A'; i <= 'Z'; i++)
mDatas.add((char) i + "");
} }

  Adapter:

package com.example.zhy_swipelistview02;

import java.util.List;

import com.fortysevendeg.swipelistview.SwipeListView;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView; public class DataAdapter extends BaseAdapter
{ private List<String> mDatas;
private LayoutInflater mInflater;
private SwipeListView mSwipeListView ; public DataAdapter(Context context, List<String> datas , SwipeListView swipeListView)
{
this.mDatas = datas;
mInflater = LayoutInflater.from(context);
mSwipeListView = swipeListView;
} @Override
public int getCount()
{
return mDatas.size();
} @Override
public Object getItem(int position)
{
return mDatas.get(position);
} @Override
public long getItemId(int position)
{
return position;
} @Override
public View getView(final int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.list_item, null); TextView tv = (TextView) convertView.findViewById(R.id.id_text);
Button del = (Button) convertView.findViewById(R.id.id_remove);
tv.setText(mDatas.get(position));
del.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mDatas.remove(position);
notifyDataSetChanged();
/**
* 关闭SwipeListView
* 不关闭的话,刚删除位置的item存在问题
* 在监听事件中onListChange中关闭,会出现问题
*/
mSwipeListView.closeOpenedItems();
}
}); return convertView;
} }

  代码相当简单,MainActivity里面设置了监听事件,可以使用Demo的时候,观察触发的事件的输出,如果有特殊需求可以做一些操作。

demo下载地址

http://www.eoeandroid.com/thread-328894-1-1.html

http://files.cnblogs.com/liaolandemengxiang/swipelistview_Demo.rar

Android listview 侧滑 SwipeListView 详解 实现微信,QQ等滑动删除效果的更多相关文章

  1. android listview 替代品recyclerview详解

    安卓v7支持包下的ListView替代品————RecyclerView   RecyclerView这个控件也出来很久了,相信大家也学习的差不多了,如果还没学习的,或许我可以带领大家体验一把这个艺术 ...

  2. SwipeListView 详解 实现微信,QQ等滑动删除效果

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/28508769 今天看别人项目,看到别人使用了SwipeListView,Goog ...

  3. SwipeListView 具体解释 实现微信,QQ等滑动删除效果

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/28508769 今天看别人项目,看到别人使用了SwipeListView,Goog ...

  4. 【转】Android 实现ListView的滑动删除效果

    http://www.cnblogs.com/weixiao870428/p/3524055.html http://download.csdn.net/download/love_javc_you/ ...

  5. ANDROID L——Material Design详解(UI控件)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! Android L: Google已经确认Android L就是Android Lolli ...

  6. Android中Service(服务)详解

    http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...

  7. Xamarin android CardView的使用详解

    android 5.0新增加的一个控件CardView,在support v7兼容包中,意思就是卡片View,虽然可以设置阴影,圆角等等样式,但是我们也可以自己写出来,谷歌工程师之所以出这个,肯定是帮 ...

  8. 转:给 Android 开发者的 RxJava 详解

    转自:  http://gank.io/post/560e15be2dca930e00da1083 评注:多图解析,但是我还是未看懂. 前言 我从去年开始使用 RxJava ,到现在一年多了.今年加入 ...

  9. 《Android NFC 开发实战详解 》简介+源码+样章+勘误ING

    <Android NFC 开发实战详解>简介+源码+样章+勘误ING SkySeraph Mar. 14th  2014 Email:skyseraph00@163.com 更多精彩请直接 ...

随机推荐

  1. NC 5系查询引擎做报表

    在集团下打开查询引擎管理节点,选中查询设计,鼠标移动到创建,点击文件夹 文件夹名字按需求起,创好文件夹后选中该文件夹后鼠标移动到创建,点击对象. 按需求起好编码和名称 都创建好后,点击SQL手工设计 ...

  2. 爬虫初窥day4:requests

      Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2 模块提供了你所需要的大多数 ...

  3. oracle 视图带参数

    -- create or replace package p_view_param is --参数一 function set_ID(num number) return number; functi ...

  4. easyui validate -- radio、checkbox 校验扩展,事件域名

    事件域名: $(dom).on('click.myNameSpace',function(){ ... }),其中‘.myNameSpace’便是域名: 目前作用:$(dom).off('click. ...

  5. optimizer_switch引起的诡异问题

    参数描述 MySQL中不同的版本优化器会有很多新特性,比如MRR.BKA等,optimizer_switch这个参数就是控制查询优化器怎样使用这些特性.很多情况下我们会根据自身的需求去设置optimi ...

  6. 【转】mysql查看表空间占用情况

    ${database} 为数据库的名称 /*1.查看索引 (1)单位是GB*/ SELECT CONCAT(ROUND(SUM(index_length)/(**), ), ' GB') AS 'To ...

  7. WebSocket的原理与优缺点

    websocket 是长连接,受网络限制比较大,需要处理好重连,比如用户进电梯或电信用户打个电话网断了,这时候就需要重连,如果 ws 一直重连不上,有些较复杂的业务方会不愿意的,是不是还要搞个 htt ...

  8. Tomcat入门

    1.JavaWeb概念 Java web,是用java技术来解决相关web互联网领域的技术的总称.web包括:web服务器和web客户端两部分.java在最早web客户端的应用有java applet ...

  9. ibatis注意要点

    一.ibatis的关键字like查询 select * from t_student where s_name '%张%'; 这种like语句在ibatis中怎么写,他们现在的项目是用ibatis作为 ...

  10. UVa 10294 Arif in Dhaka (First Love Part 2) (Polya定理)

    题意:给定 n 和 m 表示要制作一个项链和手镯,项链和手镯的区别就是手镯旋转和翻转都是相同的,而项链旋转都是相同的,而翻转是不同的,问你使用 n 个珠子和 m 种颜色可以制作多少种项链和手镯. 析: ...