TabHost与片段和FragmentActivity [英] TabHost with Fragments and FragmentActivity

查看:174
本文介绍了TabHost与片段和FragmentActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序,我想用3个标签使用片段每个标签导航,但我不知道如何创建的结构做。

我要单独添加的每个片段,因为每个人都是不同的,但我不知道在哪里,将其添加在FragmentActivity。

我拥有这些文件。

tabs_layout.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_height =match_parent
    机器人:layout_width =match_parent>

    < TabHost机器人:ID =@机器人:ID / tabhost
         机器人:layout_width =match_parent
         机器人:layout_height =match_parent>

         <的LinearLayout
                机器人:方向=垂直
                机器人:layout_width =match_parent
                机器人:layout_height =match_parent>

             < TabWidget
                机器人:ID =@机器人:ID /标签
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
             />

             <的FrameLayout
                 机器人:ID =@机器人:ID / tabcontent
                 机器人:layout_width =match_parent
                 机器人:layout_height =match_parent

             >

                 <的FrameLayout
                     机器人:ID =@ + ID / tabRateAPet
                     机器人:layout_width =match_parent
                     机器人:layout_height =match_parent

                 />

                 <的FrameLayout
                     机器人:ID =@ + ID / tabViewMyRates
                     机器人:layout_width =match_parent
                     机器人:layout_height =match_parent

                 />

                 <的FrameLayout
                     机器人:ID =@ + ID / tabViewGlobalRates
                     机器人:layout_width =match_parent
                     机器人:layout_height =match_parent

                 />


             < /的FrameLayout>
        < / LinearLayout中>
    < / TabHost>
< / LinearLayout中>
 

TabsMain.java

 进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;

公共类MainTabsActivity扩展FragmentActivity {
公共静态最后弦乐RATE_A_PET =评价一个宠物;
公共静态最后弦乐MY_R​​ATES =我的价格;
公共静态最后弦乐GLOBAL_RATES =全球价格;

@覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.tabs_layout);
 }
}
 

Tabs.java

 进口android.app.Activity;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentManager;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TabHost;
进口android.widget.TabHost.OnTabChangeListener;
进口android.widget.TabHost.TabSpec;
进口android.widget.TextView;

公共类标签扩展片段实现OnTabChangeListener {
    私有静态最后字符串变量=FragmentTabs;
    公共静态最后弦乐RATE_A_PET =评价一个宠物;
    公共静态最后弦乐MY_R​​ATES =我的价格;
    公共静态最后弦乐GLOBAL_RATES =全球价格;

    私人查看mRoot;
    私人TabHost mTab​​Host;
    私人诠释mCurrentTab;

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
// super.onCreateView(充气,容器,savedInstanceState);
        mRoot = inflater.inflate(R.layout.tabs_layout,NULL);
        mTabHost =(TabHost)mRoot.findViewById(android.R.id.tabhost);
        setupTabs();
        返回mRoot;
    }

    私人无效setupTabs(){
        mTabHost.setup(); // 重要!
        mTabHost.addTab(newTab(RATE_A_PET,R.string.tabRateAPet,R.id.tabRateAPet));
        mTabHost.addTab(newTab(MY_RATES,R.string.tabViewMyRates,R.id.tabViewMyRates));
    }

    私营则tabspec newTab(字符串标记,诠释labelId,诠释tabContentId){
        Log.d(TAG,buildTab():标签=+标签);

        查看指示灯= LayoutInflater.from(getActivity())。膨胀(
                R.layout.tab,
                (ViewGroup中)mRoot.findViewById(android.R.id.tabs),FALSE);
        ((TextView中)indicator.findViewById(R.id.text))的setText(labelId)。

        则tabspec则tabspec = mTab​​Host.newTabSpec(标签);
        tabSpec.setIndicator(指标);
        tabSpec.setContent(tabContentId);
        返回则tabspec;
    }


    @覆盖
    公共无效onTabChanged(字符串tabId){
        Log.d(TAG,onTabChanged():tabId =+ tabId);
        如果(RATE_A_PET.equals(tabId)){
            updateTab(tabId,R.id.tabRateAPet);
            mCurrentTab = 0;
            返回;
        }
        如果(MY_RATES.equals(tabId)){
            updateTab(tabId,R.id.tabViewMyRates);
            mCurrentTab = 1;
            返回;
        }
        如果(GLOBAL_RATES.equals(tabId)){
            updateTab(tabId,R.id.tabViewGlobalRates);
            mCurrentTab = 2;
            返回;
        }
    }
    私人无效updateTab(字符串tabId,INT占位符){
        FragmentManager FM = getFragmentManager();
        如果(fm.findFragmentByTag(tabId)== NULL){
            fm.beginTransaction()
                    .replace(占位符,新RateMyPetActivity(),tabId)
                    。承诺();
        }
    }

}
 

