gpt4 book ai didi

java - TabLayout 不会填充

转载 作者:行者123 更新时间:2023-12-02 04:26:45 26 4
gpt4 key购买 nike

我从一些设计支持库资源开始,特别是 TabLayout。

这是一个简单的 fragment ,但我无法填充我的 TabLayout, fragment 正常滑动,但我的 TabLayout 没有填充标题,正如我们在附图中看到的那样。我不确定是否应该将 ViewPager 与 TabLayout 一起包装在 fragment 中,这对我来说很有意义。

关注,谢谢。

http://postimg.org/image/x1z6zvlqd/

主要 fragment Xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.TabLayout
android:id="@+id/tab_sliding_tabs_about"

android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
android:background="@color/yellow"
/>

<android.support.v4.view.ViewPager
android:id="@+id/vp_about_fragment_content"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />

</LinearLayout>

主要 fragment java

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_about, container, false);
ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.vp_about_fragment_content);
mAdapter = new AboutPageAdapter(getFragmentManager(),
getContext());
viewPager.setAdapter(mAdapter);


TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tab_sliding_tabs_about);
tabLayout.setupWithViewPager(viewPager);
mAdapter.notifyDataSetChanged();
return rootView;
}

页面适配器java

public class AboutPageAdapter extends FragmentPagerAdapter {

final int PAGE_COUNT = 2;
private String tabTitles[] = new String[]{"Tab A", "Tab B"};
private Context context;

public AboutPageAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}

@Override
public int getCount() {
return PAGE_COUNT;
}

@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FragA.newInstance(position);
default:
return FragB.newInstance(position);
}

}

@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}

最佳答案

感谢您的所有提示,我能够使其工作:

    mViewPager.setAdapter(adapter);
mTabLayout.post(new Runnable() {
@Override
public void run() {
mTabLayout.setupWithViewPager(mViewPager);
}
});

关于java - TabLayout 不会填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32058633/

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