您的位置:首页 > 移动开发 > Android开发

android Fragment实现Tab功能(fragment相互切换时,可保存之前状态)

2017-06-09 18:50 579 查看
原文转自 http://blog.csdn.net/jjsyjiao/article/details/40980909
由于TabHost已经逐渐过时,现在开发已经不建议使用,将由Fragment代替,本文主要讲述Fragment替换TabHost的方法,代码简单易懂,适合初学者使用。

上个效果图先:





首先:

1、建个资源文件activity_main.xml;

[html] view
plain copy

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<LinearLayout

android:id="@+id/bottom"

android:layout_width="match_parent"

android:layout_height="60dip"

android:layout_alignParentBottom="true"

android:orientation="horizontal" >

<RelativeLayout

android:id="@+id/fotune"

android:layout_width="0dip"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="@color/bottom_background_onclick_color" >

<ImageView

android:id="@+id/fotune_img"

android:layout_width="35dip"

android:layout_height="35dip"

android:layout_centerInParent="true"

android:background="@drawable/fotune_a" />

</RelativeLayout>

<RelativeLayout

android:id="@+id/message"

android:layout_width="0dip"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="@color/bottom_background_unclick_color" >

<ImageView

android:id="@+id/message_img"

android:layout_width="35dip"

android:layout_height="35dip"

android:layout_centerInParent="true"

android:background="@drawable/message_ia" />

</RelativeLayout>

<RelativeLayout

android:id="@+id/account"

android:layout_width="0dip"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="@color/bottom_background_unclick_color" >

<ImageView

android:id="@+id/account_img"

android:layout_width="35dip"

android:layout_height="35dip"

android:layout_centerInParent="true"

android:background="@drawable/account_ia" />

</RelativeLayout>

<RelativeLayout

android:id="@+id/more"

android:layout_width="0dip"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="@color/bottom_background_unclick_color" >

<ImageView

android:id="@+id/more_img"

android:layout_width="35dip"

android:layout_height="35dip"

android:layout_centerInParent="true"

android:background="@drawable/more_ia" />

</RelativeLayout>

</LinearLayout>

<FrameLayout

android:id="@+id/container"

android:layout_width="fill_parent"

android:layout_height="match_parent"

android:layout_above="@id/bottom" />

</RelativeLayout>

其中FrameLayout就是承载着Fragment的容器;

2、接下来编写我们的Activity,注意:Activity需要继承FragmentActivity,不然调用getSupportFragmentManager()方法会提示“The method getSupportFragmentManager() is undefined for the type MainActivity”;

[java] view
plain copy

package com.example.tab;

import android.content.Context;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentActivity;

import android.support.v4.app.FragmentTransaction;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ImageView;

import android.widget.RelativeLayout;

public class MainActivity extends FragmentActivity implements OnClickListener {

public static Context context;

private RelativeLayout fotune, message, account, more;

private ImageView fotune_img, message_img, account_img, more_img;

private Fragment1 fragment1 = new Fragment1();

private Fragment2 fragment2 = new Fragment2();

private Fragment3 fragment3 = new Fragment3();

private Fragment4 fragment4 = new Fragment4();

private Bundle bundle = new Bundle();

/**

* 记录当前Activity显示的fragment

*/

private Fragment mContent;

private String[] textStrings = new String[] { "第一个页面——Fragment1", "第一个页面——Fragment2",

"第一个页面——Fragment3", "第一个页面——Fragment4" };

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

context = MainActivity.this;

initLayout();

if (savedInstanceState == null) {

bundle.putString("text", textStrings[0]);

fragment1.setArguments(bundle);

mContent = fragment1;

getSupportFragmentManager().beginTransaction()

.add(R.id.container, fragment1).commit();

}

}

/**

* 初始化控件

*/

