提问者:小点点

如何在tablayout中覆盖和显示viewpager2中选项卡的标题


Iam在片段内使用表布局。 显示表布局(由2个片段组成)的内容,但不显示选项卡的标题(请参阅gif)。 我相信这是一个布局问题,但我不确定如何解决它。 另外,在ViewPager(GetPageTitle)中使用的ViewPager2重写的方法是什么?

主片段

public class CommunityFragment extends Fragment {

    private TabLayout tbL;
    private ViewPager2 vp;
    private RecipeViewPagerAdapter rvpa;

    public CommunityFragment() {
        // Required empty public constructor
    }

    public static CommunityFragment newInstance(){
        return new CommunityFragment();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_community, container, false);
        tbL = rootView.findViewById(R.id.tabL);
        vp = rootView.findViewById(R.id.pager);
        rvpa = new RecipeViewPagerAdapter(this);
        vp.setAdapter(rvpa);

        rvpa.addFragment(new AllFragment(),"All time");
        rvpa.addFragment(new DayFragment(),"Today");
        new TabLayoutMediator(tbL, vp, new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        //////title????
                    }
                }).attach();
        return rootView;
    }
}

适配器

public class RecipeViewPagerAdapter extends FragmentStateAdapter {

    private ArrayList<Fragment> listF = new ArrayList<>();
    private ArrayList<String> listT =  new ArrayList<>();

    public RecipeViewPagerAdapter(@NonNull Fragment fragment) {
        super(fragment);
    }


    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return listF.get(position);
    }
    //new method ????
    public CharSequence getPageTitle(int position){
        return listT.get(position);
    }

    @Override
    public int getItemCount() {
        return listT.size();
    }

    public void addFragment(Fragment frag, String title){
        listF.add(frag);
        listT.add(title);
    }
}

片段布局

<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".fragments.CommunityFragment">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/yellowCool"/>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

共2个答案

匿名用户

不显示表布局。 使用此选项:

<androidx.viewpager2.widget.ViewPager2
     android:id="@+id/pager"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_weight="1" // add this line
/>

TablayoutMediator:

new TabLayoutMediator(tbL, vp, new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        if(position == 0)
                           tab.setText("All time");
                        else
                           tab.setText("Today");
                    }
                }).attach();

匿名用户

根据文档:

TabLayoutMediator对象还处理为TabLayout对象生成页面标题的任务,这意味着adapter类不需要重写getPageTitle():

要做到这一点:

    TabLayout tabLayout = view.findViewById(R.id.tab_layout);
    new TabLayoutMediator(tabLayout, viewPager,
            (tab, position) -> tab.setText("OBJECT " + (position + 1))
    ).attach();