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

问题描述 投票:0回答:2

我在TabLayout内使用Fragment。显示TabLayout(由2 fragments组成)的内容,但不显示tabs的标题(请参阅gif)。我相信这是一个布局问题,但我不确定如何解决。还有ViewPager2ViewPager)中使用的getPagetitle的替代方法是什么?

主要片段

    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>
android android-tablayout android-viewpager2
2个回答
0
投票

您的tabLayout不显示。使用这个:

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

0
投票

按照documentation

TabLayoutMediator对象还处理生成页面的任务 TabLayout对象的标题,这意味着适配器类 不需要覆盖getPageTitle():

并且要这样做:

    TabLayout tabLayout = view.findViewById(R.id.tab_layout);
    new TabLayoutMediator(tabLayout, viewPager,
            (tab, position) -> tab.setText("OBJECT " + (position + 1))
    ).attach();
© www.soinside.com 2019 - 2024. All rights reserved.