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

整体效果如下:



实现方式:通过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;
	@Override
	protected 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();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (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();
}
//用于隐藏fragment
private 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,这样避免了重复加载。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值