解决方案

我会建议创建一个独立的片段文件为每个标签。我最近做这个一样,所以我提出以下我的code:

布局文件

activity_main.xml

 < android.support.v4.app.FragmentTabHost
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@机器人:ID / tabhost
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

<的LinearLayout
    机器人:方向=垂直
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    < TabWidget
        机器人:ID =@机器人:ID /标签

        机器人:方向=横向
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =0/>

    <的FrameLayout
        机器人:ID =@机器人:ID / tabcontent
        机器人:layout_width =0dp
        机器人:layout_height =0dp
        机器人:layout_weight =0/>

    <的FrameLayout
        机器人:ID =@ + ID / realtabcontent
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_weight =1/>

< / LinearLayout中>
< /android.support.v4.app.FragmentTabHost>
 

tab1_view.xml //使用这个格式(一定要改变字符串变量)添加相应的标签布局

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:工具=htt​​p://schemas.android.com/tool​​s
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直
        工具:上下文=DeviceFragment。>

    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/ tab1_fragment_string/>

< / LinearLayout中>
 

SRC文件

MainActivity.java //通知,在 .addTab 过程中,我只用文字。您还可以添加使用可绘制,你将需要添加到您的华电国际文件夹图标。我也只创造了在这个例子中三个标签。

 包com.example.applicationname;

进口android.os.Bundle;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentTabHost;

公共类MainActivity扩展FragmentActivity {
    //片段TabHost作为mTabHost
    私人FragmentTabHost mTab​​Host;

    @覆盖
    保护无效的onCreate(包savedInstanceState){

        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        mTabHost =(FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(这一点,getSupportFragmentManager(),R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec(TAB1)。setIndicator(TAB1),
            Tab1Fragment.class,NULL);
        mTabHost.addTab(mTabHost.newTabSpec(TAB2)。setIndicator(TAB2),
            Tab2Fragment.class,NULL);
        mTabHost.addTab(mTabHost.newTabSpec(TAB3)。setIndicator(TAB3),
            Tab3Fragment.class,NULL);
    }
}
 

Tab1Fragment.java //再次复制的标签所需的数量

 包com.example.applicationname;

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

公共类Tab1Fragment扩展片段{

 @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){
        //充气的布局该片段
        视图V = inflater.inflate(R.layout.tab1_view,集装箱,假);

        返回伏;
    }
}
 

请确保您的R.java和strings.xml中的文件设置正确,然后你的标签应该是启动和运行。

I'm working on an Android App and I want to use 3 tabs for navigation using Fragments for each tab, but I don't know how to create the structure for doing it.

I want to add each fragment separately because each one is different, but I don't know where to add them in FragmentActivity.

I have these files.

tabs_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

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

             <TabWidget
                android:id="@android:id/tabs" 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
             />

             <FrameLayout
                 android:id="@android:id/tabcontent" 
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"

             >

                 <FrameLayout
                     android:id="@+id/tabRateAPet" 
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"

                 />

                 <FrameLayout
                     android:id="@+id/tabViewMyRates" 
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"

                 />

                 <FrameLayout
                     android:id="@+id/tabViewGlobalRates" 
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"

                 />


             </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

TabsMain.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

