2013.08.15——— android Fragment的简单实用

2013.08.15——— android Fragment的简单实用
参考: http://www.eoeandroid.com/thread-205166-1-1.html

<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>  
        
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:tabStripEnabled="false"
            android:background="@color/main_bg_color"
            android:layout_weight="0"/>
        
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

    </LinearLayout>
</TabHost>



id为realtabcontent的就是用来当fragment容器的,这个例子用了TabWidget,我一般都是用RadioButton来替代这个

package com.mostmood.activity;

import com.mostmood.R;
import com.mostmood.fragment.Home_Fragment;
import com.mostmood.fragment.Me_Fragment;
import com.mostmood.fragment.Message_Fragment;
import com.mostmood.fragment.Wall_Fragment;
import com.mostmood.util.DummyTabContent;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.support.v4.app.FragmentActivity;


/**
 * @Description: 主Activity,底部TabHost选项卡
 *
 * @Author Hades
 *
 * @date 2012-10-6
 *
 * @version V1.0  
 */

public class MainActivity extends FragmentActivity {

	TabHost tabHost;
	TabWidget tabWidget; 
	LinearLayout bottom_layout;
	int CURRENT_TAB = 0;	//设置常量
	Home_Fragment homeFragment;
	Wall_Fragment wallFragment;
	Message_Fragment messageFragment;
	Me_Fragment meFragment;
	android.support.v4.app.FragmentTransaction ft;
	RelativeLayout tabIndicator1,tabIndicator2,tabIndicator3,tabIndicator4,tabIndicator5;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findTabView();
        tabHost.setup();
        
