Android - ViewPager 未显示片段
Android - ViewPager is not showing the fragments
我在 fragemnt 中使用 tablayout
,TabLayout 有两个片段:一个显示日历,另一个只是简单的纯文本,我可以在选项卡之间正确切换而不会触发任何错误,问题是内容(内部片段)没有显示,它们只是 "not there"!
我尝试查看 Whosebug 上有相同问题的其他线程,但其中大多数都面临 NPE(空指针异常)错误的问题,这不是我的情况。
我尝试使用:getChildFragmentManager()
和 getFragmentManager()
而不是 getSupportFragmentManager()
请记住,我正在使用 Nested fragments
(片段中的片段)
我正在使用 AndroidHive 的文档,我以前也用过它,但是使用简单的片段而不是嵌套片段,它可以完美地工作
我尝试使用布局检查器工具。
问:我错过了什么,我应该怎么做才能把它弄起来 运行?!
到目前为止,这是我的代码:
XML: MainFragment
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager>
</LinearLayout>
XML: 日历片段
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:animateLayoutChanges="true"
android:id="@+id/main_content"
tools:context=".Tabs.CalendarTab">
<com.github.sundeepk.compactcalendarview.CompactCalendarView
android:visibility="visible"
android:id="@+id/compact_calendar"
android:layout_width="match_parent"
android:paddingRight="6dp"
android:paddingLeft="6dp"
android:layout_height="250dp"
app:compactCalendarTargetHeight="250dp"
app:compactCalendarTextSize="12sp"
app:compactCalendarBackgroundColor="@color/colorWhite"
app:compactCalendarTextColor="@color/colorDarkGrey"
app:compactCalendarCurrentDayBackgroundColor="@color/color13"
app:compactCalendarMultiEventIndicatorColor="@color/textColor2"
app:compactCalendarShouldSelectFirstDayOfMonthOnScroll="true"
app:compactCalendarCurrentDayIndicatorStyle="fill_large_indicator"/>
</RelativeLayout>
XML 列表片段
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="250dp"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".Tabs.FullListTab">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="You Are In Tab 2"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainFragmentJAVA
private TabLayout tabLayout;
private ViewPager viewPager;
//onCreate Method:
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
/*** end onCreate() ***/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new CalendarTab(), "Calendar");
adapter.addFragment(new FullListTab(), "List");
viewPager.setAdapter(adapter);
}
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);
}
}
我以前遇到过类似的问题。我通过将 TabLayout
嵌套在 ViewPager
中来解决它,如 in the documentation:
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.v4.view.ViewPager>
</LinearLayout>
adapter.addFragment(new CalendarTab(), "Calendar");
adapter.addFragment(new FullListTab(), "List");
...
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
不要这样做,永远。它会导致非常神秘的错误。您必须 return 来自内部 getItem()
的新 Fragment 实例,而不是来自其他地方。
至于你的实际问题,
XML: MainFragment
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
将此更改为 match_parent
、match_parent
。
我设法通过使用名为 SPager
的第三方库解决了这个问题,这里是 Github link 的完整文档。 SPager
不使用适配器,实施起来更容易、更快捷。
HOWEVER I AM STILL LOOKING FOR A ANOTHER WAY WITHOUT USING A THIRD PARTY LIBRARY
这就是我实际所做的(正如他们 Github 中提到的):
实施
implementation 'com.github.alfianyusufabdullah:spager:1.2.0'
在 XML 布局文件中
<com.alfianyusufabdullah.SPager
android:id="@+id/mPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在我的JAVAclass
private SPager myViewPager;
//...
myViewPager = view.findViewById(R.id.mPager);
//...
myViewPager.initFragmentManager(getChildFragmentManager());
myViewPager.addPages("Inner fragment 1", new IFragOne());
myViewPager.addPages("Inner fragment 2", new IFragTwo());
myViewPager.build();
如果 你正在使用 tabLayout
你可以像这样将它与 SPager
绑定:
myViewPager.addTabLayout(tabLayout);
我还应该提到 SPager
与 Kotlin
兼容
I decided to post, it might be useful to others, for my case this is a temporary fix until, someone answers this thread!
我在 fragemnt 中使用 tablayout
,TabLayout 有两个片段:一个显示日历,另一个只是简单的纯文本,我可以在选项卡之间正确切换而不会触发任何错误,问题是内容(内部片段)没有显示,它们只是 "not there"!
我尝试查看 Whosebug 上有相同问题的其他线程,但其中大多数都面临 NPE(空指针异常)错误的问题,这不是我的情况。
我尝试使用:
getChildFragmentManager()
和getFragmentManager()
而不是getSupportFragmentManager()
请记住,我正在使用
Nested fragments
(片段中的片段)我正在使用 AndroidHive 的文档,我以前也用过它,但是使用简单的片段而不是嵌套片段,它可以完美地工作
我尝试使用布局检查器工具。
问:我错过了什么,我应该怎么做才能把它弄起来 运行?!
到目前为止,这是我的代码:
XML: MainFragment
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager>
</LinearLayout>
XML: 日历片段
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:animateLayoutChanges="true"
android:id="@+id/main_content"
tools:context=".Tabs.CalendarTab">
<com.github.sundeepk.compactcalendarview.CompactCalendarView
android:visibility="visible"
android:id="@+id/compact_calendar"
android:layout_width="match_parent"
android:paddingRight="6dp"
android:paddingLeft="6dp"
android:layout_height="250dp"
app:compactCalendarTargetHeight="250dp"
app:compactCalendarTextSize="12sp"
app:compactCalendarBackgroundColor="@color/colorWhite"
app:compactCalendarTextColor="@color/colorDarkGrey"
app:compactCalendarCurrentDayBackgroundColor="@color/color13"
app:compactCalendarMultiEventIndicatorColor="@color/textColor2"
app:compactCalendarShouldSelectFirstDayOfMonthOnScroll="true"
app:compactCalendarCurrentDayIndicatorStyle="fill_large_indicator"/>
</RelativeLayout>
XML 列表片段
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="250dp"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".Tabs.FullListTab">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="You Are In Tab 2"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainFragmentJAVA
private TabLayout tabLayout;
private ViewPager viewPager;
//onCreate Method:
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
/*** end onCreate() ***/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new CalendarTab(), "Calendar");
adapter.addFragment(new FullListTab(), "List");
viewPager.setAdapter(adapter);
}
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);
}
}
我以前遇到过类似的问题。我通过将 TabLayout
嵌套在 ViewPager
中来解决它,如 in the documentation:
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.v4.view.ViewPager>
</LinearLayout>
adapter.addFragment(new CalendarTab(), "Calendar"); adapter.addFragment(new FullListTab(), "List"); ... class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>();
不要这样做,永远。它会导致非常神秘的错误。您必须 return 来自内部 getItem()
的新 Fragment 实例,而不是来自其他地方。
至于你的实际问题,
XML: MainFragment <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content">
将此更改为 match_parent
、match_parent
。
我设法通过使用名为 SPager
的第三方库解决了这个问题,这里是 Github link 的完整文档。 SPager
不使用适配器,实施起来更容易、更快捷。
HOWEVER I AM STILL LOOKING FOR A ANOTHER WAY WITHOUT USING A THIRD PARTY LIBRARY
这就是我实际所做的(正如他们 Github 中提到的):
实施
implementation 'com.github.alfianyusufabdullah:spager:1.2.0'
在 XML 布局文件中
<com.alfianyusufabdullah.SPager
android:id="@+id/mPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在我的JAVAclass
private SPager myViewPager;
//...
myViewPager = view.findViewById(R.id.mPager);
//...
myViewPager.initFragmentManager(getChildFragmentManager());
myViewPager.addPages("Inner fragment 1", new IFragOne());
myViewPager.addPages("Inner fragment 2", new IFragTwo());
myViewPager.build();
如果 你正在使用 tabLayout
你可以像这样将它与 SPager
绑定:
myViewPager.addTabLayout(tabLayout);
我还应该提到 SPager
与 Kotlin
I decided to post, it might be useful to others, for my case this is a temporary fix until, someone answers this thread!