最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android實現(xiàn)簡單左滑刪除控件

 更新時間:2020年09月23日 09:18:49   作者:qq_20352713  
這篇文章主要為大家詳細介紹了android實現(xiàn)一個簡單左滑刪除控件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文為大家分享了一個簡單的android左滑刪除控件,供大家參考,具體內(nèi)容如下

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;
  //滑動距離超過 可滑動距離指定值 繼續(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);
 
 }
//執(zhí)行滑動動畫必須先清除動畫 不然會鬼畜
 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>

效果

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • android studio集成unity導出工程的實現(xiàn)

    android studio集成unity導出工程的實現(xiàn)

    本文主要介紹了android studio集成unity導出工程的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-05-05
  • Android中Fragment相互切換間不被回收的實現(xiàn)方法

    Android中Fragment相互切換間不被回收的實現(xiàn)方法

    這篇文章主要給大家介紹了關于Android中Fragment相互切換間不被回收的實現(xiàn)方法,文中給出了詳細的示例代碼和注釋供大家參考學習,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-08-08
  • Android仿微信底部菜單欄功能顯示未讀消息數(shù)量

    Android仿微信底部菜單欄功能顯示未讀消息數(shù)量

    這篇文章主要介紹了Android仿微信底部菜單欄功能,并顯示未讀消息數(shù)量,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android startActivityForResult實例詳解

    Android startActivityForResult實例詳解

    這篇文章主要介紹了Android startActivityForResult實例詳解的相關資料,需要的朋友可以參考下
    2017-05-05
  • android 大圖片拖拽并縮放實現(xiàn)原理

    android 大圖片拖拽并縮放實現(xiàn)原理

    android 大圖片拖拽縮放有利于用戶體驗,在開發(fā)過程中經(jīng)常使用到,這篇圖片拖拽縮放也是我在項目中用到的,今天整理一下,將源碼奉獻給大家,希望對大家以后碰到相似的問題有幫助
    2013-01-01
  • android針對json數(shù)據(jù)解析方法實例分析

    android針對json數(shù)據(jù)解析方法實例分析

    這篇文章主要介紹了android針對json數(shù)據(jù)解析方法,以實例形式較為詳細的分析了Android操作json格式數(shù)據(jù)的各種常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Flutter使用socketIo實現(xiàn)實時通訊

    Flutter使用socketIo實現(xiàn)實時通訊

    本文主要介紹了Flutter使用socketIo實現(xiàn)實時通訊,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-07-07
  • Android自定義View用切圖顯示字符串

    Android自定義View用切圖顯示字符串

    這篇文章主要為大家詳細介紹了Android自定義View用切圖顯示字符串,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • android通過led實現(xiàn)手電筒功能

    android通過led實現(xiàn)手電筒功能

    這篇文章主要為大家詳細介紹了android通過led實現(xiàn)手電筒功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Android中WebView實現(xiàn)點擊超鏈接啟動QQ的方法

    Android中WebView實現(xiàn)點擊超鏈接啟動QQ的方法

    這篇文章主要給大家介紹了在Android中WebView如何實現(xiàn)點擊超鏈接啟動QQ的方法,文中給出了詳細的示例代碼,相信對大家的學習或者工作具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-04-04

最新評論

游戏| 沙河市| 治多县| 五寨县| 金川县| 土默特左旗| 元江| 克拉玛依市| 正镶白旗| 宁武县| 巩留县| 连城县| 余庆县| 彭泽县| 班玛县| 桑植县| 泾源县| 巴塘县| 全椒县| 碌曲县| 集安市| 开鲁县| 永安市| 通河县| 含山县| 神农架林区| 汶上县| 江门市| 长岭县| 巴彦淖尔市| 云和县| 肇源县| 宁城县| 万源市| 衡南县| 含山县| 弥勒县| 小金县| 巴彦淖尔市| 镇雄县| 虞城县|