首页 > 代码库 > Android actionBar与Fragment结合使用Demo2

Android actionBar与Fragment结合使用Demo2

上一篇文章介绍了ActionBar的使用,这里介绍ActionBar的另一种使用方法,达到的效果和以前的GroupActivity或TabHost是一样的,可作为导航来使用。

实现效果图:


源代码:


布局文件:activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:background="@android:color/white">

</RelativeLayout>

f1.xml(体育新闻对应的布局文件):

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="欢迎收看体育新闻..." 
        android:textSize="20sp"
        android:textColor="@android:color/holo_blue_dark"/>

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

f2.xml(娱乐新闻):

<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="欢迎收看娱乐新闻..."
        android:textColor="@android:color/holo_green_dark"
        android:textSize="20sp" />

</LinearLayout>

f3.xml(军事新闻):

<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="欢迎收看军事新闻..."
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="20sp" />

</LinearLayout>

代码文件:

MainActivity:

package com.fragmentdemo10_actionbar;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class MainActivity extends Activity {
	private ActionBar actionBar;
	/**
	 * 设置三个整型常量,分别为0,1,2;分别对应 :运动新闻、娱乐新闻、军事新闻。
	 */
	private final int SPORTS = 0;
	private final int ENTERTAINMENT = 1;
	private final int MILITARY = 2;

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

		actionBar = getActionBar();
		// 设置ActionBar的导航模式
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
		/**
		 * 添加三个tab,分别为:体育新闻,娱乐新闻,军事新闻。
		 */
		actionBar.addTab(actionBar.newTab().setText("体育新闻")
				.setIcon(R.drawable.ic_launcher)
				.setTabListener(new MyTabListener()).setTag(SPORTS));
		actionBar.addTab(actionBar.newTab().setText("娱乐新闻")
				.setIcon(R.drawable.ic_launcher)
				.setTabListener(new MyTabListener()).setTag(ENTERTAINMENT));
		actionBar.addTab(actionBar.newTab().setText("军事新闻")
				.setIcon(R.drawable.ic_launcher)
				.setTabListener(new MyTabListener()).setTag(MILITARY));
	}

	class MyTabListener implements TabListener {

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			switch (Integer.parseInt(tab.getTag().toString())) {
			/**
			 * 对应体育新闻
			 */
			case SPORTS:
				ft.replace(R.id.main, new FragementA());
				break;
			/**
			 * 对应娱乐新闻
			 */
			case ENTERTAINMENT:
				ft.replace(R.id.main, new FragementB());
				break;
			/**
			 * 对应军事新闻
			 */
			case MILITARY:
				ft.replace(R.id.main, new FragementC());
				break;
			default:
				break;
			}
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {

		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {

		}

	}
}
FragmentA(Tab体育新闻对应的Fragment):
package com.fragmentdemo10_actionbar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
 * Tab体育新闻对应的Fragment
 *
 */
public class FragementA extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.f1, null);
		return view;
	}
}
FragmentB(Tab娱乐新闻对应的Fragment):
package com.fragmentdemo10_actionbar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
 * TAB娱乐新闻对应的Fragment
 *
 */
public class FragementB extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.f2, null);
		return view;
	}
}
FragmentC(Tab军事新闻对应的Fragment):
package com.fragmentdemo10_actionbar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
 * TAB军事新闻对应的Fragment
 *
 */
public class FragementC extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.f3, null);
		return view;
	}
}

源代码下载:

点击下载源码