滑动删除的部分主要包含两个部分, 一个是内容区域(用于放置正常显示的view),另一个是操作区域(用于放置删除按钮)。默认情况下,操作区域是不显示的,内容区域的大小是填充整个容 器,操作区域始终位于内容区域的右面。当开始滑动的时候,整个容器中的所有子view都像左滑动,如果操作区域此时是不可见的,设置为可见。
实现思路就是自定义一个layout swipelayout继承自framelayout。swipelayout包含两个子view,第一个子view是内容区域,第二个子view是操作 区域。滑动效果的控制,主要就是通过检测swipelayout的touch事件来实现,android support库里其实已经提供了viewdraghelper来进行监听touch事件。
1、首先需要对linearlayout进行重载
具体分析看注解
package com.example.mac.agriculturemanagement;
import android.content.context;
import android.support.annotation.nullable;
import android.support.v4.view.viewcompat;
import android.support.v4.widget.viewdraghelper;
import android.util.attributeset;
import android.view.motionevent;
import android.view.view;
import android.widget.linearlayout;
/**
* created by mac on 2017/6/15.
*/
//条目滑动效果
public class slidelayout extends linearlayout {
private viewdraghelper mdraghelper;
private view contentview;
private view actionview;
private int dragdistance;
private final double auto_open_speed_limit = 800.0;
private int draggedx;
public slidelayout(context context) {
super(context);
init();
}
public slidelayout(context context, @nullable attributeset attrs) {
super(context, attrs);
init();
}
public slidelayout(context context, @nullable attributeset attrs, int defstyleattr) {
super(context, attrs, defstyleattr);
init();
}
//初始化
public void init (){
mdraghelper = viewdraghelper.create(this, new draghelpercallback());
}
@override
public boolean callonclick() {
return super.callonclick();
}
/*当你触摸屏幕,移动的时候,就会回调这个方法。
它会返回两个参数。第一个参数,就是你触摸的那个控件。
第二个就是id。
返回值又代表什么呢?返回ture,就是代笔允许拖动这个控件。
返回false就代表不允许拖动这个控件.。这里我只允许拖动主控件。*/
//把容器的事件处理委托给viewdraghelper对象
@override
public boolean onintercepttouchevent(motionevent event) {
if (mdraghelper.shouldintercepttouchevent(event)) {
return true;
}
return super.onintercepttouchevent(event);
}
@override
public boolean ontouchevent(motionevent event) {
mdraghelper.processtouchevent(event);
return true;
}
@override
protected void onfinishinflate() {
contentview = getchildat(0);
actionview = getchildat(1);
actionview.setvisibility(gone);
}
//设置拖动的距离为actionview的宽度
@override
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
super.onmeasure(widthmeasurespec, heightmeasurespec);
dragdistance = actionview.getmeasuredwidth();
//system.out.println("righttop"+actionview.gettop());
}
private class draghelpercallback extends viewdraghelper.callback {
//用来确定contentview和actionview是可以拖动的
@override
public boolean trycaptureview(view view, int i) {
return view == contentview || view == actionview;
}
//被拖动的view位置改变的时候调用,如果被拖动的view是contentview,
// 我们需要在这里更新actionview的位置
@override
public void onviewpositionchanged(view changedview, int left, int top, int dx, int dy) {
draggedx = left;
if (changedview == contentview) {
actionview.offsetleftandright(dx);
} else {
contentview.offsetleftandright(dx);
}
//actionview 是否可见
//0 -------- visible 可见
//4 -------- invisible 不可见但是占用布局空间
//8 -------- gone 不可见也不占用布局空间
if (actionview.getvisibility() == view.gone) {
actionview.setvisibility(view.visible);
}
if (left==25)
{
actionview.setvisibility(view.gone);
}
invalidate(); //刷新view
}
//用来限制view在x轴上拖动
//@override
public int clampviewpositionhorizontal(view child, int left, int dx) {
if (child == contentview) {
final int leftbound = getpaddingleft();
final int minleftbound = -leftbound - dragdistance;
final int newleft = math.min(math.max(minleftbound, left), 25);
//system.out.println("content "+newleft);
return newleft;
} else {
//getmeasuredwidth()获取全部长度 包括隐藏的
final int minleftbound = getpaddingleft() + contentview.getmeasuredwidth() - dragdistance;
final int maxleftbound = getpaddingleft() + contentview.getmeasuredwidth() + getpaddingright();
final int newleft = math.min(math.max(left, minleftbound), maxleftbound);
system.out.println("action "+newleft);
return newleft;
}
}
@override
public int clampviewpositionvertical(view child, int top, int dy) {
//system.out.println("top "+top);
if(top!=25)
{
top=25;
}
return top;
}
//用来限制view可以拖动的范围
//@override
public int getviewhorizontaldragrange(view child) {
return dragdistance;
}
@override
public int getviewverticaldragrange(view child) {
return 0;
}
//根据滑动手势的速度以及滑动的距离来确定是否显示actionview。
// smoothslideviewto方法用来在滑动手势之后实现惯性滑动效果
//@override
public void onviewreleased(view releasedchild, float xvel, float yvel) {
super.onviewreleased(releasedchild, xvel, yvel);
boolean settletoopen = false;
if (xvel > auto_open_speed_limit) {
settletoopen = false;
} else if (xvel < -auto_open_speed_limit) {
settletoopen = true;
} else if (draggedx <= -dragdistance / 2) {
settletoopen = true;
} else if (draggedx > -dragdistance / 2) {
settletoopen = false;
}
final int settledestx = settletoopen ? -dragdistance : 0;
mdraghelper.smoothslideviewto(contentview, settledestx, 0);
viewcompat.postinvalidateonanimation(slidelayout.this);
}
}
}
因为我给我的linearlayout设置了外边距,所以在向左滑动的过程,出现上下的滑动,并且该条目的原始位置也偏移。为了解决该问题,首先需要根据自己设置的margin值来修改一下的数据
将onviewpositionchanged中添加
if (left==25)
{
actionview.setvisibility(view.gone);
}
修改为适合的数据,来防止右侧的滑块不隐藏
再添加上
public int clampviewpositionvertical(view child, int top, int dy) {
//system.out.println("top "+top);
if(top!=25)
{
top=25;
}
return top;
}
来限制其上下移动 top的值依旧需要自己琢磨
2、编写布局文件
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="10dp"
android:background="@drawable/text_border"
android:elevation="3dp"
android:orientation="vertical">
android:id="@+id/mark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="textview"
android:textsize="40dp" />
android:id="@+id/marksquare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="textview"
android:textsize="20dp" />
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#f0f0f0"
android:layout_margintop="10dp"
>
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
android:id="@+id/showinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignparenttop="true"
android:layout_marginleft="5dp"
android:layout_toendof="@+id/textview6"
android:layout_torightof="@+id/textview6"
android:text="详细信息" />
android:id="@+id/textview6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignparentleft="true"
android:layout_alignparentstart="true"
android:layout_alignparenttop="true"
android:text="删除" />
具体效果
但目前还存在一个问题
listview每一个条目的点击事件和滑动事件不能共存。网上说是因为事件的触发是逐层向下传递到进行处理该事件的部件,再逐层向上返 回处理结果。
以上所述是小编给大家介绍的android 实现左滑出现删除选项,希望对大家有所帮助