本文共 1689 字,大约阅读时间需要 5 分钟。
写在前面的废话
如果你对事件分发机制了解不够清楚,请阅读我写的 ,废话不说,先看效果图
怎么用?
<pre>
package alex.com.viewdraghelper;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;/**
* 作者:Alex * 时间:2016年07月30日 22:47 * 博客: */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(); }@Overridepublic 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);}@Overrideprotected void onDestroy() { super.onDestroy(); /\*千万别忘了,销毁资源,防止内存泄漏\*/ swipeBackTouchHelper.destroy();}
}
</pre>
最后奉上源码 和
转载地址:http://trhga.baihongyu.com/