下班回来,在github上学习了SwipeRevealLayout侧滑库,实践写了些代码,感觉棒棒哒。
作者库的地址:
https://github.com/chthai64/SwipeRevealLayout
首先来看作者的效果
效果很明确:
1.支持上下左右四个方向的侧滑。
2.支持recylerview,listview,gradview,是不是很棒吧。
3.两种拖动模式
{
case 3.1:
正常(二次视图是在主视图下);
break;
case 3.2:
同一级(二级视图贴在主视图的边缘);
break;
}
4.最低支持api4,超级爽。
首先来看看实现方式吧
添加依赖:
dependencies { compile 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.0.0' }
或者添加Module
个人支持第二种,因为这样我们不仅能够看到源代码,如果库不维护了,项目也不会受到影响。
布局设置
<com.chauthai.swipereveallayout.SwipeRevealLayout android:layout_width="match_parent" android:layout_height="match_parent" app:mode="same_level" app:dragEdge="left">
<!-- Your secondary layout here -->
<FrameLayout android:layout_width="wrap_content" android:layout_height="match_parent" />
<!-- Your main layout here -->
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" />
</com.chauthai.swipereveallayout.SwipeRevealLayout>
这里作者讲的很清楚
app:mode=”same_level”表示拖动模式
app:dragEdge=”left”表示拖动方向
第一个FrameLayout是二级布局,就是滑动展示的页面,第一个FrameLayout是主布局。
如下所示:
<?xml version="1.0" encoding="utf-8"?>
<com.chauthai.swipereveallayout.SwipeRevealLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/swipe_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" app:dragEdge="right" app:mode="same_level">
<FrameLayout android:id="@+id/delete_layout" android:layout_width="wrap_content" android:layout_height="70dp" android:background="#ffcc0000">
<TextView android:layout_width="70dp" android:layout_height="match_parent" android:gravity="center" android:background="@android:color/holo_red_dark" android:textColor="@android:color/white" android:text="Delete"/>
</FrameLayout>
<FrameLayout android:layout_width="match_parent" android:layout_height="70dp" android:background="@android:color/holo_blue_light">
<TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:gravity="center" android:layout_gravity="center" android:textColor="@android:color/white"/>
</FrameLayout>
</com.chauthai.swipereveallayout.SwipeRevealLayout>
DemoAdapter中如下设置
package com.example.wangchang.testswipereveallayout;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.chauthai.swipereveallayout.SwipeRevealLayout;
import com.chauthai.swipereveallayout.ViewBinderHelper;
import java.util.ArrayList;
/** * Created by WangChang on 2016/4/25. */
public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.BaseViewHolder> {
private ArrayList<String> dataList = new ArrayList<>();
private final ViewBinderHelper binderHelper = new ViewBinderHelper();
public void replaceAll(ArrayList<String> list) {
dataList.clear();
if (list != null) {
dataList.addAll(list);
}
notifyDataSetChanged();
}
@Override
public DemoAdapter.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new DemoViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
}
@Override
public void onBindViewHolder(DemoAdapter.BaseViewHolder holder, int position) {
holder.setData(dataList.get(position));
}
@Override
public int getItemCount() {
return dataList == null ? 0 : dataList.size();
}
/** * Only if you need to restore open/close state when the orientation is changed. * Call this method in {@link android.app.Activity#onSaveInstanceState(Bundle)} */
public void saveStates(Bundle outState) {
binderHelper.saveStates(outState);
}
/** * Only if you need to restore open/close state when the orientation is changed. * Call this method in {@link android.app.Activity#onRestoreInstanceState(Bundle)} */
public void restoreStates(Bundle inState) {
binderHelper.restoreStates(inState);
}
public class BaseViewHolder extends RecyclerView.ViewHolder {
public BaseViewHolder(View itemView) {
super(itemView);
}
void setData(Object data) {
}
}
private class DemoViewHolder extends BaseViewHolder {
private SwipeRevealLayout swipeLayout;
private View deleteLayout;
private TextView textView;
public DemoViewHolder(View view) {
super(view);
swipeLayout = (SwipeRevealLayout) itemView.findViewById(R.id.swipe_layout);
deleteLayout = itemView.findViewById(R.id.delete_layout);
textView = (TextView) itemView.findViewById(R.id.text);
}
@Override
void setData(Object data) {
super.setData(data);
if (data != null && !data.equals("")) {
String content = (String) data;
binderHelper.bind(swipeLayout, content);
deleteLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataList.remove(getAdapterPosition());
notifyItemRemoved(getAdapterPosition());
}
});
textView.setText(content);
}
}
}
}
注意:1.saveStates,restoreStates保存滑动状态
2. binderHelper.bind(swipeLayout, content);将布局跟data绑定
这里很重要
MainActivity设置
private DemoAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recylerView = (RecyclerView) findViewById(R.id.recylerView);
recylerView.setHasFixedSize(true);
recylerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
recylerView.setAdapter(adapter = new DemoAdapter());
adapter.replaceAll(getData());
}
private ArrayList<String> getData() {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
list.add("你可以滑动试试看?" + i);
}
return list;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Only if you need to restore open/close state when
// the orientation is changed
if (adapter != null) {
adapter.saveStates(outState);
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Only if you need to restore open/close state when
// the orientation is changed
if (adapter != null) {
adapter.restoreStates(savedInstanceState);
}
}
这里主要是填充数据,回调saveStates,restoreStates方法。
然后讲解一下有用的方法
setSwipeListener设置监听器类
swipeLayout.setSwipeListener(new SwipeRevealLayout.SwipeListener() {
@Override
public void onClosed(SwipeRevealLayout swipeRevealLayout) {
}
@Override
public void onOpened(SwipeRevealLayout swipeRevealLayout) {
}
@Override
public void onSlide(SwipeRevealLayout swipeRevealLayout, float v) {
}
});
open(boolean animation), close(boolean animation),打开关闭动画,如果动画被设置为false,监听则不会被调用。
setMinFlingVelocity(int velocity),设置打开关闭的速率
setDragEdge(int edge),设置滑动方向。
setLockDrag(boolean lock),是否锁住滑动,如果设置为true,则不会滑动布局
源码:
http://pan.baidu.com/s/1skXwoGH
又是一点多了,睡觉,不要问我是谁,我只是小小的github搬运工,哈哈!