Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android 左滑退出Activity

Android实现左滑退出Activity的完美封装

作者:代码君哈哈

这篇文章主要介绍了Android实现左滑退出Activity的完美封装,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1:定义一个自己的父级容器,让它继承自一个布局(LinearLayout、RelativeLayout都可以)

public class SildingFinishLayout extends RelativeLayout implements View.OnTouchListener {
  /**
   * SildingFinishLayout布局的父布局
   */
  private ViewGroup mParentView;
  /**
   * 处理滑动逻辑的View
   */
  private View touchView;
  /**
   * 滑动的最小距离
   */
  private int mTouchSlop;
  /**
   * 按下点的X坐标
   */
  private int downX;
  /**
   * 按下点的Y坐标
   */
  private int downY;
  /**
   * 临时存储X坐标
   */
  private int tempX;
  /**
   * 滑动类
   */
  private Scroller mScroller;
  /**
   * SildingFinishLayout的宽度
   */
  private int viewWidth;
  /**
   * 记录是否正在滑动
   */
  private boolean isSilding;


  private OnSildingFinishListener onSildingFinishListener;
  private boolean isFinish;


  public SildingFinishLayout(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }


  public SildingFinishLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);


    mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    mScroller = new Scroller(context);
  }


  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    if (changed) {
// 获取SildingFinishLayout所在布局的父布局
      mParentView = (ViewGroup) this.getParent();
      viewWidth = this.getWidth();
    }
  }


  /**
   * 设置OnSildingFinishListener, 在onSildingFinish()方法中finish Activity
   *
   * @param onSildingFinishListener
   */
  public void setOnSildingFinishListener(
      OnSildingFinishListener onSildingFinishListener) {
    this.onSildingFinishListener = onSildingFinishListener;
  }


  /**
   * 设置Touch的View
   *
   * @param touchView
   */
  public void setTouchView(View touchView) {
    this.touchView = touchView;
    touchView.setOnTouchListener(this);
  }


  public View getTouchView() {
    return touchView;
  }


  /**
   * 滚动出界面
   */
  private void scrollRight() {
    final int delta = (viewWidth + mParentView.getScrollX());
// 调用startScroll方法来设置一些滚动的参数,我们在computeScroll()方法中调用scrollTo来滚动item
    mScroller.startScroll(mParentView.getScrollX(), 0, -delta + 1, 0,
        Math.abs(delta));
    postInvalidate();
  }


  /**
   * 滚动到起始位置
   */
  private void scrollOrigin() {
    int delta = mParentView.getScrollX();
    mScroller.startScroll(mParentView.getScrollX(), 0, -delta, 0,
        Math.abs(delta));
    postInvalidate();
  }


  /**
   * touch的View是否是AbsListView, 例如ListView, GridView等其子类
   *
   * @return
   */
  private boolean isTouchOnAbsListView() {
    return touchView instanceof AbsListView ? true : false;
  }


  /**
   * touch的view是否是ScrollView或者其子类
   *
   * @return
   */
  private boolean isTouchOnScrollView() {
    return touchView instanceof ScrollView ? true : false;
  }


  @Override
  public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        downX = tempX = (int) event.getRawX();
        downY = (int) event.getRawY();
        break;
      case MotionEvent.ACTION_MOVE:
        int moveX = (int) event.getRawX();
        int deltaX = tempX - moveX;
        tempX = moveX;
        if (Math.abs(moveX - downX) > mTouchSlop
            && Math.abs((int) event.getRawY() - downY) < mTouchSlop) {
          isSilding = true;


// 若touchView是AbsListView,
// 则当手指滑动,取消item的点击事件,不然我们滑动也伴随着item点击事件的发生
          if (isTouchOnAbsListView()) {
            MotionEvent cancelEvent = MotionEvent.obtain(event);
            cancelEvent
                .setAction(MotionEvent.ACTION_CANCEL
                    | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
            v.onTouchEvent(cancelEvent);
          }


        }


        if (moveX - downX >= 0 && isSilding) {
          mParentView.scrollBy(deltaX, 0);


// 屏蔽在滑动过程中ListView ScrollView等自己的滑动事件
          if (isTouchOnScrollView() || isTouchOnAbsListView()) {
            return true;
          }
        }
        break;
      case MotionEvent.ACTION_UP:
        isSilding = false;
        if (mParentView.getScrollX() <= -viewWidth / 2) {
          isFinish = true;
          scrollRight();
        } else {
          scrollOrigin();
          isFinish = false;
        }
        break;
    }


// 假如touch的view是AbsListView或者ScrollView 我们处理完上面自己的逻辑之后
// 再交给AbsListView, ScrollView自己处理其自己的逻辑
    if (isTouchOnScrollView() || isTouchOnAbsListView()) {
      return v.onTouchEvent(event);
    }


// 其他的情况直接返回true
    return true;
  }


  @Override
  public void computeScroll() {
// 调用startScroll的时候scroller.computeScrollOffset()返回true,
    if (mScroller.computeScrollOffset()) {
      mParentView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
      postInvalidate();


      if (mScroller.isFinished()) {


        if (onSildingFinishListener != null && isFinish) {
          onSildingFinishListener.onSildingFinish();
        }
      }
    }
  }

  public interface OnSildingFinishListener {
    public void onSildingFinish();
  }
}

2.Acitity

在Activity的onCreate()方法里面  

 @Override
  public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    SildingFinishLayout mSildingFinishLayout = (SildingFinishLayout) findViewById(R.id.timed_task_SildingFinishLayout);
mSildingFinishLayout
    mSildingFinishLayout .setOnSildingFinishListener(new SildingFinishLayout.OnSildingFinishListener() {
      @Override
      public void onSildingFinish() {
        finish();
      }
    });
    mSildingFinishLayout .setTouchView(mSildingFinishLayout);//绑定底层的布局就可以了 :不起作用的话换个你activity 布局里面view就可以啦 
  }

截止目前,这个功能已经实现了

解决问题:这个会出现左滑 背景白色问题

1.style.xml中中增加

<style name="AppTheme" parent="android:Theme.Light">
  <item name="android:windowIsTranslucent">true</item>
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowBackground">@android:color/transparent</item>
</style>

2 acvitiy 的属性中加上

android:theme="@style/AppTheme" 

3 一些属性的详解

<item name="android:windowFrame">@null</item> :Dialog的windowFrame框为无
<item name="android:windowIsFloating">true</item>:是否浮现在activity之上
<item name="android:windowIsTranslucent">false</item>:是否半透明
<item name="android:windowNoTitle">true</item>:是否显示title
<item name="android:windowBackground">@drawable/dia_bg</item>:设置dialog的背景
<item name="android:backgroundDimEnabled">false</item>: 背景是否透明显示

到此这篇关于Android实现左滑退出Activity的完美封装的文章就介绍到这了,更多相关Android 左滑退出Activity内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文