欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android实现简单左滑删除控件

程序员文章站 2022-07-06 16:50:01
本文为大家分享了一个简单的android左滑删除控件,供大家参考,具体内容如下 import android.animation.valueanimator;...

本文为大家分享了一个简单的android左滑删除控件,供大家参考,具体内容如下

import android.animation.valueanimator;
import android.content.context;
import android.graphics.pointf;
import android.support.v4.view.viewconfigurationcompat;
import android.util.attributeset;
import android.view.motionevent;
import android.view.view;
import android.view.viewconfiguration;
import android.view.viewgroup;
 
public class swipelayout extends viewgroup{
 public static string tag = "swipelayout";
 
 //可以滚动的距离
 int mswipewidth;
 
 
 pointf firstpoint;
 pointf lastpoint;
 
 float mtouchslop;
 
 valueanimator openanimator;
 valueanimator closeanimator;
 
 public swipelayout(context context) {
  this(context,null);
 }
 
 public swipelayout(context context, attributeset attrs) {
  super(context, attrs);
  mtouchslop = viewconfigurationcompat.getscaledpagingtouchslop(viewconfiguration.get(getcontext()));
 }
 
 
 
 @override
 protected void onlayout(boolean changed, int l, int t, int r, int b) {
  int left=0;
  int childcount = getchildcount();
 
  for (int i=0;i<childcount;++i){
   view child = getchildat(i);
 
   //按顺序从左往右排
//   if (i==0){
//    child.layout(0,0,child.getmeasuredwidth(),child.getmeasuredheight());
//   }else {
    child.layout(left,0,left+child.getmeasuredwidth(),child.getmeasuredheight());
//   }
   left += child.getmeasuredwidth();
  }
 
 
 }
 
 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  super.onmeasure(widthmeasurespec, heightmeasurespec);
  int childcount = getchildcount();
  view mainchild = getchildat(0);
  int width=0;
  int height=0;
  mswipewidth = 0;
//  measurechild(mainchild,widthmeasurespec,heightmeasurespec);
  measure(widthmeasurespec,heightmeasurespec);
 
  //滑动距离是 从index开始 所有控件的宽度之和
  if (childcount>1) {
   for (int i = 1; i < childcount; ++i) {
    mswipewidth += getchildat(i).getmeasuredwidth();
   }
  }
 
 
  int widthmode = measurespec.getmode(widthmeasurespec);
  int widthvalue = measurespec.getsize(widthmeasurespec);
  int heightmode = measurespec.getmode(heightmeasurespec);
  int heightvalue = measurespec.getsize(heightmeasurespec);
 
  switch (heightmode){
   case measurespec.at_most:
   case measurespec.unspecified:
    //没有指定大小 按照第一个子控件的大小来设置
    height = mainchild.getmeasuredheight();
    break;
   case measurespec.exactly:
    height = heightvalue;
    break;
  }
  switch (widthmode){
   case measurespec.at_most:
   case measurespec.unspecified:
    //没有指定大小 按照第一个子控件的大小来设置
    width = mainchild.getmeasuredwidth();
    break;
   case measurespec.exactly:
    width = widthvalue;
    break;
  }
 
//  for (int i=1;i<childcount;++i){
//   measurechild(getchildat(i),widthmeasurespec,measurespec.makemeasurespec(height,measurespec.exactly));
//  }
  setmeasureddimension(width,height);
 }
 
 
 @override
 public boolean dispatchtouchevent(motionevent ev) {
  return super.dispatchtouchevent(ev);
 }
 
 @override
 public boolean onintercepttouchevent(motionevent ev) {
    switch (ev.getaction()){
   case motionevent.action_down:
    firstpoint = new pointf(ev.getx(),ev.gety());
    lastpoint = new pointf(ev.getx(),ev.gety());
    break;
   case motionevent.action_move:
    float movedistance = ev.getx()-firstpoint.x;
 
    //移动距离大于制定值 认为进入控件的滑动模式
    if (math.abs(movedistance) > mtouchslop ){
     //让父控件不拦截我们的事件
     getparent().requestdisallowintercepttouchevent(true);
     //拦截事件
     return true;
    }
 
  }
  return super.onintercepttouchevent(ev);
 }
 
 @override
 public boolean ontouchevent(motionevent ev) {
  switch (ev.getaction()){
   case motionevent.action_move:
    float movedistance = ev.getx()-lastpoint.x;
    lastpoint = new pointf(ev.getx(),ev.gety());
 
    // 这里要注意 x大于0的时候 往左滑动 小于0往右滑动
    scrollby((int) -movedistance ,0);
 
    //边界判定 超过了边界 直接设置为边界值
    if (getscrollx()> mswipewidth){
     scrollto(mswipewidth,0);
    }else if (getscrollx()<0){
     scrollto(0,0);
    }
    break;
   case motionevent.action_up:
    //没动 不理他
    if (getscrollx()== mswipewidth ||getscrollx()==0){
     return false;
    }
     float distance = ev.getx()-firstpoint.x;
    //滑动距离超过 可滑动距离指定值 继续完成滑动
     if (math.abs(distance) > mswipewidth *0.3 ){
      if (distance>0){
       smoothclose();
      }else if (distance<0){
       smoothopen();
      }
     }else {
      if (distance>0){
       smoothopen();
 
      }else if (distance<0){
       smoothclose();
      }
     }
     return true;
  }
 
  return super.ontouchevent(ev);
 }
 
 public void smoothopen(){
 
  clearanimator();
  openanimator = valueanimator.ofint(getscrollx(), mswipewidth);
  openanimator.addupdatelistener(new valueanimator.animatorupdatelistener() {
   @override
   public void onanimationupdate(valueanimator animation) {
    integer integer = (integer) animation.getanimatedvalue();
    scrollto(integer,0);
   }
  });
  openanimator.start();
 }
 public void smoothclose(){
  clearanimator();
  closeanimator = valueanimator.ofint(getscrollx(),0);
  closeanimator.addupdatelistener(new valueanimator.animatorupdatelistener() {
   @override
   public void onanimationupdate(valueanimator animation) {
    integer integer = (integer) animation.getanimatedvalue();
    scrollto(integer,0);
   }
  });
  closeanimator.start();
 
 }
 
 public void open(){
  scrollto(mswipewidth,0);
 }
 public void close(){
  scrollto(0,0);
 
 }
//执行滑动动画必须先清除动画 不然会鬼畜
 private void clearanimator(){
  if (closeanimator!=null && closeanimator.isrunning()){
   closeanimator.cancel();
   closeanimator = null;
  }
  if (openanimator!=null && openanimator.isrunning()) {
   openanimator.cancel();
   openanimator = null;
  }
 }
 
 public void toggle(){
  if (getscrollx()==0){
   open();
  }else {
   close();
  }
 }
 
}

使用

<com.example.chenweiqi.simplerefreshview.widget.swipelayout
  android:id="@+id/swipelayout"
  android:layout_width="200dp"
  android:layout_height="wrap_content"
  android:background="#f3f3f3"
>
<button
  android:id="@+id/btn"
  android:text="123"
  android:layout_width="match_parent"
  android:layout_height="50dp" />
 
<button
  android:background="#ff0000"
  android:text="shanchu"
  android:layout_width="80dp"
  android:layout_height="match_parent" />
<textview
  android:gravity="center"
  android:textalignment="center"
  android:background="#0f0"
  android:text="123"
  android:layout_width="30dp"
  android:layout_height="match_parent" />
</com.example.chenweiqi.simplerefreshview.widget.swipelayout>

效果

android实现简单左滑删除控件

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。