gpt4 book ai didi

android - fragment 中的 Tablayout 未使用 View 寻呼机显示选项卡

转载 作者:行者123 更新时间:2023-11-30 01:15:51 25 4
gpt4 key购买 nike

我正在尝试使用 View 寻呼机和 View 寻呼机适配器在 fragment 中添加 2 个选项卡,但它们没有显示。

[![this is how it looks, no tabs are showing][1]][1]

这是fragment_home.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="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.code.erum.masterappeverycatalogdemo.activity.LatestBrandsFragment">


<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"/>
</RelativeLayout>

这是我的 ViewPagerAdapter 类,它在 fragment 列表中添加 fragment

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

这是我的 fragment

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);

viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
setupViewPager(viewPager);

tabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
// Inflate the layout for this fragment
return rootView;
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}

@Override
public void onDetach() {
super.onDetach();
}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
adapter.addFragment(new LastestCatalogsFragment(), "ONE");
adapter.addFragment(new LatestBrandsFragment(), "TWO");
viewPager.setAdapter(adapter);
}

这是gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
applicationId "com.code.erum.masterappeverycatalogdemo"
minSdkVersion 17
targetSdkVersion 22
versionCode 1
versionName "1.0"
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:recyclerview-v7:22.0.0'
compile 'com.android.support:design:22+'
}

最佳答案

改变这个:

private void setupViewPager(ViewPager viewPager) {

FragmentManager FManager = getChildFragmentManager();
ViewPagerAdapter adapter = new ViewPagerAdapter(FManager);

// ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
// ...
}

关于android - fragment 中的 Tablayout 未使用 View 寻呼机显示选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37875195/

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