public class MainTabsActivity extends FragmentActivity {
public static final String RATE_A_PET = "Rate a Pet";
public static final String MY_RATES = "My Rates";
public static final String GLOBAL_RATES = "Global Rates";

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs_layout);
 }
}

Tabs.java

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class Tabs extends Fragment implements OnTabChangeListener {
    private static final String TAG = "FragmentTabs";
    public static final String RATE_A_PET = "Rate a Pet";
    public static final String MY_RATES = "My Rates";
    public static final String GLOBAL_RATES = "Global Rates";

    private View mRoot;
    private TabHost mTabHost;
    private int mCurrentTab;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
//      super.onCreateView(inflater, container, savedInstanceState);
        mRoot = inflater.inflate(R.layout.tabs_layout, null);
        mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
        setupTabs();
        return mRoot;
    }

    private void setupTabs() {
        mTabHost.setup(); // important!
        mTabHost.addTab(newTab(RATE_A_PET, R.string.tabRateAPet, R.id.tabRateAPet));
        mTabHost.addTab(newTab(MY_RATES, R.string.tabViewMyRates, R.id.tabViewMyRates));
    }

    private TabSpec newTab(String tag, int labelId, int tabContentId) {
        Log.d(TAG, "buildTab(): tag=" + tag);

        View indicator = LayoutInflater.from(getActivity()).inflate(
                R.layout.tab,
                (ViewGroup) mRoot.findViewById(android.R.id.tabs), false);
        ((TextView) indicator.findViewById(R.id.text)).setText(labelId);

        TabSpec tabSpec = mTabHost.newTabSpec(tag);
        tabSpec.setIndicator(indicator);
        tabSpec.setContent(tabContentId);
        return tabSpec;
    }


    @Override
    public void onTabChanged(String tabId) {
        Log.d(TAG, "onTabChanged(): tabId=" + tabId);
        if (RATE_A_PET.equals(tabId)) {
            updateTab(tabId, R.id.tabRateAPet);
            mCurrentTab = 0;
            return;
        }
        if (MY_RATES.equals(tabId)) {
            updateTab(tabId, R.id.tabViewMyRates);
            mCurrentTab = 1;
            return;
        }
        if (GLOBAL_RATES.equals(tabId)) {
            updateTab(tabId, R.id.tabViewGlobalRates);
            mCurrentTab = 2;
            return;
        }
    }   
    private void updateTab(String tabId, int placeholder) {
        FragmentManager fm = getFragmentManager();
        if (fm.findFragmentByTag(tabId) == null) {
            fm.beginTransaction()
                    .replace(placeholder, new RateMyPetActivity(), tabId)
                    .commit();
        }
    }

}

解决方案

I would suggest creating a separate fragment file for each tab. I recently did this as well, so I have outlined my code below:

Layout Files

activity_main.xml

<android.support.v4.app.FragmentTabHost
    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">

    <TabWidget
        android:id="@android:id/tabs"

        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0"/>

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

</LinearLayout>
</android.support.v4.app.FragmentTabHost>

tab1_view.xml //add your respective tab layouts using this format (make sure to change string variables)

<?xml version="1.0" encoding="utf-8"?>
<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=".DeviceFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tab1_fragment_string" />

</LinearLayout>

SRC Files

MainActivity.java //notice that in the .addTab process I only used text. You can also add icons using drawables that you would need to add to your hdpi folder. I also only created three tabs in this example.

package com.example.applicationname;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;

public class MainActivity extends FragmentActivity {
    // Fragment TabHost as mTabHost
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
            Tab1Fragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
            Tab2Fragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"),
            Tab3Fragment.class, null);
    }
}

Tab1Fragment.java //once again replicate for desired number of tabs

package com.example.applicationname;

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

public class Tab1Fragment extends Fragment  {

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View V = inflater.inflate(R.layout.tab1_view, container, false);

        return V;
    }
}

Make sure that your R.java and strings.xml files are properly set up, and then your tabs should be up and running.

这篇关于TabHost与片段和FragmentActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