文章开头先注明本滑动销毁是对
http://blog.csdn.net/xiaanming/article/details/20934541
这篇博客的内容进行一个小小的改动
添加向左滑动打开另外一个activity
能够通过设置向左向右设置回调效果,这个效果是我忘设置背景透明了
如今开看一下基本的代码,略微改动了一下
具体能够开原博主的解析,解析已经非常具体了的
只是改着改着感觉这个功能有点鸡肋。跟viewpager实现的样子几乎相同了
package com.example.testsildingfinishlayout; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.ViewConfiguration; import android.view.ViewGroup; import android.widget.RelativeLayout; import android.widget.Scroller; /** * 自己定义能够滑动的RelativeLayout, 相似于IOS的滑动删除页面效果。当我们要使用 * 此功能的时候,须要将该Activity的顶层布局设置为SildingFinishLayout * 要注意:android 触摸事件分发拦截(假设子View或者子ViewGroup里面还有事件监听方法要先排除) * * @email xiaoyizong@126.com * */ public class SildingFinishLayout extends RelativeLayout{ /** * SildingFinishLayout布局的父布局 */ private ViewGroup mParentView; /** * 滑动的最小距离 */ 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; private int direction = -1; /** * 能否够向某个方向滚动 */ private boolean isSetLeft = false; private boolean isSetRight = false; private boolean isSetLeft_Right = false; /** * 是否是人工滚动回原来的地方 */ private boolean byPeople = false; 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); } /** * 1 代表向左滑动 * 2代表向右 * 3代表左右 * @param direction */ public void setSlidingDirection(int direction) { switch(direction){ case 1:isSetLeft=true;break; case 2:isSetRight=true;break; case 3:isSetLeft_Right=true;break; default:return; } } /** * 事件拦截操作 */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: downX = tempX = (int) ev.getRawX(); downY = (int) ev.getRawY(); break; case MotionEvent.ACTION_MOVE: int moveX = (int) ev.getRawX(); //满足此条件屏蔽SildingFinishLayout里面子类的touch事件 if (Math.abs(moveX - downX) > mTouchSlop && Math.abs((int) ev.getRawY() - downY) < mTouchSlop && (isSetLeft||isSetRight||isSetLeft_Right)) { System.out.println("屏蔽子类的touch事件"); return true; } break; } return super.onInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { 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) { System.out.println("isSilding == true;"); isSilding = true; } if (Math.abs(moveX - downX)>= 0 && isSilding) { System.out.println("mParentView.scrollBy("+deltaX+", 0);"); System.out.println("moveX - downX = "+(moveX - downX)); if(((moveX - downX) > 0 && isSetLeft) || ((moveX - downX) < 0 && isSetRight) || isSetLeft_Right) mParentView.scrollBy(deltaX, 0); } break; case MotionEvent.ACTION_UP: isSilding = false; System.out.println("mParentView.getScrollX() -->"+mParentView.getScrollX() ); if (mParentView.getScrollX() <= -viewWidth / 2) { isFinish = true; direction = 1; scrollRight(); } else if(mParentView.getScrollX() >= viewWidth / 2){ isFinish = true; direction=2; scrollLeft(); }else{ isFinish = false; direction=0; scrollOrigin(false); } break; } return true; } @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; } /** * 从左向右滚动出界面 */ 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 scrollLeft() { final int delta = (viewWidth - mParentView.getScrollX()); // 调用startScroll方法来设置一些滚动的參数,我们在computeScroll()方法中调用scrollTo来滚动item mScroller.startScroll(mParentView.getScrollX(), 0, delta - 1, 0, Math.abs(delta)); postInvalidate(); } /** * 滚动到起始位置 */ public void scrollOrigin(boolean byPeople) { //getScrollX() ---> Return the scrolled left position of this view(返回偏离了屏幕左边界多远 “正数(左边界|)负数”) this.byPeople = byPeople; int delta = mParentView.getScrollX(); mScroller.startScroll(mParentView.getScrollX(), 0, -delta, 0, Math.abs(delta)); postInvalidate(); } @Override public void computeScroll() { // 调用startScroll的时候scroller.computeScrollOffset()返回值为boolean。true说明滚动尚未完毕,false说明滚动已经完毕 if (mScroller.computeScrollOffset()) { mParentView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); if (mScroller.isFinished() && isFinish) { if (onSildingFinishListener != null) { if(!this.byPeople){ onSildingFinishListener.onSildingFinish(direction); } this.byPeople = false; }else{ //没有设置OnSildingFinishListener,让其滚动到事实上位置 scrollOrigin(false); isFinish = false; } } } } public interface OnSildingFinishListener { /** * 从左向右滚动出界面 type=1 * 从右向左滚动出界面 type=2 * @param type */ public void onSildingFinish(int type); } }
看一下这些activity里面的代码
第一个activity仅仅能向左滑动打开新的activity
package com.example.testsildingfinishlayout; import com.example.testsildingfinishlayout.SildingFinishLayout.OnSildingFinishListener; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { SildingFinishLayout sildingFinishLayout=null; boolean flag = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sildingFinishLayout= (SildingFinishLayout) findViewById(R.id.num1); sildingFinishLayout .setOnSildingFinishListener(new OnSildingFinishListener() { @Override public void onSildingFinish(int type) { if (type ==2){ Intent fromSpiltOuttoPostIntent = new Intent(); fromSpiltOuttoPostIntent.setClass(MainActivity.this, Num2Activity.class); startActivity(fromSpiltOuttoPostIntent); } } }); //设置仅仅能向左滑动打开新的activity sildingFinishLayout.setSlidingDirection(2); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); //当从新显示当前页面的时候把sildingFinishLayout滚回到屏幕可视范围,之前已经滚出了屏幕 if(flag) sildingFinishLayout.scrollOrigin(true);//将原来私有的滚会初始位置的方法设为public } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); flag = true; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
第二个activity能够向右滑动finish掉自己,也能够向左滑动打开第三个activity
package com.example.testsildingfinishlayout; import com.example.testsildingfinishlayout.SildingFinishLayout.OnSildingFinishListener; import android.support.v7.app.ActionBarActivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class Num2Activity extends Activity { SildingFinishLayout sildingFinishLayout=null; boolean flag = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_num2); sildingFinishLayout= (SildingFinishLayout) findViewById(R.id.num2); sildingFinishLayout .setOnSildingFinishListener(new OnSildingFinishListener() { @Override public void onSildingFinish(int type) { if (type == 1) Num2Activity.this.finish(); if (type ==2){ Intent fromSpiltOuttoPostIntent = new Intent(); fromSpiltOuttoPostIntent.setClass(Num2Activity.this, Num3Activity.class); startActivity(fromSpiltOuttoPostIntent); } } }); sildingFinishLayout.setSlidingDirection(3); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); if(flag) sildingFinishLayout.scrollOrigin(true); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); flag = true; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.num2, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
第三个activity就不贴代码了,就是那回事