gpt4 book ai didi

android - fragment 在 tabLayout 中多次出现

转载 作者:行者123 更新时间:2023-11-29 18:25:54 25 4
gpt4 key购买 nike

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

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

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

fragment 是一个衬里,所以我认为那里没有任何问题。请在下面找到 2 个截取的屏幕截图 1) onLoading Activity 2) 第一次单击 tab2 后。

enter image description here enter image description here

最佳答案

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

关于android - fragment 在 tabLayout 中多次出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59080752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com