有多种fragment之间切换的效果,效果是这样的:

Demo的实现是很简单的。

在res/values中,新建一个arrays.xml文件,存放Fragment动画效果的名称,在spinner中使用:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string-array name="array_spinner">  
  5.         <item>SCALEX</item>  
  6.         <item>SCALEY</item>  
  7.         <item>SCALEXY</item>  
  8.         <item>FADE</item>  
  9.         <item>FLIP_HORIZONTAL</item>  
  10.         <item>FLIP_VERTICAL</item>  
  11.         <item>SLIDE_VERTICAL</item>  
  12.         <item>SLIDE_HORIZONTAL</item>  
  13.         <item>SLIDE_HORIZONTAL_PUSH_TOP</item>  
  14.         <item>SLIDE_VERTICAL_PUSH_LEFT</item>  
  15.         <item>GLIDE</item>  
  16.         <item>SLIDING</item>  
  17.         <item>STACK</item>  
  18.         <item>CUBE</item>  
  19.         <item>ROTATE_DOWN</item>  
  20.         <item>ROTATE_UP</item>  
  21.         <item>ACCORDION</item>  
  22.         <item>TABLE_HORIZONTAL</item>  
  23.         <item>TABLE_VERTICAL</item>  
  24.         <item>ZOOM_FROM_LEFT_CORNER</item>  
  25.         <item>ZOOM_FROM_RIGHT_CORNER</item>  
  26.         <item>ZOOM_SLIDE_HORIZONTAL</item>  
  27.         <item>ZOOM_SLIDE_VERTICAL</item>  
  28.     </string-array>  
  29.   
  30. </resources>  

 

 

两个空的fragment的布局(都有一样):

 

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <com.desarrollodroide.libraryfragmenttransactionextended.SlidingRelativeLayout  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:background="@drawable/back1" >  
  8.   
  9.     <TextView  
  10.         android:layout_centerInParent="true"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="Fragment1"  
  14.         android:textColor="#FF009922"  
  15.         android:textSize="20dp"/>  
  16.   
  17. </com.desarrollodroide.libraryfragmenttransactionextended.SlidingRelativeLayout>  


主部局:

 

 

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical">  
  6.   
  7.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="horizontal">  
  11.   
  12.         <Spinner  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_weight="1"  
  16.             android:id="@+id/spinner" />  
  17.   
  18.         <Button  
  19.             android:id="@+id/button"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content"  
  22.             android:onClick="addTransition"  
  23.             android:text="Push" />  
  24.     </LinearLayout>  
  25.   
  26.     <com.desarrollodroide.libraryfragmenttransactionextended.SlidingRelativeLayout  
  27.         android:id="@+id/fragment_place"  
  28.         android:layout_marginLeft="10dp"  
  29.         android:layout_marginRight="10dp"  
  30.         android:layout_marginBottom="10dp"  
  31.         android:layout_marginTop="20dp"  
  32.         android:layout_width="match_parent"  
  33.         android:layout_height="match_parent">  
  34.     </com.desarrollodroide.libraryfragmenttransactionextended.SlidingRelativeLayout>  
  35.   
  36. </LinearLayout>  


MAinActivity:

 

 

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.example.scxh.testfragmenttransaction;  
  2.   
  3. import android.app.FragmentManager;  
  4. import android.app.FragmentTransaction;  
  5. import android.os.Bundle;  
  6. import android.support.v7.app.AppCompatActivity;  
  7. import android.view.View;  
  8. import android.widget.AdapterView;  
  9. import android.widget.ArrayAdapter;  
  10. import android.widget.Button;  
  11. import android.widget.Spinner;  
  12.   
  13. import com.desarrollodroide.libraryfragmenttransactionextended.FragmentTransactionExtended;  
  14.   
  15. public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {  
  16.     private int optionSelected = 0;  
  17.     private BlankFragment mFirstFragment;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.   
  24.   
  25.         Spinner spinner = (Spinner) findViewById(R.id.spinner);  
  26.         ArrayAdapter adapter=ArrayAdapter.createFromResource(this,R.array.array_spinner,android.R.layout.simple_spinner_item);  
  27.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  28.         spinner.setAdapter(adapter);  
  29.         spinner.setOnItemSelectedListener(this);  
  30.   
  31.         mFirstFragment=new BlankFragment();  
  32.   
  33.         getFragmentManager().beginTransaction()  
  34.                 .add(R.id.fragment_place,mFirstFragment).commit();  
  35.   
  36.     }  
  37.   
  38.     public void addTransition(View view) {  
  39.         Button button = (Button) findViewById(R.id.button);  
  40.         if (getFragmentManager().getBackStackEntryCount() == 0) {  
  41.             BlankFragment2 fragment2 = new BlankFragment2();  
  42.             FragmentManager manager = getFragmentManager();  
  43.             FragmentTransaction transaction=manager.beginTransaction();  
  44.             FragmentTransactionExtended fragmentTransactionExtended = new FragmentTransactionExtended(this, transaction, mFirstFragment, fragment2, R.id.fragment_place);  
  45.             fragmentTransactionExtended.addTransition(optionSelected);  
  46.             fragmentTransactionExtended.commit();  
  47.             button.setText("Back");  
  48.         }else{  
  49.             getFragmentManager().popBackStack();  
  50.             button.setText("Push");  
  51.         }  
  52.     }  
  53.   
  54.     @Override  
  55.     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
  56.         optionSelected = position;  
  57.     }  
  58.   
  59.     @Override  
  60.     public void onNothingSelected(AdapterView<?> parent) {  
  61.   
  62.     }  
  63.   
  64.     @Override  
  65.     public void onBackPressed(){  
  66.         Button button = (Button) findViewById(R.id.button);  
  67.         button.setText("Push");  
  68.         super.onBackPressed();  
  69.     }  
  70. }  
posted on 2016-03-30 15:55  0代码狂人0  阅读(637)  评论(0编辑  收藏  举报