Fragment 搭建框架 实现 导航 切换

先看效果图:

  

关于效果图中的汉字、颜色、样式、图片类的素材,在这里我不再提供。

只提供主要的功能代码,样式等自己定义。文件的名称也是自己定义。


1.main_tab.xml文件也就是主导航页面:

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

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/main_tab_bg_click" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </TabWidget>
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

2.main_tab_items_view.xml此文件是在第3步的文件中使用,自定义导航的个数绑定到此页面上,绑定导航事件。

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

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:layout_marginTop="6dp"
        android:src="@drawable/ic_launcher"
        android:contentDescription="@string/app_name">
    </ImageView>

    <TextView
        android:id="@+id/textview"       
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textSize="12sp"
        android:layout_marginTop="1dp"
        android:textColor="@drawable/main_tab_text_click">
    </TextView>

</LinearLayout>

3.MainTabActivity.java文件(此文件实现导航的切换功能)

package com.yunfeng.mc.fragment;

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;

import com.yunfeng.mc.R;
@SuppressLint("NewApi")
public class MainTabActivity extends FragmentActivity implements OnTabChangeListener{
	private FragmentTabHost mTabHost;

	//定义一个布局
	private LayoutInflater layoutInflater;

	//定义数组来存放Fragment界面
	@SuppressWarnings("rawtypes")
	private Class fragmentArray[] = {IndexFragment.class,IndexFragment.class,IndexFragment.class,UserFragment.class};

	//定义数组来存放按钮图片
	private int mImageViewArray[] = {R.drawable.main_tab1_click,R.drawable.main_tab2_click,R.drawable.main_tab3_click,
			R.drawable.main_tab4_click};

	//Tab选项卡的文字
	private String mTextviewArray[] = {"首页", "分类", "购物车", "我的"};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_tab);
		initView();
	}

	/**
	 * 初始化组件
	 */
	private void initView(){
		//实例化布局对象
		layoutInflater = LayoutInflater.from(this);

		//实例化TabHost对象,得到TabHost
		mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);      
		mTabHost.setOnTabChangedListener(this);

		//得到fragment的个数
		int count = fragmentArray.length;        

		for(int i = 0; i < count; i++){        
			//为每一个Tab按钮设置图标、文字和内容
			TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
			//将Tab按钮添加进Tab选项卡中
			mTabHost.addTab(tabSpec, fragmentArray[i], null);
		}

	}

	/**
	 * 给Tab按钮设置图标和文字
	 */
	private View getTabItemView(int index){
		View view = layoutInflater.inflate(R.layout.main_tab_item_view, null);

		ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
		imageView.setImageResource(mImageViewArray[index]);

		TextView textView = (TextView) view.findViewById(R.id.textview);                
		textView.setText(mTextviewArray[index]);

		return view;
	}


	@Override
	public void onTabChanged(String tabId) {

	}

}
4.其实到此为止,框架已经搭好了。

  在3中可以看到导航的绑定事件需要IndexFragment 和UserFragment 其实就是绑定到了两个页面上。

  在点击导航时跳转到不同的页面上。只要再创建两个.xml页面,和Activity就可以了.在这里我偷个懒就不再赘述了。

  就是一般的写两个页面,将对应的class放到3中对应的数组中就可以了。

注:页面Activity要继承Fragment。


完了。看我的运行效果(主要是看切换功能,我的页面就不给大家提供了): 

    


源码下载地址:http://download.csdn.net/detail/u010469432/6745277


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值