Implementation of the android development tutorial slides off the fragment example

  • 2020-05-27 07:12:44
  • OfStack

Main code :(with comments)


package com.example.checkboxtest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MyView extends ViewGroup {
    View leftView = null;
    View rightView = null;
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = new View(context, attrs);
        view.setBackgroundColor(Color.BLACK);
        this.addView(view, 0);
    }
    /**
     *  measurement 
     */
    @SuppressLint("DrawAllocation")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (getChildCount() != 2) {
            try {
                //  The custom Exception
                throw new Exception() {
                    @Override
                    public void printStackTrace() {
                        System.err.println("MyView Medium can only exist 1 a View");
                        super.printStackTrace();
                    }
                };
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        leftView = getChildAt(0);
        //  Set up the leftview The high and wide 
        leftView.measure(widthMeasureSpec, heightMeasureSpec);
        rightView = getChildAt(1);
        //  Set up the rightview The high and wide 
        rightView.measure(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    /**
     *  layout 
     */
    @SuppressLint("NewApi")
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        System.out.println(l + "," + t + "," + r + "," + b);
        if (leftView != null & rightView != null) {
            //  Set up the leftview The location of the , On the right side of the screen ( Not initially visible )
            leftView.layout(-r, 0, 0, b);
            //  Set up the rightView The location of the , In the screen 
            rightView.layout(l, t, r, b);
        }
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
    @SuppressLint("NewApi")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final float X = event.getX();
        float Y = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:
            System.out.println("X:" + X);
            if (X < 100) {
                scrollTo(0, 0);
            } else if (X > rightView.getMeasuredWidth() - 100) {//  When the user slides away from the right edge 100 when , Page is closed 
                new Thread(new Runnable() {//  The new thread , Sliding shut down 
                            @Override
                            public void run() {
                                for (int i = 0;; i++) {
                                    try {
                                        Thread.sleep(10);// rightView every 10ms Moves to the right 3, Ensure smooth sliding 
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    int len = (int) (X + 3 * i);
                                    // System.out.println("len:" + len);
                                    Message message = new Message();// android The central African UI Threads are not allowed to manipulate controls directly , Messages can be sent to the main thread handler class 
                                    if (len >= rightView.getMeasuredWidth()) {
                                        message.what = 1;
                                        handler.sendMessage(message);//  Send a message 
                                                                        //  Shut down View
                                        break;
                                    } else {
                                        message.what = 0; //  Send a message   Automatic sliding 
                                        handler.sendMessage(message);
                                    }
                                }
                            }
                        }).start();
            } else {
                scrollTo((int) -X, 0);
                //  Computing transparency information 
                float alpha = (float) (1.0 - (float) (1.0 / 400) * X);
                // System.out.println("alpha:" + al);
                //  Set transparency 
                leftView.setAlpha(alpha);
            }
            break;
        }
        //  Set up the true, consumption event The event , It's not passing out 
        return true;
    }
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @SuppressLint("NewApi")
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                scrollBy(-3, 0);// viewgroup Slide to the right 3
            } else if (msg.what == 1) {
                Toast.makeText(getContext(), " Shut down ", 50).show();
                setVisibility(View.GONE);//  Set up the viewgroup invisible ( hidden )
            }
        }
    };
}

Use:


<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>

To display the activity interface below, you need to use the add method of Fragment


// new 1 a Fragment
Fragment bFragment = new BFragment();
getFragmentManager().beginTransaction().add(R.id.fragment, bFragment).commit();


Related articles: