Fragment switching Animation

Source: Internet
Author: User

Free source code

Package com. example. myfragmentanimation;


Import Android. App. activity;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. Support. v4.app. Fragment;
Import Android. Support. v4.app. fragmentactivity;
Import Android. Support. v4.app. fragmentmanager;
Import Android. View. view;
Import Android. View. window;
Import Android. widget. Button;
Import Android. widget. imageview;


Public class myactivity extends fragmentactivity {
Private fragment [] fragments;
Private imageview find_group_but;
Private imageview find_user_but;
Private fragmentmanager mfragmentmanager = NULL;
Private fragment mtextfragmentone = NULL;
/**
* Called when the activity is first created.
*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Requestwindowfeature (window. feature_no_title );
Setcontentview (R. layout. find_fragment_activity );
Initview ();
}
/*
Initialize View
*/
Private void initview (){
Find_group_but = (imageview) findviewbyid (R. Id. find_group_but );
Find_user_but = (imageview) findviewbyid (R. Id. find_user_but );
Fragments = new fragment [2];
Fragments [0] = getsupportfragmentmanager (). findfragmentbyid (R. Id. fragment_findgroup );
Fragments [1] = getsupportfragmentmanager (). findfragmentbyid (R. Id. fragment_finduser );
Removefragment ();
Addfragment ();
Find_group_but.setenabled (false );
Find_user_but.setenabled (true );
Getsuppfrfragmentmanager (). begintransaction (). Hide (fragments [1]). Show (fragments [0]). Commit ();


}






// Fragment display Animation
Private void addfragment (){
If (null = mfragmentmanager ){
Mfragmentmanager = getsupportfragmentmanager ();
}




Android. Support. v4.app. fragmenttransaction = mfragmentmanager
. Begintransaction ();
If (fragments [0]. ishidden () = true ){
System. Out. println ("<000000 ");
Fragmenttransaction. setcustomanimations (
R. anim. push_right_in,
R. anim. push_right_out,
R. anim. push_right_in,

R. anim. push_right_out );

// The first two animations are display animations, and the last two animations are hidden animations.

Mtextfragmentone = new findgroupfragment ();
// The first parameter shows the fagment Control ID, and the second parameter control's Java file is fragment
Fragmenttransaction. Add (R. Id. fragment_findgroup, mtextfragmentone );




} If (fragments [1]. ishidden () = true ){
System. Out. println ("<111111 ");
Fragmenttransaction. setcustomanimations (
R. anim. push_left_in,
R. anim. push_left_out,
R. anim. push_left_in,
R. anim. push_left_out );
Mtextfragmentone = new finduserfragment ();
Fragmenttransaction. Add (R. Id. fragment_finduser, mtextfragmentone );


}


Mtextfragmentone = new findgroupfragment ();
Fragmenttransaction. Add (R. Id. fragment_findgroup, mtextfragmentone );


Fragmenttransaction. addtobackstack (null );
Fragmenttransaction. Commit ();
}
// Fragment disappearance Animation
Private void removefragment (){
If (null = mfragmentmanager ){
Mfragmentmanager = getsupportfragmentmanager ();
}
Mfragmentmanager. popbackstack ();
}
/*
Show search alliance page
*/
Public void findgroupclick (view ){
Removefragment ();
Addfragment ();
Find_group_but.setenabled (false );
Find_user_but.setenabled (true );
Getsuppfrfragmentmanager (). begintransaction (). Hide (fragments [1]). Show (fragments [0]). Commit ();




}
/*
Show user search page
*/
Public void finduserclick (view ){
Removefragment ();
Addfragment ();
Find_group_but.setenabled (true );
Find_user_but.setenabled (false );
Getsuppfrfragmentmanager (). begintransaction (). Hide (fragments [0]). Show (fragments [1]). Commit ();


}


}

Free source code

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.