[英]ViewPager fragments' layout not displayed
I have a child activity that uses CollapsingToolbarLayout and TabLayout. 我有一个使用CollapsingToolbarLayout和TabLayout的子活动。 I need to use the ViewPager to display the tabs' fragments, however the fragments are not displayed at all, even though onCreateView event is called correctly.
我需要使用ViewPager来显示选项卡的片段,但是即使onCreateView事件被正确调用,这些片段也不会显示。
If I replace the ViewPager element for a simple TextView it is displayed correctly. 如果我将ViewPager元素替换为简单的TextView,它将正确显示。
Here is the code I'm using: 这是我正在使用的代码:
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 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 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>
And my Activity class: 而我的Activity类:
@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);
}
}
What is wrong with my code? 我的代码有什么问题?
From a quick look at your code it would seem to me that wrapping your ViewPager
in a NestedScrollView
and a RelativeLayout
is unnecessary. 通过快速查看您的代码,在我看来,将
ViewPager
在NestedScrollView
和RelativeLayout
是不必要的。 You should be able to at least remove the NestedScrollView
and possibly also the RelativeLayout
by moving any necessary attributes to the ViewPager
instead. 你应该能够至少除去
NestedScrollView
可能还有RelativeLayout
通过移动到任何必要的属性ViewPager
代替。
So here's what you could quickly try to verify: 因此,您可以快速尝试进行以下验证:
content_comic_book
with content_chapters
in your activity layout content_chapters
替换对content_comic_book
的包含引用 app:layout_behaviour
attribute to the ViewPager
app:layout_behaviour
属性移动到ViewPager
Make sure that your activity layout has the tab views and the view pager only! 确保您的活动布局仅具有选项卡视图和视图分页器! Then in the fragment class inflate the fragments own layout inside the fragments class.
然后,在fragment类中,在fragment类中填充fragment自己的布局。 Handle all fragment stuff in the fragment class or its layout.
处理片段类或其布局中的所有片段内容。 Only time where you want to be doing fragment work is when transacting or replacing fragments in Java or initializing the pager adapter.
只有在Java中进行事务处理或替换片段或初始化寻呼机适配器时,才需要执行片段工作。 With UI/UX stuff, try and do it as much in the XML files as possible.
使用UI / UX内容,请尝试在XML文件中尽可能多地进行操作。 It gets to be a mess otherwise.
否则,将变得一团糟。
Do something like this for your viewpager adapter: 对您的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;
}
}
This assumes that all these fragements are wanted on app launch (or the first two like the default viewpager setting). 假设所有这些碎片都需要在应用启动时使用(或者前两个如默认的viewpager设置一样)。 My app uses what you see below.
我的应用使用您在下面看到的内容。 It does all the work on launch.
它在启动时完成所有工作。 All you have to do is define the adapter:
您要做的就是定义适配器:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.