Android实现左滑退出Activity的完美封装
1:定义一个自己的父级容器,让它继承自一个布局(LinearLayout、RelativeLayout都可以)
publicclassSildingFinishLayoutextendsRelativeLayoutimplementsView.OnTouchListener{
/**
*SildingFinishLayout布局的父布局
*/
privateViewGroupmParentView;
/**
*处理滑动逻辑的View
*/
privateViewtouchView;
/**
*滑动的最小距离
*/
privateintmTouchSlop;
/**
*按下点的X坐标
*/
privateintdownX;
/**
*按下点的Y坐标
*/
privateintdownY;
/**
*临时存储X坐标
*/
privateinttempX;
/**
*滑动类
*/
privateScrollermScroller;
/**
*SildingFinishLayout的宽度
*/
privateintviewWidth;
/**
*记录是否正在滑动
*/
privatebooleanisSilding;
privateOnSildingFinishListeneronSildingFinishListener;
privatebooleanisFinish;
publicSildingFinishLayout(Contextcontext,AttributeSetattrs){
this(context,attrs,0);
}
publicSildingFinishLayout(Contextcontext,AttributeSetattrs,intdefStyle){
super(context,attrs,defStyle);
mTouchSlop=ViewConfiguration.get(context).getScaledTouchSlop();
mScroller=newScroller(context);
}
@Override
protectedvoidonLayout(booleanchanged,intl,intt,intr,intb){
super.onLayout(changed,l,t,r,b);
if(changed){
//获取SildingFinishLayout所在布局的父布局
mParentView=(ViewGroup)this.getParent();
viewWidth=this.getWidth();
}
}
/**
*设置OnSildingFinishListener,在onSildingFinish()方法中finishActivity
*
*@paramonSildingFinishListener
*/
publicvoidsetOnSildingFinishListener(
OnSildingFinishListeneronSildingFinishListener){
this.onSildingFinishListener=onSildingFinishListener;
}
/**
*设置Touch的View
*
*@paramtouchView
*/
publicvoidsetTouchView(ViewtouchView){
this.touchView=touchView;
touchView.setOnTouchListener(this);
}
publicViewgetTouchView(){
returntouchView;
}
/**
*滚动出界面
*/
privatevoidscrollRight(){
finalintdelta=(viewWidth+mParentView.getScrollX());
//调用startScroll方法来设置一些滚动的参数,我们在computeScroll()方法中调用scrollTo来滚动item
mScroller.startScroll(mParentView.getScrollX(),0,-delta+1,0,
Math.abs(delta));
postInvalidate();
}
/**
*滚动到起始位置
*/
privatevoidscrollOrigin(){
intdelta=mParentView.getScrollX();
mScroller.startScroll(mParentView.getScrollX(),0,-delta,0,
Math.abs(delta));
postInvalidate();
}
/**
*touch的View是否是AbsListView,例如ListView,GridView等其子类
*
*@return
*/
privatebooleanisTouchOnAbsListView(){
returntouchViewinstanceofAbsListView?true:false;
}
/**
*touch的view是否是ScrollView或者其子类
*
*@return
*/
privatebooleanisTouchOnScrollView(){
returntouchViewinstanceofScrollView?true:false;
}
@Override
publicbooleanonTouch(Viewv,MotionEventevent){
switch(event.getAction()){
caseMotionEvent.ACTION_DOWN:
downX=tempX=(int)event.getRawX();
downY=(int)event.getRawY();
break;
caseMotionEvent.ACTION_MOVE:
intmoveX=(int)event.getRawX();
intdeltaX=tempX-moveX;
tempX=moveX;
if(Math.abs(moveX-downX)>mTouchSlop
&&Math.abs((int)event.getRawY()-downY)=0&&isSilding){
mParentView.scrollBy(deltaX,0);
//屏蔽在滑动过程中ListViewScrollView等自己的滑动事件
if(isTouchOnScrollView()||isTouchOnAbsListView()){
returntrue;
}
}
break;
caseMotionEvent.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()){
returnv.onTouchEvent(event);
}
//其他的情况直接返回true
returntrue;
}
@Override
publicvoidcomputeScroll(){
//调用startScroll的时候scroller.computeScrollOffset()返回true,
if(mScroller.computeScrollOffset()){
mParentView.scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
postInvalidate();
if(mScroller.isFinished()){
if(onSildingFinishListener!=null&&isFinish){
onSildingFinishListener.onSildingFinish();
}
}
}
}
publicinterfaceOnSildingFinishListener{
publicvoidonSildingFinish();
}
}
2.Acitity
在Activity的onCreate()方法里面
@Override
publicvoidonCreate(@NullableBundlesavedInstanceState,@NullablePersistableBundlepersistentState){
super.onCreate(savedInstanceState,persistentState);
SildingFinishLayoutmSildingFinishLayout=(SildingFinishLayout)findViewById(R.id.timed_task_SildingFinishLayout);
mSildingFinishLayout
mSildingFinishLayout.setOnSildingFinishListener(newSildingFinishLayout.OnSildingFinishListener(){
@Override
publicvoidonSildingFinish(){
finish();
}
});
mSildingFinishLayout.setTouchView(mSildingFinishLayout);//绑定底层的布局就可以了:不起作用的话换个你activity布局里面view就可以啦
}
截止目前,这个功能已经实现了
解决问题:这个会出现左滑背景白色问题
1.style.xml中中增加
true true @android:color/transparent
2acvitiy的属性中加上
android:theme="@style/AppTheme"
3一些属性的详解
@null:Dialog的windowFrame框为无 true:是否浮现在activity之上 false:是否半透明 true:是否显示title @drawable/dia_bg:设置dialog的背景 false:背景是否透明显示
到此这篇关于Android实现左滑退出Activity的完美封装的文章就介绍到这了,更多相关Android左滑退出Activity内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!