androidSwipeLayout简单用法,仿qq会话列表listview左右滑动

1.概述

AndroidSwipeLayout,开发者是代码家, AndroidSwipeLayout 是一个支持ListView, GridView, ViewGroup等等左右上下滑动出操作菜单, 类似 qq 消息列表向左滑动显示出多某条信息的操作菜单

2.配置

在模块中添加:

compile 'com.daimajia.swipelayout:library:1.2.0'

如果自己的项目中有v4包可以使用如下的加载方式

compile ('com.daimajia.swipelayout:library:1.2.0'){
        exclude module: 'support-v4'
    }

3.基本使用


1. Show Mode

  • LayDown(亲试效果不是特别好)
  • PullOut(和默认效果一样)

2. Drag edge

  • LEFT
  • RIGHT
  • TOP
  • BOTTOM

3. 在xml布局文件中使用SwipeLayout

  • SwipeLayout的最后一个孩子是SurfaceView,其他孩子都是BottomView
  • BottomView最好添加上andorid:layout_gravity属性
 <com.daimajia.swipe.SwipeLayoutandroid:layout_width="match_parent"android:layout_height="80dp"><!-- Bottom View Start--><LinearLayoutandroid:background="#66ddff00"android:id="@+id/bottom_wrapper"android:layout_width="160dp"android:layout_height="match_parent"android:orientation="vertical"><!--What you want to show--><TextViewandroid:text="欢迎关注我的微信公众号:Android技术漫谈"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout><!-- Bottom View End--><!-- Surface View Start --><LinearLayoutandroid:padding="10dp"android:background="#ffffff"android:layout_width="match_parent"android:layout_height="match_parent"><!--What you want to show in SurfaceView--><TextViewandroid:text="欢迎关注我的github:lavor-zl"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout><!-- Surface View End --></com.daimajia.swipe.SwipeLayout>


4. 在java文件中对SwipeLayout进行一些相关操作

SwipeLayout swipeLayout =  (SwipeLayout)findViewById(R.id.sample1);//set show mode.
swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);//add drag edge.(If the BottomView has 'layout_gravity' attribute, this line is unnecessary)
swipeLayout.addDrag(SwipeLayout.DragEdge.Left, findViewById(R.id.bottom_wrapper));swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {@Overridepublic void onClose(SwipeLayout layout) {//when the SurfaceView totally cover the BottomView.}@Overridepublic void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {//you are swiping.}@Overridepublic void onStartOpen(SwipeLayout layout) {}@Overridepublic void onOpen(SwipeLayout layout) {//when the BottomView totally show.}@Overridepublic void onStartClose(SwipeLayout layout) {}@Overridepublic void onHandRelease(SwipeLayout layout, float xvel, float yvel) {//when user's hand released.}});

5.在Listview中使用

1).设置adapter适配器MySwipeAdapter

public class MySwipeAdapter extends BaseSwipeAdapter {List<MySwipeBean> items;Context context;public MySwipeAdapter(Context context, List<MySwipeBean> items) {this.context = context;this.items = items;}@Overridepublic int getSwipeLayoutResourceId(int i) {return R.id.sl_content;}@Overridepublic View generateView(final int i, ViewGroup viewGroup) {View view = View.inflate(context, R.layout.list_item_layout, null);return view;}@Overridepublic void fillValues(final int i, View view) {TextView tv = (TextView) view.findViewById(R.id.tv);final CheckBox cb_swipe_tag1 = (CheckBox) view.findViewById(R.id.cb_swipe_tag1);TextView tv_swipe_delect1 = (TextView) view.findViewById(R.id.tv_swipe_delect1);TextView tv_swipe_top1 = (TextView) view.findViewById(R.id.tv_swipe_top1);final SwipeLayout sl_content = (SwipeLayout) view.findViewById(R.id.sl_content);ImageView iv_myice= (ImageView) view.findViewById(R.id.iv_myice);sl_content.setShowMode(SwipeLayout.ShowMode.PullOut);BitmapUtils bitmapUtils=new BitmapUtils(context);bitmapUtils.display(iv_myice,items.get(i).getIce());tv_swipe_delect1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(context, items.get(i).getContent() + i, Toast.LENGTH_SHORT).show();items.remove(i);notifyDataSetChanged();sl_content.close();}});cb_swipe_tag1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(context,items.get(i).getContent()+i,Toast.LENGTH_SHORT).show();if (cb_swipe_tag1.isChecked()) {items.get(i).setTag(true);notifyDataSetChanged();sl_content.close();} else {items.get(i).setTag(false);notifyDataSetChanged();sl_content.close();}}});tv_swipe_top1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(context,items.get(i).getContent()+i,Toast.LENGTH_SHORT).show();
//                items.add(items.get(position));items.add(0, items.get(i));items.remove(i + 1);notifyDataSetChanged();sl_content.close();}});tv.setText(items.get(i).getContent());if (items.get(i).isTag()) {tv.setTextColor(Color.parseColor("#ff0000"));cb_swipe_tag1.setText("取消标记");} else {tv.setTextColor(Color.parseColor("#000000"));cb_swipe_tag1.setText("标记");}}@Overridepublic int getCount() {return items.size();}@Overridepublic Object getItem(int position) {return items.get(position);}@Overridepublic long getItemId(int position) {return position;}class ViewHolder{TextView tv;}
}


1.发现MySwipeAdapter继承自BaseSwipeAdapter,
BaseSwipeAdapter继承自BaseAdapter。

2.getCount,getItem,getItemId函数实现和BaseAdapter中一样
3.getSwipeLayoutResourced需要返回一个Swipelayout的id,便于对其的处理
4.generateView函数用于生成一个View,和BaseAdapter的getView的用法基本一样,不同的是在这里可以不处理view的复用,因为BaseSwipeAdapter中已经封装了view的复用处理
5.fillValues在这个函数中填充数据

adapter的.xml文件

<?xml version="1.0" encoding="utf-8"?>
<com.daimajia.swipe.SwipeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/sl_content"android:layout_width="match_parent"android:layout_height="120dp"><LinearLayoutandroid:layout_width="270dp"android:layout_height="match_parent"android:orientation="horizontal"><TextViewandroid:id="@+id/tv_swipe_delect1"android:layout_width="0dp"android:layout_height="match_parent"android:gravity="center"android:textColor="#ff0000"android:textSize="20dp"android:layout_weight="1"android:text="删除" /><CheckBoxandroid:id="@+id/cb_swipe_tag1"android:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="1"android:textColor="#00ff00"android:gravity="center"android:textSize="20dp"android:background="@android:color/transparent"android:button="@android:color/transparent"android:text="标记"/><TextViewandroid:id="@+id/tv_swipe_top1"android:layout_width="0dp"android:layout_height="match_parent"android:textColor="#0000ff"android:textSize="20dp"android:gravity="center"android:layout_weight="1"android:text="置顶" /></LinearLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="120dp"><ImageViewandroid:layout_width="60dp"android:layout_height="60dp"android:layout_centerVertical="true"android:id="@+id/iv_myice"/><TextViewandroid:id="@+id/tv"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerInParent="true"android:gravity="center"android:textColor="@color/black"android:textSize="16sp" /></RelativeLayout></com.daimajia.swipe.SwipeLayout>

2).Activity

这块没什么好说的直接上代码就可以了

