安卓中点击不同按钮切换不同到Fragment

整体效果如下:



实现方式:通过Activity的FragmentManage去实现

首先要先去创建两个布局文件,分别为pay.xml和income.xml,代表两个片段的内容,下面我只是贴了其中一个布局文件的内容

[html]  view plain  copy
  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:background="#445543"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.       
  8.   
  9. </LinearLayout>  

简单起见我只是把这两个布局文件的background给设置了一下,用于区分。

然后写两个类,分别为PayFragment和IncomeFragment,让它们都去继承Fragment类,重写onCreateView方法,

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.pay, null);
return view;
}

Inflate()作用就是将xml定义的一个布局找出来,inflate方法中间那个参数是制定的布局文件。

完成上面的工作后我们就该总布局文件的编写了:

[html]  view plain  copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     tools:context=".SecondActivity" >  
  7. <LinearLayout   
  8.     android:layout_width="match_parent"  
  9.     android:layout_height="wrap_content"  
  10.     android:orientation="horizontal">  
  11.   <Button   
  12.      android:layout_width="wrap_content"  
  13.      android:layout_height="wrap_content"  
  14.      android:layout_weight="1"  
  15.      android:id="@+id/pay"  
  16.      android:text="支出"/>  
  17.   <Button   
  18.      android:layout_width="wrap_content"  
  19.      android:layout_height="wrap_content"  
  20.      android:layout_weight="1"  
  21.      android:id="@+id/income"  
  22.      android:text="收入"/>  
  23. </LinearLayout>  
  24. <FrameLayout   
  25.     android:layout_width="match_parent"  
  26.     android:layout_height="wrap_content"  
  27.     android:layout_weight="1"  
  28.     android:id="@+id/fl">  
  29.       
  30. </FrameLayout>  
  31. </LinearLayout>  
然后写一个类去继承Activity:

[html]  view plain  copy
  1. import android.os.Bundle;  
  2. import android.app.Activity;  
  3. import android.support.v4.app.FragmentActivity;  
  4. import android.support.v4.app.FragmentManager;  
  5. import android.support.v4.app.FragmentTransaction;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.RelativeLayout;  
  11.   
  12. public class SecondActivity extends FragmentActivity implements OnClickListener{  
  13.   
  14.     private Button pay;  
  15.     private Button income;  
  16.     private RelativeLayout rl;  
  17.     private IncomeFragment incomeFragment;  
  18.     private PayFragment payFragment;  
  19.     FragmentManager fm;  
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_second);  
  24.         initview();  
  25.     }  
  26.   
  27.     private void initview(){  
  28.         pay = (Button) findViewById(R.id.pay);  
  29.         income = (Button) findViewById(R.id.income);  
  30.         pay.setOnClickListener(this);  
  31.         income.setOnClickListener(this);  
  32.         fm = getSupportFragmentManager();  
  33.     }  
  34.   
  35.     @Override  
  36.     public void onClick(View v) {  
  37.         // TODO Auto-generated method stub  
  38.         switch (v.getId()) {  
  39.         case R.id.income:  
  40.             setTabSelection(0);  
  41.             break;  
  42.   
  43.         case R.id.pay:  
  44.             setTabSelection(1);  
  45.             break;  
  46.         }  
  47.     }  
  48. private void setTabSelection(int index){  
  49.     FragmentTransaction ft = fm.beginTransaction();  
  50.     hideFragment(ft);  
  51.     switch (index) {  
  52.     case 0:  
  53.         if(incomeFragment==null){  
  54.             incomeFragment = new IncomeFragment();  
  55.             ft.add(R.id.fl, incomeFragment);  
  56.         }else{  
  57.             ft.show(incomeFragment);  
  58.         }  
  59.           
  60.         break;  
  61.   
  62.     case 1:  
  63.         if(payFragment==null){  
  64.             payFragment = new PayFragment();  
  65.             ft.add(R.id.fl, payFragment);  
  66.         }  
  67.             ft.show(payFragment);  
  68.         break;  
  69.     }  
  70.     ft.commit();  
  71. }  
  72. //用于隐藏fragment  
  73. private void hideFragment(FragmentTransaction ft){  
  74.     if(incomeFragment!=null){  
  75.         ft.hide(incomeFragment);  
  76.     }if(payFragment!=null){  
  77.         ft.hide(payFragment);  
  78.     }  
  79. }  
  80. }  
其中重要是用了FragmentTransaction的show和hide方法,当然,如果用replace方法也能实现,但是replace方法相比而言比用show和hide方法要浪费资源,因为replace方法其实就是remove方法和add方法的结合,当我们加载布局文件后当不需要显示的时候就remove掉,当用的时候再去加载,这个中间要耗资源,如果用show和hide方法的话,如果用到某个布局,那我们就show,不显示布局就hide,这样避免了重复加载。

猜你喜欢

转载自blog.csdn.net/taa1007/article/details/80581150
今日推荐