private void initLayout() {

fotune = (RelativeLayout) findViewById(R.id.fotune);

fotune.setOnClickListener(this);

message = (RelativeLayout) findViewById(R.id.message);

message.setOnClickListener(this);

account = (RelativeLayout) findViewById(R.id.account);

account.setOnClickListener(this);

more = (RelativeLayout) findViewById(R.id.more);

more.setOnClickListener(this);

fotune_img = (ImageView) findViewById(R.id.fotune_img);

message_img = (ImageView) findViewById(R.id.message_img);

account_img = (ImageView) findViewById(R.id.account_img);

more_img = (ImageView) findViewById(R.id.more_img);

}

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

switch (arg0.getId()) {

case R.id.fotune:

fotune.setBackgroundColor(getResources().getColor(

R.color.bottom_background_onclick_color));

message.setBackgroundColor(getResources().getColor(

R.color.bottom_background_unclick_color));

account.setBackgroundColor(getResources().getColor(

R.color.bottom_background_unclick_color));

more.setBackgroundColor(getResources().getColor(

R.color.bottom_background_unclick_color));

fotune_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.fotune_a));

message_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.message_ia));

account_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.account_ia));

more_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.more_ia));

// switchContent(fragment1,

// 0);若不想保留之前fragment的状态,每次都重新创建fragment,则选择该条命令;

switchContent_keep(mContent, fragment1, 0);// 若想保留之前fragment的状态,则选择该条命令;

break;

case R.id.message:

fotune.setBackgroundColor(getResources().getColor(

R.color.bottom_background_unclick_color));

message.setBackgroundColor(getResources().getColor(

R.color.bottom_background_onclick_color));

account.setBackgroundColor(getResources().getColor(

R.color.bottom_background_unclick_color));

more.setBackgroundColor(getResources().getColor(

R.color.bottom_background_unclick_color));

fotune_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.fotune_ia));

message_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.message_a));

account_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.account_ia));

more_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.more_ia));

// switchContent(fragment2,

// 1);若不想保留之前fragment的状态,每次都重新创建fragment,则选择该条命令;

switchContent_keep(mContent, fragment2, 1);// 若想保留之前fragment的状态,则选择该条命令;

break;

case R.id.account:

fotune.setBackgroundColor(getResources().getColor(

R.color.bottom_background_unclick_color));

message.setBackgroundColor(getResources().getColor(

R.color.bottom_background_unclick_color));

account.setBackgroundColor(getResources().getColor(

R.color.bottom_background_onclick_color));

more.setBackgroundColor(getResources().getColor(

R.color.bottom_background_unclick_color));

fotune_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.fotune_ia));

message_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.message_ia));

account_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.account_a));

more_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.more_ia));

// switchContent(fragment3,

// 2);若不想保留之前fragment的状态,每次都重新创建fragment,则选择该条命令;

switchContent_keep(mContent, fragment3, 2);// 若想保留之前fragment的状态,则选择该条命令;

break;

case R.id.more:

fotune.setBackgroundColor(getResources().getColor(

R.color.bottom_background_unclick_color));

message.setBackgroundColor(getResources().getColor(

R.color.bottom_background_unclick_color));

account.setBackgroundColor(getResources().getColor(

R.color.bottom_background_unclick_color));

more.setBackgroundColor(getResources().getColor(

R.color.bottom_background_onclick_color));

fotune_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.fotune_ia));

message_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.message_ia));

account_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.account_ia));

more_img.setBackgroundDrawable(getResources().getDrawable(

R.drawable.more_a));

// switchContent(fragment4,

// 3);若不想保留之前fragment的状态,每次都重新创建fragment,则选择该条命令;

switchContent_keep(mContent, fragment4, 3);// 若想保留之前fragment的状态,则选择该条命令;

break;

default:

break;

}

}

/**

* fragment替换(不保留之前的状态)

*

* @param to

* @param i

*/

public void switchContent(Fragment to, int i) {

bundle.putString("text", textStrings[i]);

to.setArguments(bundle);

getSupportFragmentManager().beginTransaction()

.replace(R.id.container, to).commit();

}

/**

* fragment替换(保留之前的状态)

*

* @param from

* @param to

* @param i

*/

