【问题标题】:Fragment appearing multiple times in tabLayout片段在 tabLayout 中出现多次
【发布时间】:2019-11-28 01:59:38
【问题描述】:

我没有找到任何与此问题相关的帖子。我正在尝试使用this tutorial 滑动来实现简单的选项卡(选项卡布局)。但是,即使我第一次单击,我的片段也会出现多次。请在下面找到相关代码。将不胜感激提供的任何帮助。

FragmentPagerAdapter 实现:

public class SectionsPageAdapter extends FragmentPagerAdapter {

private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public SectionsPageAdapter(FragmentManager manager) {
    super(manager);

}

public void addFragment(Fragment fragment, String title) {
    mFragmentList.add(fragment);
    mFragmentTitleList.add(title);
}

@Override
public Fragment getItem(int position) {
    return mFragmentList.get(position);
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    return mFragmentTitleList.get(position);
}

@Override
public int getCount() {
    return mFragmentList.size();
}
}

主类:

SectionsPageAdapter mSectionsPageAdapter = new 
SectionsPageAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter and add fragments.
ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

mSectionsPageAdapter.addFragment(new TodayFragment(), "TAB1");
mSectionsPageAdapter.addFragment(new WeeklyFragment(), "TAB2");
mSectionsPageAdapter.addFragment(new PhotosFragment(), "TAB3");
mViewPager.setAdapter(mSectionsPageAdapter);
tabLayout.setupWithViewPager(mViewPager);

main.xml

<?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"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".DetailActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay">

            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            />

        </android.support.design.widget.CoordinatorLayout>

片段是一个衬里,所以我认为那里没有任何问题。请在下面找到 2 个屏幕截图 1) onLoading 活动 2) 第一次点击 tab2 后。

【问题讨论】:

  • 你没有分享按钮动作代码,我想你应该在点击按钮时刷新适配器。
  • @FaridHaq 我没有用按钮做任何事情(没有附加监听器)。都是关于点击标签的,没有额外的行为。
  • 另一个观察,我不知道为什么,但我在 FragmentPagerAdapter 中的 getCount 方法被多次调用(> 20),即使我只点击一次。
  • 你的主类代码在onCreate()里面吗?
  • @Ayush 是的,它在 onCreate() 里面

标签: android android-fragments android-tablayout


【解决方案1】:

您能与我们分享您的第二个片段的布局代码吗?我想看看是否缺乏坚实的背景可能会导致问题。

【讨论】:

  • 谢谢伙计。设置 android:background="@color/colorPrimary" 就可以了。我以为是遗传的。我非常感谢您在我的问题中发现我什至认为不足以提及的部分中的错误。
  • @DevendraSwami 这并不能解决您有重叠内容的事实。这只是隐藏它。
  • @MikeM。诚然,请随时提出更好的解决方案。我觉得重叠的内容可能是因为多次调用 getCount 并在每个计数上呈现视图。不过不确定!!!
  • @DevendraSwami 是的,没有足够的信息来确定实际问题。我只是让您知道您将来可能会遇到其他问题。
  • @MikeM。谢谢你让我知道。如果您希望我为问题添加更多信息,请告诉我。坦率地说,我在其他地方没有发现类似的问题,也不确定根本原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多