Fragment里面的Android Tab Layout没有加载标签内容

时间:2015-10-24 05:55:29

标签: android android-viewpager material-design android-tabs android-tablayout

我的主要活动包含navigation drawercontainernavigation drawer包含选项列表。抽屉选项将container替换为fragments。{{1当我点击fragtab选项时加载container布局tab,但是当我尝试通过点击相同选项第二次加载它时,它没有显示fragment内容。

片段xml布局

tab

Fragment class FragTabs.java

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            card_view:cardBackgroundColor="@android:color/white"
            card_view:cardCornerRadius="2dp">


            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


                <LinearLayout
                    android:id="@+id/linearLayout_search"
                    android:layout_width="match_parent"
                    android:layout_height="48dp">


                    <ImageView
                        android:id="@+id/image_search_back"
                        android:layout_width="48dp"
                        android:layout_height="48dp"
                        android:background="?android:attr/selectableItemBackground"
                        android:clickable="true"

                        android:padding="12dp"
                        android:src="@mipmap/ic_search_black_24dp" />


                    <EditText
                        android:id="@+id/edit_text_search"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:background="#fff"
                        android:gravity="center_vertical"
                        android:hint="Search"
                        android:imeOptions="actionSearch"
                        android:inputType="textCapWords"
                        android:paddingLeft="12dp"
                        android:paddingRight="8dp"
                        android:singleLine="true"
                        android:textColor="@color/colorAccent"
                        android:textColorHint="@color/colorAccent" />

                </LinearLayout>

            </RelativeLayout>
        </android.support.v7.widget.CardView>


        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:tabGravity="fill"
            app:tabIndicatorColor="@android:color/white"
            app:tabMode="fixed"
            app:tabSelectedTextColor="#F4F1F1"
            app:tabTextColor="@android:color/white" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/add_icon_white" />

主要活动xml

public class FragTabs extends Fragment {


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

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_frag_tabs, container, false);
    viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) view.findViewById(R.id.tabs);
    //tabLayout.setupWithViewPager(viewPager);

    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });

    FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (viewPager.getCurrentItem() == 0) {
                getFragmentManager().beginTransaction().replace(R.id.profile_container, new Addplaymates()).commit();
            } else {
                getFragmentManager().beginTransaction().replace(R.id.profile_container, new Addplaygroups()).commit();
            }
        }
    });
    return view;
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());

    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {

    CharSequence Titles[] = {"Frag A", "Frag B"};

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment f = null;
        switch (position) {
            case 0:
                FragA tab1 = new FragA();
                f = tab1;
                break;
            case 1:
                FragB tab2 = new FragB();
                f = tab2;
                break;
        }
        return f;
    }

    @Override
    public int getCount() {
        return 2;
    }

    /*public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }*/

    @Override
    public CharSequence getPageTitle(int position) {
        return Titles[position];
    }
}}

导航抽屉。

  <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/conta"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/app_bar"
            layout="@layout/app_bar" />

        <FrameLayout
            android:id="@+id/profile_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/app_bar" />

    </RelativeLayout>

    <fragment
        android:id="@+id/navigation_drawer"
        android:name="com.kshediv.playforever.Profile.NaviagtionFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_naviagtion"
        tools:layout="@layout/fragment_naviagtion" />
</android.support.v4.widget.DrawerLayout>

主要活动

public class NaviagtionFragment extends Fragment {


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

private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;

public void setUp(DrawerLayout dl, Toolbar toolbar) {
    mDrawerLayout = dl;
    mDrawerToggle = new ActionBarDrawerToggle(getActivity(), dl, toolbar, R.string.DrawerOpn, R.string.DrawerClose) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getActivity().invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            getActivity().invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });
}


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

    FrameLayout home = (FrameLayout) view.findViewById(R.id.home);
    home.setOnClickListener(onClickListener);

    FrameLayout fragtabs = (FrameLayout) view.findViewById(R.id.fragtabs);
    playmates.setOnClickListener(onClickListener);

    FrameLayout settings = (FrameLayout) view.findViewById(R.id.settings);
    playmates.setOnClickListener(onClickListener);

    return view;
}

public View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home:
                getFragmentManager().beginTransaction().replace(R.id.profile_container, new Home()).commit();
                mDrawerLayout.closeDrawers();
                break;
            case R.id.playmates:
                getFragmentManager().beginTransaction().replace(R.id.profile_container, new PlayTabs()).commit();
                mDrawerLayout.closeDrawers();
                break;
            case R.id.performance:
                getFragmentManager().beginTransaction().replace(R.id.profile_container, new Settings()).commit();
                mDrawerLayout.closeDrawers();
                break;
        }
    }
};}

谢谢,

2 个答案:

答案 0 :(得分:11)

尝试使用

class ViewPagerAdapter extends FragmentStatePagerAdapter {
...
}

而不是

class ViewPagerAdapter extends FragmentPagerAdapter {
...
}

答案 1 :(得分:0)

用于viewpager适配器实现 FragmentStatePagerAdapter ,而不是 FragmentPagerAdapter

它将以选项卡布局加载片段,而不会出现任何问题。