Java Android-ViewPager没有显示片段

Java Android-ViewPager没有显示片段,java,android,android-fragments,android-viewpager,android-tablayout,Java,Android,Android Fragments,Android Viewpager,Android Tablayout,我在fragemnt内部使用tablayout,tablayout有两个片段:一个是显示日历,另一个只是简单的纯文本,我可以在选项卡之间正确切换,不会触发任何错误,问题是内容(内部片段)没有显示,它们只是“不存在” 我尝试研究StackoverFlow上存在相同问题的其他线程,但大多数线程都面临NPEs(空指针异常)错误的问题,而我的情况并非如此 我尝试使用:getChildFragmentManager()和getFragmentManager()而不是getSupportFragmentM

我在fragemnt内部使用
tablayout
,tablayout有两个片段:一个是显示日历,另一个只是简单的纯文本,我可以在选项卡之间正确切换,不会触发任何错误,问题是内容(内部片段)没有显示,它们只是“不存在”

  • 我尝试研究StackoverFlow上存在相同问题的其他线程,但大多数线程都面临NPEs(空指针异常)错误的问题,而我的情况并非如此

  • 我尝试使用:
    getChildFragmentManager()
    getFragmentManager()
    而不是
    getSupportFragmentManager()

  • 请记住,我使用的是
    嵌套片段
    (片段中的片段)

  • 我使用的是的文档,我以前也使用过它,但是对于简单的片段(不是嵌套的片段),它可以完美地工作

  • 我尝试使用布局检查器工具

Q:我错过了什么?我应该做些什么来启动并运行它

以下是我目前的代码:

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

XML:Calendar片段

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

XML列表片段

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

MainFragment JAVA

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);
    }
}
专用表格布局;
私人视页机视页机;
//onCreate方法:
viewPager=(viewPager)view.findViewById(R.id.viewPager);
设置viewPager(viewPager);
tabLayout=(tabLayout)view.findviewbyd(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
/***结束onCreate()***/
专用无效设置ViewPager(ViewPager ViewPager){
ViewPagerAdapter适配器=新的ViewPagerAdapter(getChildFragmentManager());
addFragment(新的CalendarTab(),“Calendar”);
addFragment(新的FullListTab(),“List”);
viewPager.setAdapter(适配器);
}
类ViewPagerAdapter扩展了FragmentPagerAdapter{
私有最终列表MFFragmentList=new ArrayList();
私有最终列表MFFragmentTitleList=new ArrayList();
公共视图页面编辑器(碎片管理器){
高级经理;
}
@凌驾
公共片段getItem(int位置){
返回MFFragmentList.get(位置);
}
@凌驾
public int getCount(){
返回MFFragmentList.size();
}
public void addFragment(片段片段,字符串标题){
添加(片段);
MFFragmentTitleList.add(标题);
}
@凌驾
公共字符序列getPageTitle(int位置){
返回MFFragmentTitleList.get(位置);
}
}

我过去也有类似的问题。我通过在
ViewPager
中嵌套
TabLayout
解决了这个问题,如图所示:



将此更改为
match\u parent
match\u parent

我通过使用名为
SPager
的第三方库解决了此问题。以下是完整文档的详细信息
SPager
不使用适配器,实现起来更简单、更快

但我仍在寻找另一种不使用第三方库的方法

这就是我实际做的(正如他们在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" />
如果您使用的是
表格布局
,您可以将其与
SPager
绑定,如下所示:

myViewPager.addTabLayout(tabLayout);
我还应该提到,
SPager
Kotlin

implementation 'com.github.alfianyusufabdullah:spager:1.2.0'
我决定发布,它可能对其他人有用,对于我的情况,这是一个临时修复,直到有人回答这个帖子


什么?不,你不知道,我几周前就遇到了这个问题,通过嵌套表格布局解决了这个问题。除非是另一个bug通过这样做得到了“修复”:P无论如何,我遵循文档:嗯,
作为ViewPager装饰的一部分。。我不知道那是什么意思。但是,是的,显然你可以把它放在一个寻呼机里。不过,我认为你不必这么做。即使它不是,它也能工作。是的,我一开始并没有嵌套它,因为大多数在线示例也不这么做。我也不理解“装饰”部分,但我试着把它嵌套起来,它解决了我的问题。我的问题与OP的问题相似,因此“你必须筑巢”。我想我会把答案编辑得更清楚。@RicardoCosteira可能这不是问题所在,我试过了,但没有luckI使用类似于你的答案的东西<代码>ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager();adapter.AddFragment(new FragmentHistory(),“”);adapter.AddFragment(new FragmentBookmark(),“”);adapter.AddFragment(new FragmentSettings(),“”);mViewPager.setAdapter(adapter);mTabLayout.setupWithViewPager(mViewPager);
。为什么你说永远不要这样做,这是实现这一点的另一种方法?@Spritzig如果你这样做,你会有这个问题:(但你也可以在中找到同样的问题)@EpicPandaForce对于第一部分,感谢您提供的信息提示,我会改进它!但是将宽度和高度更改为
match\u parent
并没有解决问题,就像视图可见性设置为
Gone
除了选项卡标题之外没有其他内容!!另外,您是否记住所有这些都在另一个p中片段不是吗?应该已经通过使用
getChildFragmentManager()解决它是否位于另一个父片段中的问题。这听起来像布局大小问题。但是它不是我所想的那样,所以你可能想考虑使用<代码>布局检查器< /C>工具来查看你的视图中的哪些代码有“<代码> 0,0< /代码>大小,而不是你实际期望的东西。@ EpICBANDAUTHORY谢谢你的提示,但目前已经有一年多了。使用
java
,我为
viewpager
添加了
fragment
,比如comm