Implementation of the Android development tutorial slide off fragment Example _android

Source: Internet
Author: User

Main code: (with Comments)

Copy Code code as follows:

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);
}

/**
* Measuring
*/
@SuppressLint ("Drawallocation")
@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
if (Getchildcount ()!= 2) {
try {
Custom exception
throw new Exception () {
@Override
public void Printstacktrace () {
System.err.println ("Only one view exists in MyView");
Super.printstacktrace ();
}

};
catch (Exception e) {
E.printstacktrace ();
}
}
Leftview = Getchildat (0);
Set the height and width of the Leftview
Leftview.measure (Widthmeasurespec, Heightmeasurespec);
Rightview = Getchildat (1);
Set the height and width of the Rightview
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) {
   & nbsp;       //Set Leftview position, on the right side of the screen (initially invisible)
             Leftview.layout (-r, 0, 0, b);
           //Set the location of the Rightview 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 to the right edge 100, the page closes
New Thread (new Runnable () {//new threads, sliding off
@Override
public void Run () {
for (int i = 0;; i++) {
try {
Thread.Sleep (3)//Rightview every 10ms right to ensure smooth sliding
catch (Interruptedexception e) {
E.printstacktrace ();
}
int len = (int) (X + 3 * i);
System.out.println ("len:" + len);
Messages = new message ();//Android non-UI thread does not allow direct manipulation of controls, you can send them to the handler class of the main thread
if (Len >= rightview.getmeasuredwidth ()) {
Message.what = 1;
Handler.sendmessage (message);//Send messages
Close View
Break
} else {
Message.what = 0; Send Message Auto Slide
Handler.sendmessage (message);
}
}
}
). Start ();
} else {
Scrollto ((int)-X, 0);
Calculate Transparency Information
float alpha = (float) (1.0-(float) (1.0/400) * X);
System.out.println ("Alpha:" + al);
Set transparency
Leftview.setalpha (Alpha);
}
Break
}
Set true to consume event events that are not passed outward
return true;
}

@SuppressLint ("Handlerleak")
Handler Handler = new Handler () {
@SuppressLint ("Newapi")
@Override
public void Handlemessage (msg) {
if (Msg.what = = 0) {
Scrollby ( -3, 0);//ViewGroup slide 3 to the right
else if (Msg.what = 1) {
Toast.maketext (GetContext (), "close",). Show ();
Setvisibility (View.gone);/set ViewGroup not visible (hidden)
}
}

};
}

Use:

Copy Code code as follows:

<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 underlying activity interface, you need to use the fragment Add method

Copy Code code as follows:

Create a new fragment
Fragment bfragment = new Bfragment ();
Getfragmentmanager (). BeginTransaction (). Add (R.id.fragment, bfragment). commit ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.