优秀开源项目SwipeLayout的使用
来源:互联网 发布:戴森无叶风扇 知乎 编辑:程序博客网 时间:2024/04/30 11:33
今天项目使用了SwipeLayout开源项目,就是像QQ侧滑删除效果的第三方库,支持上下左右划出布局,并支持所有组件!非常强大!在这里写下它的简单使用,当作自己的笔记。
这里用Listview来举例。
首先写布局文件list_item.xml:
<strong><span style="font-family:SimHei;font-size:14px;color:#009900;"><?xml version="1.0" encoding="utf-8" ?><com.daimajia.swipe.SwipeLayout xmlns:swipe="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipe_layout" android:layout_width="match_parent" android:layout_height="wrap_content" swipe:leftEdgeSwipeOffset="0dp" swipe:rightEdgeSwipeOffset="0dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="80dp" android:background="#FF5534" android:gravity="center"> <ImageView android:id="@+id/delete" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/trash" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete Item?" android:textSize="19sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/item_selector"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="match_parent" android:text="item" /> </LinearLayout></com.daimajia.swipe.SwipeLayout></span></strong>
注意: 默认是SwipeLayout的第一个子布局作为SwipeLayout!对于上面的布局来说,也就是第一个LinearLayout作为SwipeLayout。
接着实现Adapter:
作者已经帮我们封装好各种Adapter了,我们只需实现继承之并实现它的方法就好了。
这里拿继承了BaseAdapter的BaseSwipeAdapter的举例:
需要实现一下几个抽象方法:
public abstract int getSwipeLayoutResourceId(int position); // 返回SwipeLayout的id
public abstract View generateView(int position, ViewGroup parent);<span style="white-space:pre"></span>// 生成list_item布局文件
public abstract void fillValues(int position, View convertView);<span style="white-space:pre"></span>// 绑定数据
public int getCount();public Object getItem(int position);public long getItemId(int position);
你会发现,不要重写getView()方法,原来是BaseSwipeAdapter已经重写了gerView方法,并用final修饰,在里面调用generateView()和fillValues():
public final View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if(v == null){ v = generateView(position, parent); } mItemManger.bind(v, position); fillValues(position, v); return v;}
三个方法的实现:
@Override public int getSwipeLayoutResourceId(int position) { return R.id.swipe_layout; } @Override public View generateView(int position, ViewGroup parent) { View v = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null); SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position)); swipeLayout.addSwipeListener(new SimpleSwipeListener() { @Override public void onOpen(SwipeLayout layout) { // SwipeLayout划出时调用 } }); swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() { @Override public void onDoubleClick(SwipeLayout layout, boolean surface) { // SwipeLayout双击时调用 } }); v.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 监听SwipeLayout中的组件的点击事件 } }); return v; } @Override public void fillValues(int position, View convertView) { TextView t = (TextView)convertView.findViewById(R.id.text_view);<span style="white-space:pre"></span>// 绑定数据 t.setText("item" + position); }
可以看到,可以直接在Adapter里监听SwipeLayout的事件!!
最后在Activity里的操作跟平常的ListView操作没什么区别了,这里再说说几个常用的方法:
swipeLayout.getSurfaceView();<span style="white-space:pre"></span>// 可以获取表面的View,既是普通状态下的布局
swipeLayout.addDrag(SwipeLayout.DragEdge.Left, swipeLayout.findViewById(R.id.wrapper));<span style="white-space:pre"></span>// 设置SwipeLayout在左边相应的还可以设置上下右:只需把SwipeLayout.DragEdge.Leftt改成相应的Top、Bottom和Right就好。
好吧,我就了解到这里了,有不懂得去看作者的代码吧。
项目地址:AndroidSwipeLayout
1 0
- 优秀开源项目SwipeLayout的使用
- swipelayout的基本使用
- SwipeLayout的简单使用
- 优秀的开源项目
- 优秀的开源项目
- 优秀的开源项目
- 优秀的开源项目
- github使用发现优秀的开源项目
- SwipeLayout在ListView中使用的使用的问题
- 优秀开源项目
- 优秀开源项目
- 优秀开源项目
- 优秀开源项目
- 一些优秀的开源项目
- 优秀的Java开源项目
- 七个优秀的开源J2ME项目
- 七个优秀的开源J2ME项目
- android 优秀的开源项目
- pandas 作图无法显示中文
- NOJ - 1009 2的N次方
- 屏幕分辨率等级
- 单链表C/C++实现
- PHP中的流程控制
- 优秀开源项目SwipeLayout的使用
- NOJ - 2070 马尔扎哈的疑惑
- HTML5书写规范
- 关于“幽灵架构”的补充说明3:为什么不会产生“循环引用”
- ssh中jndi的配置(struts2.4.1+spring4.1.5+4.3.10+tomcat7)
- NOJ - 1093 阶乘之和
- hadoop集群搭建HDFS、HA、 YARN
- Scala学习9之产生随机数并写入到文件中
- 零编程经验的我是如何找到工作的