未显示 ViewPager 片段的布局
ViewPager fragments' layout not displayed
我有一个使用 CollapsingToolbarLayout 和 TabLayout 的 child activity。我需要使用 ViewPager 来显示选项卡的片段,但是即使正确调用了 onCreateView 事件,片段也根本不显示。
如果我将 ViewPager 元素替换为简单的 TextView,它会正确显示。
这是我使用的代码:
Activity
<?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>
还有我的Activityclass:
@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);
}
}
我的代码有什么问题?
确保您的 activity 布局只有选项卡视图和视图分页器!
然后在片段 class 中膨胀片段自己在片段 class 内的布局。处理片段 class 或其布局中的所有片段内容。只有在 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;
}
通过快速查看您的代码,在我看来,将您的 ViewPager
包装在 NestedScrollView
和 RelativeLayout
中是不必要的。通过将任何必要的属性移至 ViewPager
,您至少应该能够移除 NestedScrollView
,也可能移除 RelativeLayout
。
下面是您可以快速验证的内容:
- 在 activity 布局中用
content_chapters
替换对 content_comic_book
的引用
- 记得把
app:layout_behaviour
属性移到ViewPager
我有一个使用 CollapsingToolbarLayout 和 TabLayout 的 child activity。我需要使用 ViewPager 来显示选项卡的片段,但是即使正确调用了 onCreateView 事件,片段也根本不显示。
如果我将 ViewPager 元素替换为简单的 TextView,它会正确显示。
这是我使用的代码:
Activity
<?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>
还有我的Activityclass:
@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);
}
}
我的代码有什么问题?
确保您的 activity 布局只有选项卡视图和视图分页器! 然后在片段 class 中膨胀片段自己在片段 class 内的布局。处理片段 class 或其布局中的所有片段内容。只有在 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;
}
通过快速查看您的代码,在我看来,将您的 ViewPager
包装在 NestedScrollView
和 RelativeLayout
中是不必要的。通过将任何必要的属性移至 ViewPager
,您至少应该能够移除 NestedScrollView
,也可能移除 RelativeLayout
。
下面是您可以快速验证的内容:
- 在 activity 布局中用
content_chapters
替换对content_comic_book
的引用 - 记得把
app:layout_behaviour
属性移到ViewPager