先上图看效果
https://github.com/chthai64/SwipeRevealLayoutgithub地址: https://github.com/chthai64/SwipeRevealLayout
这是简单做了一个item右侧滑动删除演示,还有其他类似,可以前往github查看
studio中的build.gradle文件中添加依赖
dependencies {
compile 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.4.1'
}
Layout 文件
<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>
我的 layout 文件
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/x10"
android:layout_marginBottom="@dimen/x10"
android:orientation="horizontal"
app:mode="same_level"
app:dragEdge="right">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="@dimen/x105"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/x10"
android:layout_marginBottom="@dimen/x10"
android:layout_marginLeft="@dimen/x20"
app:cardCornerRadius="10dp">
<LinearLayout
android:id="@+id/ll_item_delete"
android:layout_width="match_parent"
android:layout_height="@dimen/x215"
android:background="#E32C2C"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="@dimen/x26"
android:textColor="@color/white"
android:gravity="center"
android:text="删\n除"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/x10"
android:layout_marginBottom="@dimen/x10"
android:visibility="visible"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EFF6FF"
android:orientation="vertical"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/x45"
android:layout_marginLeft="@dimen/x30"
android:layout_marginRight="@dimen/x30"
android:layout_marginTop="@dimen/x20"
android:layout_marginBottom="@dimen/x20"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="@dimen/x30"
android:layout_height="@dimen/x30"
android:text="1"
android:textColor="@color/white"
android:gravity="center"
android:textSize="@dimen/x20"
android:background="@drawable/button_bg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/x20"
android:text="预灌"
android:textColor="@color/black"
android:textSize="@dimen/x26" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/iv_gg"
android:layout_width="@dimen/x65"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textSize="@dimen/x26"
android:text="分钟"
android:textColor="@color/black"/>
<EditText
android:layout_width="@dimen/x125"
android:layout_height="match_parent"
android:textSize="@dimen/x26"
android:layout_toLeftOf="@id/iv_gg"
android:layout_marginRight="@dimen/x20"
android:background="@drawable/two_shuifei_bg"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/x45"
android:layout_marginLeft="@dimen/x30"
android:layout_marginRight="@dimen/x30"
android:layout_marginBottom="@dimen/x20"
android:gravity="center_vertical"
android:orientation="horizontal">
<View
android:layout_width="@dimen/x30"
android:layout_height="@dimen/x30"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/x20"
android:text="施肥"
android:textColor="@color/black"
android:textSize="@dimen/x26" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/iv_sf"
android:layout_width="@dimen/x65"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textSize="@dimen/x26"
android:text="分钟"
android:textColor="@color/black"/>
<EditText
android:layout_width="@dimen/x125"
android:layout_height="match_parent"
android:textSize="@dimen/x26"
android:layout_toLeftOf="@id/iv_sf"
android:layout_marginRight="@dimen/x20"
android:background="@drawable/two_shuifei_bg"/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/x45"
android:layout_marginLeft="@dimen/x30"
android:layout_marginRight="@dimen/x30"
android:layout_marginBottom="@dimen/x20"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/x50"
android:text="施肥管道"
android:textColor="@color/black"
android:textSize="@dimen/x26" />
<RelativeLayout
android:layout_width="@dimen/x210"
android:layout_height="match_parent"
android:background="@drawable/two_shuifei_bg"
android:layout_alignParentRight="true">
<ImageView
android:id="@+id/iv_b1"
android:layout_width="@dimen/x65"
android:layout_height="@dimen/x25"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="@mipmap/back_black"
android:layout_marginRight="@dimen/x10"
android:rotation="90"/>
<TextView
android:layout_width="@dimen/x125"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="@color/black"
android:gravity="center"
android:textSize="@dimen/x24"
android:text="请选择"/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</FrameLayout>
</com.chauthai.swipereveallayout.SwipeRevealLayout>
看布局,外面用到的SwipeRevealLayout包裹完,前面FrameLayout是滑动删除,后面FrameLayout是正常的item
我的Activity代码
List<String> two_task_list = new ArrayList<>();
for (int i = 0; i < 6; i++) {
two_task_list.add("虫子" + i);
}
TwoTaskDetailsAdapter twoTaskDetailsAdapter = new TwoTaskDetailsAdapter(R.layout.item_two_water_task_details,two_task_list);
RecyclerView rv_two_task = helper.getView(R.id.rv_two_task);
GridLayoutManager layoutManager = new GridLayoutManager(mContext, 1);
rv_two_task.setLayoutManager(layoutManager);
rv_two_task.setAdapter(twoTaskDetailsAdapter);
我的Adapter代码
public class TwoTaskDetailsAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
public TwoTaskDetailsAdapter(int layoutResId, @Nullable List<String> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, String item) {
LinearLayout ll_item_delete = (LinearLayout) helper.getView(R.id.ll_item_delete);
ll_item_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtil.s("删除:"+item);
}
});
}
}
至此就大功告成了,赶紧去试试吧~铁子
网友评论