如何在片段内使用选项卡式导航布局

时间:2019-05-15 12:00:11

标签: java android android-layout android-fragments

我想将选项卡式布局放在片段布局中,我尝试了很多方法,但没有一个起作用。 首先要注意的是,我已经有一个包含4个片段的导航抽屉,我想将选项卡式布局应用于其片段之一而不是全部。

这是我要在其中使用Tabbed布局的片段的JAVA代码,我正在使用android studio v3.3

maxlength

对应的xml文件

package com.androidapp.kjsscapp;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class NoticeBoard extends Fragment {


    public NoticeBoard() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_notice_board, container, false);

        TabLayout tabLayout = v.findViewById(R.id.NoticeBoard_Tab);

        final ViewPager viewPager = v.findViewById(R.id.Mpager1);
        tabpageAdapter tabpageAdapter = new tabpageAdapter(getChildFragmentManager(),tabLayout.getTabCount());
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        return v;
    }

}

标签控制器:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NoticeBoard">

    <android.support.design.widget.TabLayout
        android:id="@+id/NoticeBoard_Tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:visibility="visible"
        app:tabBackground="@color/colorPrimary"
        app:tabIndicatorColor="@android:color/holo_blue_dark"
        app:tabTextColor="@color/White">

        <android.support.design.widget.TabItem
            android:id="@+id/TabItem_general"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="General" />

        <android.support.design.widget.TabItem
            android:id="@+id/TabItem_admissions"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Admissions" />

        <android.support.design.widget.TabItem
            android:id="@+id/TabItem_scholarship"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Scholarships" />
    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/Mpager1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/NoticeBoard_Tab" />

</RelativeLayout>

我希望在调用抽屉的片段后立即在抽屉布局的片段中查看tablayout的片段。请提供解决方案

0 个答案:

没有答案