public class MySwipeActivity extends BaseActivity {@ViewInject(R.id.lv_myswipe)private ListView lv_myswipe;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_my_swipe);ViewUtils.inject(this);init();}private void init() {List<MySwipeBean> lists=new ArrayList<MySwipeBean>();lists.add(new MySwipeBean("测试侧滑模块1",false,"http://b.hiphotos.baidu.com/image/h%3D200/sign=e3572ad35e2c11dfc1d1b82353276255/342ac65c103853437a7c6a119a13b07eca80884b.jpg"));lists.add(new MySwipeBean("测试侧滑模块2",false,"http://v1.qzone.cc/avatar/201404/10/09/30/5345f41e670ec580.jpg%21200x200.jpg"));lists.add(new MySwipeBean("测试侧滑模块3",false,"http://img3.duitang.com/uploads/item/201502/21/20150221162526_Krfzk.jpeg"));lists.add(new MySwipeBean("测试侧滑模块4",false,"http://www.qqxoo.com/uploads/allimg/161105/22335BQ7-14.jpg"));lists.add(new MySwipeBean("测试侧滑模块5",false,"http://www.qqxoo.com/uploads/allimg/161105/22335C296-15.jpg"));lists.add(new MySwipeBean("测试侧滑模块6",false,"http://pic.wenwen.soso.com/p/20161030/20161030141349-583191339.jpg"));lists.add(new MySwipeBean("测试侧滑模块7",false,"http://p3.gexing.com/G1/M00/B1/31/rBACFFP0wguj59JTAAAiBWiMllU953_200x200_3.jpg?recache=20131108"));lists.add(new MySwipeBean("测试侧滑模块8",false,"http://e.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=44348f00357adab43d851347bee49f2a/cc11728b4710b91274d1cad6c0fdfc0392452281.jpg"));lists.add(new MySwipeBean("测试侧滑模块9",false,"http://img2.a0bi.com/upload/ttq/20161105/1478327376930.jpg"));lists.add(new MySwipeBean("测试侧滑模块10",false,"http://img1.2345.com/duoteimg/qqTxImg/2013/12/ns/18-024824_754.jpg"));lists.add(new MySwipeBean("测试侧滑模块11",false,"http://v1.qzone.cc/avatar/201407/01/12/53/53b23ebb14c27312.jpg%21200x200.jpg"));lists.add(new MySwipeBean("测试侧滑模块12",false,"http://img2.a0bi.com/upload/ttq/20161105/1478326897529.jpg"));lists.add(new MySwipeBean("测试侧滑模块13",false,"http://img3.a0bi.com/upload/ttq/20161105/1478326994480.jpg"));lists.add(new MySwipeBean("测试侧滑模块14",false,"http://img3.a0bi.com/upload/ttq/20161105/1478327290562.jpg"));lists.add(new MySwipeBean("测试侧滑模块15",false,"http://img1.imgtn.bdimg.com/it/u=1546847952,3875672549&fm=21&gp=0.jpg"));lists.add(new MySwipeBean("测试侧滑模块16",false,"http://b.hiphotos.baidu.com/image/h%3D200/sign=e3572ad35e2c11dfc1d1b82353276255/342ac65c103853437a7c6a119a13b07eca80884b.jpg"));lists.add(new MySwipeBean("测试侧滑模块17",false,"http://v1.qzone.cc/avatar/201404/10/09/30/5345f41e670ec580.jpg%21200x200.jpg"));lists.add(new MySwipeBean("测试侧滑模块18",false,"http://img3.duitang.com/uploads/item/201502/21/20150221162526_Krfzk.jpeg"));lists.add(new MySwipeBean("测试侧滑模块19",false,"http://www.qqxoo.com/uploads/allimg/161105/22335BQ7-14.jpg"));lists.add(new MySwipeBean("测试侧滑模块20",false,"http://www.qqxoo.com/uploads/allimg/161105/22335C296-15.jpg"));lists.add(new MySwipeBean("测试侧滑模块21",false,"http://pic.wenwen.soso.com/p/20161030/20161030141349-583191339.jpg"));lists.add(new MySwipeBean("测试侧滑模块22",false,"http://p3.gexing.com/G1/M00/B1/31/rBACFFP0wguj59JTAAAiBWiMllU953_200x200_3.jpg?recache=20131108"));lists.add(new MySwipeBean("测试侧滑模块23",false,"http://e.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=44348f00357adab43d851347bee49f2a/cc11728b4710b91274d1cad6c0fdfc0392452281.jpg"));lists.add(new MySwipeBean("测试侧滑模块24",false,"http://img2.a0bi.com/upload/ttq/20161105/1478327376930.jpg"));lists.add(new MySwipeBean("测试侧滑模块25",false,"http://img1.2345.com/duoteimg/qqTxImg/2013/12/ns/18-024824_754.jpg"));lists.add(new MySwipeBean("测试侧滑模块26",false,"http://v1.qzone.cc/avatar/201407/01/12/53/53b23ebb14c27312.jpg%21200x200.jpg"));lists.add(new MySwipeBean("测试侧滑模块27",false,"http://img2.a0bi.com/upload/ttq/20161105/1478326897529.jpg"));lists.add(new MySwipeBean("测试侧滑模块28",false,"http://img3.a0bi.com/upload/ttq/20161105/1478326994480.jpg"));lists.add(new MySwipeBean("测试侧滑模块29",false,"http://img3.a0bi.com/upload/ttq/20161105/1478327290562.jpg"));lists.add(new MySwipeBean("测试侧滑模块30",false,"http://img1.imgtn.bdimg.com/it/u=1546847952,3875672549&fm=21&gp=0.jpg"));MySwipeAdapter adapter=new MySwipeAdapter(MySwipeActivity.this,lists);lv_myswipe.setAdapter(adapter);adapter.setMode(Attributes.Mode.Single);}
}


本人用的是Xutils框架,以注入的方式获取的id

ViewUtils.inject(this);

@ViewInject(R.id.lv_myswipe)
private ListView lv_myswipe;

Activity的.XML

<RelativeLayout 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"><ListViewandroid:id="@+id/lv_myswipe"android:layout_width="fill_parent"android:layout_height="fill_parent"></ListView></RelativeLayout>

MySwipeBean

/*** Created by Administrator on 2016/11/11 0011.* user:tyk*/
public class MySwipeBean {String content;boolean isTag;String ice;public MySwipeBean(String content, boolean isTag, String ice) {this.content = content;this.isTag = isTag;this.ice = ice;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public boolean isTag() {return isTag;}public void setTag(boolean isTag) {this.isTag = isTag;}public String getIce() {return ice;}public void setIce(String ice) {this.ice = ice;}
}

然后就大功告成,是不是很简单呢,看一下效果图



Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注