需要帮助使用导航栏

时间:2016-09-24 18:46:10

标签: java android android-layout

我在导航抽屉活动的一个片段中实现了滑动标签视图。

问题是 - 当我第一次打开那个片段时,一切正常。但是当我切换到另一个片段然后返回时,我的滑动选项卡视图中的某些选项卡在某些选项卡正确显示时不会显示任何内容。 而且我在标签中找不到任何缺少返回数据的模式。

以下是我实施它的方式 -

HourlyFragment.java

public class HourlyFragment extends Fragment {

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    private View view;
    private FragmentActivity myContext;

    public HourlyFragment() {
    }

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

        View view = inflater.inflate(R.layout.fragment_hourly, container, false);
        mSectionsPagerAdapter = new SectionsPagerAdapter(myContext.getSupportFragmentManager());
        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        return view;
    }

    @Override
    public void onAttach(Context context) {
        myContext=(FragmentActivity) context;
        super.onAttach(context);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return new Graph();
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return ("Test1").toUpperCase(l);
                case 1:
                    return ("Test2").toUpperCase(l);
                case 2:
                    return ("Test3").toUpperCase(l);
            }
            return null;
        }
    }

}

Graph.java

public class Graph extends Fragment {

    public Graph() {
    }

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

}

graph.xml

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_margin="20dp"
        android:background="@android:color/white"
        android:elevation="5dp"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_menu_camera"/>
    </LinearLayout>
</FrameLayout>

HourlyFragment.xml

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
                android:background="@android:color/white">

                <android.support.v4.view.PagerTabStrip
                    android:id="@+id/pager_tab_strip"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="top"
                    android:background="@android:color/holo_orange_dark"
                    android:textColor="#fff"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />

            </android.support.v4.view.ViewPager>

        </LinearLayout>

注意 - 出于演示目的,我在每个标签中显示相同的片段(图表)。

1 个答案:

答案 0 :(得分:0)

您在Fragment中使用ViewPager。使用 getChildFragmentManager()代替 new SectionsPagerAdapter(myContext.getSupportFragmentManager());

您的代码必须如下所示:

View view = inflater.inflate(R.layout.fragment_hourly, container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(mSectionsPagerAdapter);
return view;

试试吧!