Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 标签don';无法从从其中一个选项卡导航的子片段工作_Android_Android Fragments_Tabs - Fatal编程技术网

Android 标签don';无法从从其中一个选项卡导航的子片段工作

Android 标签don';无法从从其中一个选项卡导航的子片段工作,android,android-fragments,tabs,Android,Android Fragments,Tabs,我正在开发一个Android应用程序,我已经有3个标签在使用每个标签的片段。在其中一个选项卡(Tab3Fragment)中,我导航到另一个片段(NewExampleFragment)。 当我尝试从子片段(NewExampleFragment)导航回选项卡片段(Tab3Fragment)时,就会出现问题。从子片段中可以看到这些选项卡,但单击它们不会产生任何效果。我希望能够从子片段访问这3个选项卡。如何让它工作 层次结构是:appcompativity->Fragment(tabs)->Child

我正在开发一个Android应用程序,我已经有3个标签在使用每个标签的片段。在其中一个选项卡(Tab3Fragment)中,我导航到另一个片段(NewExampleFragment)。 当我尝试从子片段(NewExampleFragment)导航回选项卡片段(Tab3Fragment)时,就会出现问题。从子片段中可以看到这些选项卡,但单击它们不会产生任何效果。我希望能够从子片段访问这3个选项卡。如何让它工作

层次结构是:appcompativity->Fragment(tabs)->Child Fragment(我想从这里再次访问这些选项卡)

活动:

public class WordPageActivity extends AppCompatActivity {

private FragmentTabHost mTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_word_page);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_word_page);
    setSupportActionBar(toolbar);

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator(getString(R.string.tab1_text), null),
            Tab1Fragment.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator(getString(R.string.tab2_text), null),
            Tab2Fragment.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator(getString(R.string.tab3_text), null),
            Tab3Fragment.class, null);
}
活动布局:

<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/content_frame_word_page"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

   <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_word_page"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:elevation="4dp"
        android:theme="@style/AppTheme.NoActionBar"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"/>

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

</LinearLayout>
}

子片段:

public class NewExampleFragment extends Fragment implements View.OnClickListener  {

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

    View rootView = inflater.inflate(R.layout.fragment_new_example, container, false);


    return rootView;
}

我试着用TabLayout,但问题还是一样。但是,我通过更改我在onOpenExamplePage方法中替换的布局来解决此问题:

public void onOpenExamplePage(View v){
    getFragmentManager().beginTransaction().replace(android.R.id.tabcontent, new NewExampleFragment()).commit();
}
它应该是android.R.id.tabcontent,而不是R.id.content\u frame\u word\u页面,因为在活动布局中,我要替换的部分是

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


不是整个线性布局。

尝试使用
TabLayout
。这个链接将帮助你。只需在
NewExampleFragment
内制作一个容器即可。然后,第一次,加载父片段时,单击将其更改为子片段,然后再按返回键将其切换为父片段。
<FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />