`

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);
	}

}




分享到:
评论

相关推荐

    最新!!!安卓开发——Fragment应用实战

    基于新包 androidx.fragment.app.Fragment androidx.fragment.app.FragmentManager androidx.fragment.app.FragmentTransaction 一个帮助你学习fragment的练手项目

    Android support.v7包

    Android support.v7包

    安卓Android源码——Android ViewPager Fragment.zip

    【Android ViewPager Fragment详解】 在Android应用开发中,ViewPager是一个非常重要的组件,它允许用户通过左右滑动来浏览多个页面,通常用于实现滑动切换的页面效果。本源码包主要探讨了如何在Android中集成和...

    FragmentView

    将fragment和listview结合使用 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment1, null); ...

    安卓Android源码——安卓Android ViewPager Fragment实现选项卡.zip

    本压缩包包含的是关于如何在 Android 中使用 ViewPager 结合 Fragment 实现选项卡功能的源代码示例。 在 Android 中,ViewPager 的主要作用是提供一种平滑的滚动体验,用户可以滑动页面,而不是点击按钮来切换内容...

    安卓Android源码——Fragment实现tab实例代码.zip

    在Android开发中,Fragment是构建动态、可重用的用户界面的一个重要组件。它允许开发者在单个活动中展示多个UI片段。在这个实例中,我们将会深入理解如何使用Fragment来实现tab切换的效果,这对于创建一个多页面的...

    Android Fragment简单应用

    在本教程中,我们将深入探讨如何在Android Studio 3.1.4环境下进行Fragment的简单应用,实现一个Activity中显示一个Fragment。 首先,创建一个新的Android项目。打开Android Studio,选择“Start a new Android ...

    Android fragment切换动画.rar

    在Android应用开发中,Fragment是UI组件的一种,用于在大屏幕设备上实现多屏或复合视图。Fragment可以在Activity之间动态地添加、移除或替换,使得应用在不同屏幕尺寸和配置下都能提供良好的用户体验。"Android ...

    Android Studio —— fragment

    在Android应用开发中,Fragment是Android SDK中的一个重要组件,它被设计用来支持多屏幕适配和复杂的用户界面设计。在Android Studio中,Fragment是应用程序界面的一部分,可以独立于Activity进行部分交互,允许...

    androidx-fragment-1.1.0.aar

    androidx-fragment-1.1.0.aar

    安卓Android源码——Fragment例子.zip

    本资料“安卓Android源码——Fragment例子.zip”提供了关于Fragment的实战示例,帮助开发者深入理解其工作原理和用法。下面我们将详细探讨Fragment的基本概念、生命周期以及如何在实际应用中使用。 Fragment最早在...

    安卓Android源码——基于Fragment实现Tab的切换,滑出侧边栏.zip

    本项目“安卓Android源码——基于Fragment实现Tab的切换,滑出侧边栏.zip”显然是一个示例,演示了如何使用Fragment来创建一个多标签导航界面,并结合侧滑菜单实现更丰富的用户交互。 1. **Fragment基础**: - ...

    智能家居客户端实现——Android Studio版.zip

    智能家居客户端实现——Android Studio版 在当今科技飞速发展的时代,智能家居已经成为许多家庭追求便捷、高效生活的重要组成部分。Android Studio作为Google推出的官方Android应用开发工具,为开发者提供了强大的...

    安卓app开发之Android Fragment使用教程.zip

    安卓app开发之Android Fragment使用教程.zip

    安卓Android源码——基于Fragment实现Tab的切换,滑出侧边栏.rar

    此项目“安卓Android源码——基于Fragment实现Tab的切换,滑出侧边栏”是一个典型的Android应用程序,展示了如何利用Fragment来构建多标签(Tab)界面,并实现侧滑菜单功能。下面我们将深入探讨这个主题。 1. **...

    安卓Android源码——Fragment例子.rar

    本压缩包“安卓Android源码——Fragment例子.rar”提供了一个关于Fragment的实际应用示例,让我们深入探讨Fragment的相关知识点。 1. **Fragment的生命周期**: Fragment拥有自己的生命周期,包括onAttach(), ...

    博客《Fragment详解之四——管理Fragment(2)》源码

    Fragment是Android应用开发中的一个重要组件,它是在Android 3.0(API级别11)引入的,用于在Activity中管理用户界面的模块化部分。Fragment的设计使得开发者可以在一个Activity中包含多个Fragment,甚至在一个...

    安卓Android源码——Fragment实现tab实例 代码.rar

    在安卓应用开发中,`Fragment` 是一个非常重要的组件,它是 Android SDK 提供的一种机制,允许开发者在活动中添加可拆分、复用的用户界面部分。`Fragment` 的使用极大地提高了应用的灵活性和可维护性,尤其是在设计...

Global site tag (gtag.js) - Google Analytics