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

来源:互联网 发布:js原生排序 编辑:程序博客网 时间:2024/04/29 17:36

整体效果如下:



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

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

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:background="#445543"    android:layout_height="match_parent"    android:orientation="vertical" >    </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方法中间那个参数是制定的布局文件。

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".SecondActivity" ><LinearLayout     android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal">  <Button      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_weight="1"     android:id="@+id/pay"     android:text="支出"/>  <Button      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_weight="1"     android:id="@+id/income"     android:text="收入"/></LinearLayout><FrameLayout     android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_weight="1"    android:id="@+id/fl">    </FrameLayout></LinearLayout>
然后写一个类去继承Activity:

import android.os.Bundle;import android.app.Activity;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RelativeLayout;public class SecondActivity extends FragmentActivity implements OnClickListener{private Button pay;private Button income;private RelativeLayout rl;private IncomeFragment incomeFragment;private PayFragment payFragment;FragmentManager fm;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);    initview();}private void initview(){pay = (Button) findViewById(R.id.pay);income = (Button) findViewById(R.id.income);pay.setOnClickListener(this);income.setOnClickListener(this);fm = getSupportFragmentManager();}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.income:setTabSelection(0);break;        case R.id.pay:        setTabSelection(1);break;}}private void setTabSelection(int index){FragmentTransaction ft = fm.beginTransaction();hideFragment(ft);switch (index) {case 0:if(incomeFragment==null){incomeFragment = new IncomeFragment();ft.add(R.id.fl, incomeFragment);}else{ft.show(incomeFragment);}break;    case 1:    if(payFragment==null){    payFragment = new PayFragment();        ft.add(R.id.fl, payFragment);    }    ft.show(payFragment);break;}ft.commit();}//用于隐藏fragmentprivate void hideFragment(FragmentTransaction ft){if(incomeFragment!=null){ft.hide(incomeFragment);}if(payFragment!=null){ft.hide(payFragment);}}}
其中重要是用了FragmentTransaction的show和hide方法,当然,如果用replace方法也能实现,但是replace方法相比而言比用show和hide方法要浪费资源,因为replace方法其实就是remove方法和add方法的结合,当我们加载布局文件后当不需要显示的时候就remove掉,当用的时候再去加载,这个中间要耗资源,如果用show和hide方法的话,如果用到某个布局,那我们就show,不显示布局就hide,这样避免了重复加载。





0 0
原创粉丝点击