优秀开源项目SwipeLayout的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Leo_eight/article/details/51379439

今天项目使用了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>// 绑定数据


下面几个跟BaseAdapter是一样的,这里不说了:

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






猜你喜欢

转载自blog.csdn.net/Leo_eight/article/details/51379439