ViewPager不使用match_parent

时间:2016-12-13 15:21:39

标签: android android-fragments android-viewpager fragment

我的基本活动包含AppBarLayout toolbartablayout。有一个bottombarcontentframe来加载片段。现在有一个片段,其中有一个viewpager,它将加载所选标签的片段。

现在的问题是,无论何时我在viewpager中加载片段,viewpager的高度都是0。我无法看到加载的片段。

如果我将400dp左右的高度指定为viewpager,那么它就会显示并且正常工作。

工具栏xml:

<?xml version="1.0" encoding="utf-8"?>
<merge 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="wrap_content">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbarlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways"/>
    </android.support.design.widget.AppBarLayout>
</merge>

活动xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/activity_job_landing"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".xxx.xxxx.xxxx">

    <include
        layout="@layout/layout_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/myScrollingContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout
            android:id="@+id/framelayout_joblanding_contentframe"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.NestedScrollView>

    <com.roughike.bottombar.BottomBar
        android:id="@+id/bottom_bar_job_landing"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="bottom"
        android:background="@color/indigo900"
        app:bb_activeTabAlpha="1"
        app:bb_activeTabColor="@color/white"
        app:bb_behavior="shy"
        app:bb_inActiveTabAlpha="0.8"
        app:bb_inActiveTabColor="#B6BDBF"
        app:bb_tabXmlResource="@xml/job_landing_bottom_bar_tabs" />
</android.support.design.widget.CoordinatorLayout>

片段xml:

<LinearLayout 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"
        android:orientation="vertical"
        tools:context="com.xxx.xxxx.xxx"
        android:background="@color/bluegrey50">

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

</LinearLayout>

Fragment Java:

public class ApplicantFragment extends Fragment {

    @BindView(R.id.viewpager_job_applicant)
    ViewPager mViewPager;

    //@BindView(R.id.tab_layout_job_applicant)
    TabLayout mTabLayout;

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


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

        ButterKnife.bind(this, view);

        mTabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout_job_applicant);
        mViewPager.setAdapter(new ApplicantFragmentPagerAdapter(getChildFragmentManager()));
        mTabLayout.setupWithViewPager(mViewPager);
        return view;
    }


    class ApplicantFragmentPagerAdapter extends FragmentPagerAdapter {
        final int PAGE_COUNT = 7;
        private final String tabTitles[] =
                new String[]{"Fresher", "Next Round", "Hold",
                        "Reject", "TCFI", "CFI", "Hired"};

        public ApplicantFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            CandidateListFragment candidateListFragment;
            candidateListFragment = CandidateListFragment.newInstance();
            return candidateListFragment;
        }

        @Override
        public int getCount() {
            return tabTitles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // Generate title based on item position
            return tabTitles[position];
        }
    }
}

加载的片段:

<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"
    tools:context="xxx.xxxx.xxxx">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

0 个答案:

没有答案