Android Fragment之间的点击切换

本文主要讲解点击不同按钮之后,相应Fragment之间的切换。类似的这种实例,在开发中经常用到,网上也有很多比较好的开源Demo,写的很详细。我当时也是用过其中的某些Demo完成过项目的编写。但别人的终究不是自己的,以至于,以后要修改个什么东西,就显得比较吃力。

所以这篇文章是写的比较简单的一个实例,目的就是使用最新的Fragment来实现这种切换效果,希望初学者能够完全理解其中的思路。为以后更复杂的开发打好基础。

闲言少叙,开始学习吧:


Demo实现效果:


源代码:


布局文件,activity_main:

<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"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="体育"
        />

    <Button
        android:id="@+id/button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="娱乐" />

    <Button
        android:id="@+id/button03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="教育" />

    <Button
        android:id="@+id/button04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="文学" />

</LinearLayout>

fragment01:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
    

   	<TextView 
   	    android:id="@+id/textView"
   	    android:layout_width="wrap_content"
   	    android:layout_height="wrap_content"
   	    android:text="现在是体育新闻..."
   	    android:textSize="20sp"/>
</LinearLayout>

fragment02:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
    

   	<TextView 
   	    android:id="@+id/textView"
   	    android:layout_width="wrap_content"
   	    android:layout_height="wrap_content"
   	    android:text="现在是娱乐新闻..."
   	    android:textSize="20sp"/>
</LinearLayout>


fragment03:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
    

   	<TextView 
   	    android:id="@+id/textView"
   	    android:layout_width="wrap_content"
   	    android:layout_height="wrap_content"
   	    android:text="现在是教育新闻..."
   	    android:textSize="20sp"/>
</LinearLayout>


fragment04:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
    

   	<TextView 
   	    android:id="@+id/textView"
   	    android:layout_width="wrap_content"
   	    android:layout_height="wrap_content"
   	    android:text="现在是文艺新闻..."
   	    android:textSize="20sp"/>
</LinearLayout>

代码:MainActivity:

package com.fragmentdemoqihuan;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
	private Button button01, button02, button03, button04;
	private FragmentManager fm;
	private FragmentTransaction ft;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();

	}

	private void initView() {
		button01 = (Button) findViewById(R.id.button01);
		button02 = (Button) findViewById(R.id.button02);
		button03 = (Button) findViewById(R.id.button03);
		button04 = (Button) findViewById(R.id.button04);

		button01.setBackgroundColor(Color.BLUE);
		button02.setBackgroundColor(Color.GREEN);
		button03.setBackgroundColor(Color.GREEN);
		button04.setBackgroundColor(Color.GREEN);
		
		fm = getFragmentManager();
		ft = fm.beginTransaction();
		/**
		 * 应用进入后,默认选择点击Fragment01
		 */
		ft.replace(android.R.id.content, new Fragment01());
		ft.commit();

		button01.setOnClickListener(this);
		button02.setOnClickListener(this);
		button03.setOnClickListener(this);
		button04.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		fm = getFragmentManager();
		ft = fm.beginTransaction();
		switch (v.getId()) {

		case R.id.button01:
			button01.setBackgroundColor(Color.BLUE);
			button02.setBackgroundColor(Color.GREEN);
			button03.setBackgroundColor(Color.GREEN);
			button04.setBackgroundColor(Color.GREEN);

			ft.replace(android.R.id.content, new Fragment01());
			break;

		case R.id.button02:
			button01.setBackgroundColor(Color.GREEN);
			button02.setBackgroundColor(Color.BLUE);
			button03.setBackgroundColor(Color.GREEN);
			button04.setBackgroundColor(Color.GREEN);

			ft.replace(android.R.id.content, new Fragment02());
			break;

		case R.id.button03:
			button01.setBackgroundColor(Color.GREEN);
			button02.setBackgroundColor(Color.GREEN);
			button03.setBackgroundColor(Color.BLUE);
			button04.setBackgroundColor(Color.GREEN);

			ft.replace(android.R.id.content, new Fragment03());
			break;

		case R.id.button04:
			button01.setBackgroundColor(Color.GREEN);
			button02.setBackgroundColor(Color.GREEN);
			button03.setBackgroundColor(Color.GREEN);
			button04.setBackgroundColor(Color.BLUE);

			ft.replace(android.R.id.content, new Fragment04());
			break;

		default:
			break;
		}
		//不要忘记提交
		ft.commit();
	}

}

Fragment01:

package com.fragmentdemoqihuan;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment01 extends Fragment {

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

Fragment02:

package com.fragmentdemoqihuan;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment02 extends Fragment {

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




Fragment03:

package com.fragmentdemoqihuan;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment03 extends Fragment {

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


Fragment04:

package com.fragmentdemoqihuan;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment04 extends Fragment {

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


至此,该文章书写介绍,我觉得这种写法比较好,可以替代以前我们使用过的ActivityGroup或Tabhost。


源代码下载:

点击下载源码


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

红日666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值