您的当前位置:首页正文

滑动返回-方式1

来源:要发发知识网

写在前面的废话

  • 6月1号的时候,我想到一个做滑动返回的思路,于是在我的简书上,记录了这个思路,没有推广这个文章,还是有朋友看到了,在这里说声抱歉,整整两个月过去了,才想起来去实现它。
  • 可是没有办法啊,之前一直加班累成狗,最近又在学新的框架,是这周末才想起来试着去实现它。
  • 如果,有朋友打算用我的这个思路,抱歉,带来的问题,我可能没有时间帮你解决。。
001.png 002.gif

怎么用?

  • 方式一 通过继承 SwipeBackActivity
  • 方式二 通过使用 SwipeBackTouchHelper

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import github.alex.annotation.DispatchType;
import github.alex.helper.SwipeBackTouchHelper;

public class Scroll2Activity extends Activity {
private SwipeBackTouchHelper swipeBackTouchHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
swipeBackTouchHelper = new SwipeBackTouchHelper(this);
}
public void toast(View view){
Toast.makeText(this, "吐司 "+((ViewGroup)view.getParent()).indexOfChild(view), Toast.LENGTH_SHORT).show();
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    swipeBackTouchHelper.dispatchTouchEvent(ev);
    if(DispatchType.returnSuper.equals(swipeBackTouchHelper.dispatchType)){
        return super.dispatchTouchEvent(ev);
    }else if(DispatchType.returnTrue.equals(swipeBackTouchHelper.dispatchType)){
        return true;
    }else if(DispatchType.returnFalse.equals(swipeBackTouchHelper.dispatchType)){
        return false;
    }
    return super.dispatchTouchEvent(ev);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    /\*千万别忘了,销毁资源,防止内存泄漏\*/
    swipeBackTouchHelper.destroy();
}

}

</pre>