【问题标题】:Android - ViewPager is not showing the fragmentsAndroid - ViewPager 不显示片段
【发布时间】:2019-05-23 13:04:59
【问题描述】:

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

  • 我尝试查看 StackoverFlow 上存在相同问题的其他线程,但其中大多数都面临 NPE(Null 指针异常)错误的问题,这不是我的情况。

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

  • 请记住,我正在使用Nested fragments(片段中的片段)

  • 我正在使用AndroidHive 的文档,并且我之前使用过它,但是没有嵌套的简单片段,它可以完美运行

  • 我尝试使用 Layout Inspector 工具。

问:我缺少什么,我应该怎么做才能让它运行起来?!

到目前为止,这是我的代码:

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>

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);
    }
}

【问题讨论】:

    标签: java android android-fragments android-viewpager android-tablayout


    【解决方案1】:
    adapter.addFragment(new CalendarTab(), "Calendar");
    adapter.addFragment(new FullListTab(), "List");
    
    ...
    
    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
    

    不要这样做,永远。它可能导致非常神秘的错误。您必须从 getItem() 内部返回新的 Fragment 实例,而不是从其他地方返回。



    至于你的实际问题,

    XML: MainFragment
    
    <LinearLayout
       android:orientation="vertical"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
    

    将其更改为match_parentmatch_parent

    【讨论】:

    • 我正在使用类似于您的答案的内容。 ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(); adapter.AddFragment(new FragmentHistory(), ""); adapter.AddFragment(new FragmentBookmark(), ""); adapter.AddFragment(new FragmentSettings(), ""); mViewPager.setAdapter(adapter); mTabLayout.setupWithViewPager(mViewPager);。为什么你说永远不要这样做,这是实现这一目标的另一种方式?
    • @Spritzig 如果你这样做,你会遇到这个问题:stackoverflow.com/questions/55753099/…(但你也可以在stackoverflow.com/q/53978606/2413303找到同样的问题)
    • @EpicPandaForce 对于第一部分,感谢您提供的信息提示,我会努力改进它!但是将宽度和高度更改为match_parent 并没有解决它,就像视图可见性设置为Gone 除了标签标题之外什么都没有!另外,您是否记住所有这些都在另一个父片段中?
    • 它在另一个父片段中应该已经使用getChildFragmentManager()解决了。这听起来像布局大小问题。然而这不是我想象的那样,所以您可能需要考虑使用Layout Inspector 工具来查看您的哪些视图具有0,0 大小而不是您实际期望的大小。
    • @EpicPandaForce 感谢您的提示,但目前我使用java 已经一年多了,我已经为viewpager 添加了fragment,就像上面的评论一样,我没有有任何崩溃或其他事情,但我会在那里检查您的答案并再次获得最佳方法,谢谢。如果你有时间,请检查我的最后一个问题,我需要一个答案。
    【解决方案2】:

    我过去也遇到过类似的问题。我通过将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>
    

    【讨论】:

    • 什么?不,你没有
    • 几周前我遇到了这个问题,并通过嵌套 TabLayout 解决了它。除非它是另一个通过这样做“修复”的错误:P 无论如何,我都遵循了文档:developer.android.com/reference/android/support/design/widget/…
    • 嗯,as part of a ViewPager's decor.. 我想知道这是什么意思。但是,是的,显然您可以将它放在 ViewPager 中。不过,我认为你不必这样做。即使不是,它也可以工作。
    • 是的,我一开始并没有嵌套它,因为大多数在线示例也没有嵌套。我也不理解“装饰”部分,但我尝试嵌套它并解决了我的问题。我的问题类似于 OP 的问题,因此“你必须嵌套”。我想我会编辑答案更清楚。
    • @RicardoCosteira 可能不是问题,我试过了,但没有运气
    【解决方案3】:

    我设法通过使用名为SPager 的第三方库解决了这个问题,这里是Github link 的完整文档。 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" />
    

    在我的 JAVA 课程中

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

    我还应该提到SPagerKotlin

    兼容

    我决定发布,它可能对其他人有用,对于我来说,这是一个临时修复,直到有人回答这个帖子!

    【讨论】:

    • 这足以解决我的问题!我刚刚提交了+50打赏!快乐编码:)
    猜你喜欢
    • 2015-10-16
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    相关资源
    最近更新 更多