在幻灯片菜单中选择项目时,TabH在Fragment中

时间:2014-02-23 08:08:27

标签: android android-fragments android-tabs android-menu android-nested-fragment

我的项目允许用户可以使用幻灯片菜单功能(左侧)。在幻灯片菜单上,用户可以选择4项:零,一,二和三。我的项目有行动吧。

当用户点击Zero项目时,会显示一个屏幕。当用户点击一,二,三时,屏幕显示并且类似。一,二和三屏幕设计为TabView。您可以看到项目更容易理解:

使用Zero项目运行正常。但是,我在编码一,二,三时遇到问题。当我选择它们时,会发生错误。我不太了解Fragment和Tabhost。

以下是我选择任何项目的代码(在MainActivity.java中):

private void selectItem(int position) {
    // update the main content by replacing fragments
    switch (position) {
        case 0:
            Fragment fragment = new ZeroItem();
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, fragment).commit();
            break;
        case 1: break;
        case 2: break;
        case 3: break;
    }
}

案例0(表示您选择零项目,显示零项目屏幕) ZeroItem.java

package com.example.android.navigationdrawerexample;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

public class ZeroItem extends Fragment {

    public ZeroItem() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_zero_item,
                container, false);
        return rootView;
    }

}

现在,我希望你帮助我编写案例1,案例2,案例3中的代码。它类似。它必须调用TabHost.java

Here is TabHost:
package com.ramesh.fragmenttabhostdemo;

import com.example.android.navigationdrawerexample.R;

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

public class TabHost extends FragmentActivity {

    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bottom_tabs);
        // mTabHost = new FragmentTabHost(this);
        // mTabHost.setup(this, getSupportFragmentManager(),
        // R.id.menu_settings);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        Bundle b = new Bundle();
        b.putString("key", "1");
        mTabHost.addTab(mTabHost.newTabSpec("1").setIndicator("1"),
                OneItem.class, b);
        //
        b = new Bundle();
        System.out.print("hello git");
        b.putString("key", "2");
        mTabHost.addTab(mTabHost.newTabSpec("2")
                .setIndicator("2"), TwoItem.class, b);
        b = new Bundle();
        b.putString("key", "3");
        mTabHost.addTab(mTabHost.newTabSpec("3").setIndicator("3"),
                ThreeItem.class, b);
        // setContentView(mTabHost);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

OneItem.java

package com.ramesh.fragmenttabhostdemo;

import com.example.android.navigationdrawerexample.R;

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

public class OneItem extends Fragment {

    private TextView text;

    public OneItem() {
        // TODO Auto-generated constructor stub

    }

    @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
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.layout,
                null);
        text = (TextView) v.findViewById(R.id.text);
        if (getArguments() != null) {
            //
            try {
                String value = getArguments().getString("key");
                text.setText("1");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return v;
    }

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

}

TwoItem.java

package com.ramesh.fragmenttabhostdemo;


public class TwoItem extends OneItem {

}

ThreeItem.java

package com.ramesh.fragmenttabhostdemo;

import com.ramesh.fragmenttabhostdemo.OneItem;
import com.ramesh.fragmenttabhostdemo.TwoItem;

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

public class ThreeItem extends OneItem {

    private FragmentTabHost mTabHost;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        mTabHost = new FragmentTabHost(getActivity());
        Bundle b = new Bundle();
        b.putString("key", "1");
        mTabHost.addTab(mTabHost.newTabSpec("1").setIndicator("1"),
                OneItem.class, b);
        //
        b = new Bundle();
        b.putString("key", "2");
        mTabHost.addTab(mTabHost.newTabSpec("2")
                .setIndicator("2"), TwoItem.class, b);
        return mTabHost;
    }
}

您可以下载my project here

1 个答案:

答案 0 :(得分:0)

使用getChildFragmentManager()而不是getFragmentManager()。

  

如果此片段是另一个片段的子片段,则为FragmentManager   这里返回的将是父级的getChildFragmentManager()。

引自http://developer.android.com/reference/android/app/Fragment.html