public void switchContent_keep(Fragment from, Fragment to, int i) {

if (from != to) {

mContent = to;

FragmentTransaction transaction = getSupportFragmentManager()

.beginTransaction();

// 先判断是否被add过

if (!to.isAdded()) {

bundle.putString("text", textStrings[i]);

to.setArguments(bundle);

// 隐藏当前的fragment,add下一个fragment到Activity中

transaction.hide(from).add(R.id.container, to).commit();

} else {

// 隐藏当前的fragment,显示下一个fragment

transaction.hide(from).show(to).commit();

// to.onResume();该命令可注释,若希望fragment切换的过程中,被显示的fragment执行onResume方法,则解注;

}

}

}

}

如果在Fragment切换的过程中,不需要保留之前Fragment的状态,直接调用getSupportFragmentManager().beginTransaction().replace(R.id.Container,
to).commit();即可。每次替换实质都是先remove再add,Fragment的onCreate方法都会被调用;

如果在Fragment切换的过程中,需要保留之前Fragment的状态,则需要定义一个变量Fragment mContent来保存当前activity显示fragment对象。如果你要显示的fragment对象之前未被添加过,则需要先隐藏当前显示的fragment对象,再添加你要显示的fragment对象;例如:transaction.hide(from).add(R.id.container, to).commit();若你要显示的fragment对象之前已经被添加过了,则先隐藏当前的fragment对象,再显示你要显示的fragment对象即可;例如:transaction.hide(from).show(to).commit();(注:如果此时希望调用显示的fragment对象的onResume方法,则执行to.onResume();即可。但注意的是:从fragmentActivity跳转到下一个Activity,再按返回键时,FrameLayout容器中所有的fragment都将会调用onResume方法;打印log如下所示:)

[html] view
plain copy

11-10 15:54:48.546: E/onCreate(19070): Fragment1

11-10 15:54:48.546: E/onCreateView(19070): Fragment1

11-10 15:54:48.546: E/onResume(19070): Fragment1

11-10 15:54:49.936: E/onCreate(19070): Fragment2

11-10 15:54:49.936: E/onCreateView(19070): Fragment2

11-10 15:54:49.936: E/onResume(19070): Fragment2

11-10 15:54:51.656: E/onCreate(19070): Fragment3

11-10 15:54:51.656: E/onCreateView(19070): Fragment3

11-10 15:54:51.656: E/onResume(19070): Fragment3

11-10 15:54:52.806: E/onCreate(19070): Fragment4

11-10 15:54:52.806: E/onCreateView(19070): Fragment4

11-10 15:54:52.806: E/onResume(19070): Fragment4

<span style="color:#ff0000;">11-10 15:54:54.636: E/onResume(19070): Fragment1

11-10 15:54:54.636: E/onResume(19070): Fragment2

11-10 15:54:54.636: E/onResume(19070): Fragment3

11-10 15:54:54.636: E/onResume(19070): Fragment4</span>

FragmentActivity向Fragment传递数据:创建个Bundle数据包,Fragment对象调用setArguments(Bundle bundle)方法即可;

Fragment接收从FragmentActivity传来的数据:调用getArguments()接收数据包,返回Bundle对象;

3、Fragment的具体实现

[java] view
plain copy

package com.example.tab;

import android.content.Intent;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class Fragment1 extends Fragment {

public Fragment1() {

}

@Override

public void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

Log.e("onCreate", "Fragment1");

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

Log.e("onCreateView", "Fragment1");

View rootView = inflater.inflate(R.layout.fragment_main, container,

false);

TextView text = (TextView) rootView.findViewById(R.id.text);

text.setText(getArguments().getString("text"));

Button button = (Button) rootView.findViewById(R.id.button);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

Intent intent = new Intent();

intent.setClass(MainActivity.context, SecondActivity.class);

startActivity(intent);

}

});

return rootView;

}

@Override

public void onResume() {

// TODO Auto-generated method stub

super.onResume();

Log.e("onResume", "Fragment1");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  fragment 代替 tab
相关文章推荐