android 左滑删除自动恢复,Android 实现左滑出现删除选项

滑动删除的部分主要包含两个部分, 一个是内容区域(用于放置正常显示的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="删除" />

具体效果

437b29a843b749315c846ea347e995c2.png

但目前还存在一个问题

listview每一个条目的点击事件和滑动事件不能共存。网上说是因为事件的触发是逐层向下传递到进行处理该事件的部件,再逐层向上返 回处理结果。

以上所述是小编给大家介绍的android 实现左滑出现删除选项,希望对大家有所帮助

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android全局拖动的悬浮按钮是一种在应用界面上悬浮的按钮,用户可以通过拖动按钮的方式,在任意界面使用其提供的功能。这种悬浮按钮通常会呈现在屏幕的某个固定位置,如屏幕右下角。 使用全局拖动的悬浮按钮可以为用户提供方便和快捷的操作方式。用户只需通过轻触按钮即可快速打开某个应用或执行某个功能,而不需要返回主界面或搜索相应的功能选项。这样可以大大提高用户的操作效率和体验。 开发这一功能的关键在于如何实现悬浮按钮在全局范围内的拖动和点击事件。一种常用的方法是通过在Android系统的WindowManager中创建一个可拖动的View,并设置其触摸事件监听器来实现。在触摸事件监听器中,我们可以处理按钮的拖动、点击、长按等各种事件。 为了使全局拖动的悬浮按钮更好地融入应用界面,我们可以对其进行自定义设置。例如,可以自定义按钮的形状、颜色、动画效果等,以适配不同的应用主题和风格。 需要注意的是,在设计和使用全局拖动的悬浮按钮时,我们要遵循用户界面设计的基本原则,避免对用户的正常操作造成干扰和困扰。同时,也要考虑到移动设备的屏幕尺寸和分辨率的差异,以确保全局拖动的悬浮按钮在不同设备上都能够正常显示和操作。 总而言之,Android全局拖动的悬浮按钮是一种方便用户操作的功能,通过简单轻松的拖动和点击,用户可以快速访问应用的各种功能。开发者可以根据应用的需求和用户体验考虑,来设计和实现这一功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值