        /** 监听*/
        TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener(){
			@Override
			public void onTabChanged(String tabId) {
				
				/**碎片管理*/
				android.support.v4.app.FragmentManager fm =  getSupportFragmentManager();
				homeFragment = (Home_Fragment) fm.findFragmentByTag("home");
				wallFragment = (Wall_Fragment) fm.findFragmentByTag("wall");
				messageFragment = (Message_Fragment) fm.findFragmentByTag("message");
				meFragment = (Me_Fragment) fm.findFragmentByTag("me");
				ft = fm.beginTransaction();
				
				/** 如果存在Detaches掉 */
				if(homeFragment!=null)
					ft.detach(homeFragment);
				
				/** 如果存在Detaches掉 */
				if(wallFragment!=null)
					ft.detach(wallFragment);
				
				/** 如果存在Detaches掉 */
				if(messageFragment!=null)
					ft.detach(messageFragment);
				
				/** 如果存在Detaches掉 */
				if(meFragment!=null)
					ft.detach(meFragment);
				
				/** 如果当前选项卡是home */
				if(tabId.equalsIgnoreCase("home")){
					isTabHome();
					CURRENT_TAB = 1;
					
				/** 如果当前选项卡是wall */
				}else if(tabId.equalsIgnoreCase("wall")){	
					isTabWall();
					CURRENT_TAB = 2;
					
				/** 如果当前选项卡是message */
				}else if(tabId.equalsIgnoreCase("message")){	
					isTabMessage();
					CURRENT_TAB = 3;
					
				/** 如果当前选项卡是me */
				}else if(tabId.equalsIgnoreCase("me")){	
					isTabMe();
					CURRENT_TAB = 4;
				}else{
					switch (CURRENT_TAB) {
					case 1:
						isTabHome();
						break;
					case 2:
						isTabWall();
						break;
					case 3:
						isTabMessage();
						break;
					case 4:
						isTabMe();
						break;
					default:
						isTabHome();
						break;
					}		
					
				}
					ft.commit();	
			}
        	
        };
        //设置初始选项卡  
        tabHost.setCurrentTab(0);
        tabHost.setOnTabChangedListener(tabChangeListener);
        initTab();
         /**  设置初始化界面  */
        tabHost.setCurrentTab(0);

    }
    
    //判断当前
    public void isTabHome(){
    	
    	if(homeFragment==null){		
			ft.add(R.id.realtabcontent,new Home_Fragment(), "home");						
		}else{
			ft.attach(homeFragment);						
		}
    }
    
    public void isTabWall(){
    	
    	if(wallFragment==null){
			ft.add(R.id.realtabcontent,new Wall_Fragment(), "wall");						
		}else{
			ft.attach(wallFragment);						
		}
    }
    
    public void isTabMessage(){
    	
    	if(messageFragment==null){
			ft.add(R.id.realtabcontent,new Message_Fragment(), "message");						
		}else{
			ft.attach(messageFragment);						
		}
    }
    
    public void isTabMe(){
    	
    	if(meFragment==null){
			ft.add(R.id.realtabcontent,new Me_Fragment(), "me");						
		}else{
			ft.attach(meFragment);	
		}
    }
    /**
     * 找到Tabhost布局
     */
    public void findTabView(){
    	
    	 tabHost = (TabHost) findViewById(android.R.id.tabhost);
         tabWidget = (TabWidget) findViewById(android.R.id.tabs);
         LinearLayout layout = (LinearLayout)tabHost.getChildAt(0);
         TabWidget tw = (TabWidget)layout.getChildAt(1);
         
         tabIndicator1 = (RelativeLayout) LayoutInflater.from(this)
         		.inflate(R.layout.tab_indicator, tw, false);
         TextView tvTab1 = (TextView)tabIndicator1.getChildAt(1);
         ImageView ivTab1 = (ImageView)tabIndicator1.getChildAt(0);
         ivTab1.setBackgroundResource(R.drawable.selector_mood_home);
         tvTab1.setText(R.string.buttom_home);
         
         tabIndicator2 = (RelativeLayout) LayoutInflater.from(this)
         		.inflate(R.layout.tab_indicator, tw, false);
         TextView tvTab2 = (TextView)tabIndicator2.getChildAt(1);
         ImageView ivTab2 = (ImageView)tabIndicator2.getChildAt(0);
         ivTab2.setBackgroundResource(R.drawable.selector_mood_wall);
         tvTab2.setText(R.string.buttom_wall);
         
         tabIndicator3 = (RelativeLayout) LayoutInflater.from(this)
         		.inflate(R.layout.tab_indicator_camera, tw, false);
         TextView tvTab3 = (TextView)tabIndicator3.getChildAt(1);
         ImageView ivTab3 = (ImageView)tabIndicator3.getChildAt(0);
         ivTab3.setBackgroundResource(R.drawable.selector_mood_photograph);
         tvTab3.setText(R.string.buttom_camera);
          
         tabIndicator4 = (RelativeLayout) LayoutInflater.from(this)
         		.inflate(R.layout.tab_indicator, tw, false);
         TextView tvTab4 = (TextView)tabIndicator4.getChildAt(1);
         ImageView ivTab4 = (ImageView)tabIndicator4.getChildAt(0);
         ivTab4.setBackgroundResource(R.drawable.selector_mood_message);
         tvTab4.setText(R.string.buttom_message);
         
         tabIndicator5 = (RelativeLayout) LayoutInflater.from(this)
         		.inflate(R.layout.tab_indicator, tw, false);
         TextView tvTab5 = (TextView)tabIndicator5.getChildAt(1);
         ImageView ivTab5 = (ImageView)tabIndicator5.getChildAt(0);
         ivTab5.setBackgroundResource(R.drawable.selector_mood_my_wall);
         tvTab5.setText(R.string.buttom_me);
    }
    
    /** 
     * 初始化选项卡
     * 
     * */
    public void initTab(){
    	
        TabHost.TabSpec tSpecHome = tabHost.newTabSpec("home");
        tSpecHome.setIndicator(tabIndicator1);        
        tSpecHome.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecHome);
        
        TabHost.TabSpec tSpecWall = tabHost.newTabSpec("wall");
        tSpecWall.setIndicator(tabIndicator2);        
        tSpecWall.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecWall);
        
        TabHost.TabSpec tSpecCamera = tabHost.newTabSpec("camera");
        tSpecCamera.setIndicator(tabIndicator3);        
        tSpecCamera.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecCamera);
        
        //拍照按钮监听事件,弹出dialog
        tabIndicator3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				Dialog choose = new Dialog(MainActivity.this,R.style.draw_dialog);
		    	choose.setContentView(R.layout.camera_dialog);
		    	// 设置背景模糊参数
				WindowManager.LayoutParams winlp = choose.getWindow()
						.getAttributes();
				winlp.alpha = 0.9f; // 0.0-1.0
				choose.getWindow().setAttributes(winlp);
				choose.show();// 显示弹出框
			}
		});
        
        TabHost.TabSpec tSpecMessage = tabHost.newTabSpec("message");
        tSpecMessage.setIndicator(tabIndicator4);      
        tSpecMessage.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecMessage);
        
        TabHost.TabSpec tSpecMe = tabHost.newTabSpec("me");
        tSpecMe.setIndicator(tabIndicator5);        
        tSpecMe.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecMe);
        
    }
    
}


点击选项卡,然后就new 一个fragment,并且添加进去刚才那个id为realtabcontent的framelayout

看一下fragment怎么写的
package com.mostmood.fragment;

import com.mostmood.R;

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

public class Home_Fragment extends Fragment{

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.moodhome, container, false);
	}

}




猜你喜欢

转载自trylovecatch.iteye.com/blog/1925131