zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

android开发教程之实现滑动关闭fragment示例

Android教程开发 实现 示例 关闭 滑动 fragment
2023-06-13 09:15:19 时间

主要代码:(有注释)

复制代码代码如下:


packagecom.example.checkboxtest;

importandroid.annotation.SuppressLint;
importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.util.AttributeSet;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.Toast;

publicclassMyViewextendsViewGroup{
   ViewleftView=null;
   ViewrightView=null;

   publicMyView(Contextcontext,AttributeSetattrs){
       super(context,attrs);
       Viewview=newView(context,attrs);
       view.setBackgroundColor(Color.BLACK);
       this.addView(view,0);
   }

   /**
    *测量
    */
   @SuppressLint("DrawAllocation")
   @Override
   protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
       if(getChildCount()!=2){
           try{
               //自定义Exception
               thrownewException(){
                   @Override
                   publicvoidprintStackTrace(){
                       System.err.println("MyView中只能存在一个View");
                       super.printStackTrace();
                   }

               };
           }catch(Exceptione){
               e.printStackTrace();
           }
       }
       leftView=getChildAt(0);
       //设置leftview的高和宽
       leftView.measure(widthMeasureSpec,heightMeasureSpec);
       rightView=getChildAt(1);
       //设置rightview的高和宽
       rightView.measure(widthMeasureSpec,heightMeasureSpec);
       super.onMeasure(widthMeasureSpec,heightMeasureSpec);
   }

   /**
    *布局
    */
   @SuppressLint("NewApi")
   @Override
   protectedvoidonLayout(booleanchanged,intl,intt,intr,intb){
       System.out.println(l+","+t+","+r+","+b);
       if(leftView!=null&rightView!=null){
           //设置leftview的位置,在屏幕右侧(初始不可见)
           leftView.layout(-r,0,0,b);
           //设置rightView的位置,在屏幕中
           rightView.layout(l,t,r,b);
       }
   }

   @Override
   protectedvoidonDraw(Canvascanvas){
       super.onDraw(canvas);

   }

   @SuppressLint("NewApi")
   @Override
   publicbooleanonTouchEvent(MotionEventevent){
       finalfloatX=event.getX();
       floatY=event.getY();
       switch(event.getAction()){

       caseMotionEvent.ACTION_MOVE:
           System.out.println("X:"+X);
           if(X<100){
               scrollTo(0,0);
           }elseif(X>rightView.getMeasuredWidth()-100){//当用户滑动至离右边缘100时,页面关闭
               newThread(newRunnable(){//新建线程,滑动关闭
                           @Override
                           publicvoidrun(){
                               for(inti=0;;i++){
                                   try{
                                       Thread.sleep(10);//rightView每隔10ms右移3,保证滑动流畅
                                   }catch(InterruptedExceptione){
                                       e.printStackTrace();
                                   }
                                   intlen=(int)(X+3*i);
                                   //System.out.println("len:"+len);
                                   Messagemessage=newMessage();//android中非UI线程不允许直接操作控件,可以将消息发送至主线程的handler类
                                   if(len>=rightView.getMeasuredWidth()){
                                       message.what=1;
                                       handler.sendMessage(message);//发送消息
                                                                       //关闭View
                                       break;
                                   }else{
                                       message.what=0;//发送消息自动滑动
                                       handler.sendMessage(message);
                                   }
                               }
                           }
                       }).start();
           }else{
               scrollTo((int)-X,0);
               //计算透明度信息
               floatalpha=(float)(1.0-(float)(1.0/400)*X);
               //System.out.println("alpha:"+al);
               //设置透明度
               leftView.setAlpha(alpha);
           }
           break;
       }
       //设置true,消费event事件,不在向外传递
       returntrue;
   }

   @SuppressLint("HandlerLeak")
   Handlerhandler=newHandler(){
       @SuppressLint("NewApi")
       @Override
       publicvoidhandleMessage(Messagemsg){
           if(msg.what==0){
               scrollBy(-3,0);//viewgroup向右滑动3
           }elseif(msg.what==1){
               Toast.makeText(getContext(),"关闭",50).show();
               setVisibility(View.GONE);//设置viewgroup不可见(隐藏)
           }
       }

   };
}

使用:

复制代码代码如下:


<com.example.checkboxtest.MyView
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">

       <View
           android:id="@+id/rightview"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:background="#ff0000"/>
   </com.example.checkboxtest.MyView>

要显示底下的activity界面需要使用Fragment的add方法

复制代码代码如下:
//新建一个Fragment
FragmentbFragment=newBFragment();
getFragmentManager().beginTransaction().add(R.id.fragment,bFragment).commit();