【问题标题】:TabLayout with ViewPager on new tab selection previous tab text disappersTabLayout with ViewPager on new tab selection previous tab text消失
【发布时间】:2021-07-09 12:20:01
【问题描述】:

我将 TabLayout 与 ViewPager 一起使用以按要求显示数据。 在选项卡布局中,我的要求是,我必须为所选项目设置不同的字体,为其他项目设置不同的字体。我为此编写了以下代码。

但我面临一个问题。假设我有 4 个项目“一”、“二”、“三”和“四”。最初“一个”默认选择粗体字体,其他字体默认选择普通字体。但是当我单击“两个”选项卡时,“一个”选项卡消失了。当我点击“三”选项卡时,“二”选项卡消失,“一”选项卡以正常字体显示。

private void setupViewPager(CustomViewPager viewPager, TabLayout tabLayout, ArrayList<TipsInfo> mListTips) {
        adapter = new ViewPagerAdapter(getChildFragmentManager());

        for (int i = 0; i < mListTips.size(); i++) {
            if (!TextUtils.isEmpty(mListTips.get(i).getValue()))
                adapter.addFragment(TipsDetailFragment.newInstance(mListTips.get(i).getValue()), mListTips.get(i).getLabel());
        }
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

        View tabContent = LayoutInflater.from(requireContext()).inflate(R.layout.item_tips_tablayout_selected, null);
        TextView selected = tabContent.findViewById(R.id.tv_tips_selected_tab);
        View tabContent1 = LayoutInflater.from(requireContext()).inflate(R.layout.item_tips_tablayout_unselected, null);
        TextView unselected = tabContent1.findViewById(R.id.tv_tips_unselected_tab);

        tabLayout.addOnTabSelectedListener(
                new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        selected.setText(tab.getText());
                        tab.setCustomView(selected);
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                        unselected.setText(tab.getText());
                        tab.setCustomView(unselected);
//
//                        for(int i=0; i<tabLayout.getTabCount(); i++){
//                            if(i != tabLayout.getSelectedTabPosition()){
//                                unselected.setText(tabLayout.getTabAt(i).getText());
//                                tabLayout.getTabAt(i).setCustomView(unselected);
//                            }
//                        }
                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {
                        onTabSelected(tab);
                    }
                }
        );

    }

我的ViewPaerAdapterFragmentStatePagerAdapter 扩展,它的对象用getChildFragmentManager 创建。我的 XML 代码如下所示:

<com.google.android.material.tabs.TabLayout
            android:id="@+id/tl_tips"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorWhite"
            android:elevation="@dimen/v1dp"
            android:visibility="invisible"
            app:tabGravity="fill"
            android:gravity="start"
            app:tabMinWidth="@dimen/v100dp"
            android:textAlignment="viewStart"
            app:tabIndicatorFullWidth="false"
            app:tabIndicatorColor="@color/colorBannerNewDotSelected"
            app:tabMode="scrollable"
            android:layout_gravity="start"
            app:tabIndicatorHeight="@dimen/v3dp"
            app:tabSelectedTextColor="@color/colorBlackDark"
            app:tabTextAppearance="@style/MyTabLayoutTextAppearance"
            app:tabTextColor="@color/colorServiceCount"
            />

item_tips_tablayout_selecteditem_tips_tablayout_unselected 仅包含应用了不同字体的 TextView。

截图:

  1. 首字母

  1. 当点击“Benefits”时,应用到它的字体但“How to use”消失了。

  1. 当点击“产品使用”时,应用的字体,“使用方法”以普通字体出现,“优点”选项卡消失。

【问题讨论】:

    标签: android android-viewpager android-tablayout


    【解决方案1】:

    它基本上完成了您在代码中编写的操作。

    tabLayout.addOnTabSelectedListener(
                        new TabLayout.OnTabSelectedListener() {
                            @Override
                            //as tab 2 selected it set text to tab 2
                            public void onTabSelected(TabLayout.Tab tab) {
                                selected.setText(tab.getText());
                                tab.setCustomView(selected);
                            }
        
                            @Override
                            public void onTabUnselected(TabLayout.Tab tab) {
         //as tab 1 unselected it set NO text to tab 1 in it 
                                unselected.setText(tab.getText());
                                tab.setCustomView(unselected);
    

    为什么每次只需要换字体的时候都要设置CustomView呢?

    我有一个转换成 JAVA 代码的 Kotlin 代码,它在 JAVA 中看起来很糟糕,但它可以工作:

     private void setTypefaceToTab(Context context, TabLayout tabLayout, TabLayout.Tab tab, int stylem) {
                if (tab != null && context != null) {
                    try {
                         View var10000 = ((ViewGroup)tabLayout.getChildAt(0)).getChildAt(tab.getPosition());
                        LinearLayout layout = (LinearLayout)var10000;
                        var10000 = layout.getChildAt(1);
                        TextView tabTextView = (TextView)var10000;
                        tabTextView.setTextSize (***Text Size***);
                        tabTextView.setTypeface(Typeface.create("sans-serif-condensed",stylem));
                    } catch (Exception var7) {
                        Log.d ("EXC",var7.toString ());
                    }
                }
            }
    

    然后只需在 tabselectedlistener 和 onCreate 中使用它来为第一个选定的选项卡设置字体:

    protected void onCreate(Bundle savedInstanceState) {
    ...
     setTypefaceToTab(context,tablayout, tablayout.getTabAt (tablayout.getSelectedTabPosition ()),Typeface.BOLD_ITALIC);
    ...
    }
    

    在tabListener中:

      private void tabListener(){
                tablayout.addOnTabSelectedListener (new TabLayout.OnTabSelectedListener () {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                                setTypefaceToTab(context,tablayout,tab,Typeface.BOLD_ITALIC);
                    }
        
                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
                        setTypefaceToTab(context,tablayout,tab,Typeface.NORMAL);
                    }
        
                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {
        
                    }
                });
            }
    

    希望这是你想要的

    【讨论】:

    • 非常感谢......它正在工作......我花了超过 6 个小时。
    猜你喜欢
    • 2014-11-16
    • 1970-01-01
    • 2011-10-02
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多