ViewPager片段'布局未显示

时间:2016-01-17 00:08:24

标签: android android-fragments android-viewpager android-collapsingtoolbarlayout

我有一个使用CollapsingToolbarLayout和TabLayout的子活动。我需要使用ViewPager来显示选项卡'片段,但片段根本不显示,即使正确调用onCreateView事件。

如果我将ViewPager元素替换为简单的TextView,它就会正确显示。

以下是我使用的代码:

活动

<?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"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ui.ComicBookActivity">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:background="@color/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="210dip"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            android:fitsSystemWindows="true"
            android:id="@+id/collapsing_toolbar">

            <com.facebook.drawee.view.SimpleDraweeView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_collapseMode="parallax"
                android:id="@+id/imageViewProfile"
                android:scaleType="fitStart"
                fresco:placeholderImageScaleType="centerCrop"
                android:fitsSystemWindows="true"
                />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="pin">


            </android.support.v7.widget.Toolbar>


        </android.support.design.widget.CollapsingToolbarLayout>
        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:scrollbars="horizontal"
            android:layout_below="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/tabColor"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabMode="scrollable"/>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_comic_book" />


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

content_comic_book.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:scrollbars="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include layout="@layout/content_chapters" />

</android.support.v4.widget.NestedScrollView>

content_chapters.xml

<?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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".ui.ComicBookActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/light_background"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

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

</RelativeLayout>

我的活动课程:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

// Configuring the toolbar as the actionbar
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        setupTabLayout(tabLayout);

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new AboutComicBookFragment(), getString(R.string.label_about));
        adapter.addFragment(new ComicBookChaptersFragment(), getString(R.string.label_chapters));
        viewPager.setAdapter(adapter);
    }

    private void setupTabLayout(TabLayout tabLayout) {
        tabLayout.setupWithViewPager(viewPager);
    }


    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

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

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

我的代码出了什么问题?

2 个答案:

答案 0 :(得分:1)

通过快速查看代码,我认为将ViewPager包裹在NestedScrollViewRelativeLayout中是不必要的。您应该至少删除NestedScrollView,也可以删除RelativeLayout,方法是将所有必要的属性移至ViewPager

所以,您可以快速尝试验证:

  • 将您的包含引用替换为活动布局中content_comic_book的{​​{1}}
  • 请务必将content_chapters属性移至app:layout_behaviour

答案 1 :(得分:0)

确保您的活动布局仅包含标签视图和视图寻呼机! 然后在片段类中膨胀碎片类中的片段自己的布局。处理片段类或其布局中的所有片段内容。只有在您想要进行片段工作的时候才是在Java中处理或替换片段或初始化寻呼机适配器。使用UI / UX的东西,尝试尽可能多地在XML文件中执行。否则会变得一团糟。

为viewpager适配器执行类似的操作:

 @Override
public Fragment getItem(int i) {
    switch(i) {
        case 0: 
           //viewpager's page 1 (first tab)
           return new XFragment(); break; //fragment class
        case 1: 
           //viewpager's page 2 (second tab)
           return new YFragment(); break;
        defualt: return null; break;
    }
}

这假设在应用程序启动时需要所有这些功能(或者前两个类似于默认的viewpager设置)。 我的应用使用您在下面看到的内容。它完成了发布的所有工作。您所要做的就是定义适配器:

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        switch (position) {
            case 0:
                return new WriteFragment();
            case 1:
                return new PreviewFragment();
            case 2:
                return new ListFragment();
            case 3:
                return new RhymeFragment();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        // Show tab pages count.
        return TAB_PAGE_COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return getString(R.string.save_btn);
            case 1:
                return getString(R.string.preview_btn);
            case 2:
                return getString(R.string.find_btn);
            case 3:
                return getString(R.string.rhyme_btn);
        }
        return null;
    }