【问题标题】:I want to make scrollable TabWidget [duplicate]我想制作可滚动的 TabWidget [重复]
【发布时间】:2021-02-02 06:28:42
【问题描述】:

我正在向TabHost 内容添加大量布局。

<TabHost
            android:id="@+id/tab_host"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <LinearLayout
                        android:id="@+id/tab4"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <include
                            layout="@layout/fragment_mask"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/tab5"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <include
                            layout="@layout/fragment_mask"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/tab6"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <include
                            layout="@layout/fragment_mask"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/tab7"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <include
                            layout="@layout/fragment_mask"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/tab8"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <include
                            layout="@layout/fragment_mask"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/tab1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <include
                            layout="@layout/fragment_glass"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/tab2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <include
                            layout="@layout/fragment_hair"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/tab3"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <include
                            layout="@layout/fragment_mask"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />
                    </LinearLayout>
                </FrameLayout>

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>
        </TabHost>

但是,并不是每个Tabs 都可以正常显示。所以,我想让它可以滚动。我可以通过TabLayout 做到这一点。但是,我想从MainActivity 访问那些layouts。所以,我不能使用TabLayoutViewPager。所以,我必须让TabWidget 可滚动。怎么做?或者,有没有类似的图书馆?

【问题讨论】:

    标签: java android android-tabhost android-framelayout tabwidget


    【解决方案1】:

    您可以使用 tablayout 和 viewpager 来做到这一点

    Layout.xml 文件

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/_32sdp"
            android:layout_margin="@dimen/_8sdp"
            android:layout_marginLeft="8sp"
            android:background="@drawable/tab_layout_background"
            app:tabBackground="@drawable/tab_layout_selector"
            app:tabIndicatorHeight="0dp"
            app:tabMode="auto"
            app:tabPaddingEnd="16dp"
            app:tabPaddingStart="16dp"
            app:tabRippleColor="@null"
            app:tabSelectedTextColor="@color/white"
            app:tabTextAppearance="@style/TabTextAppearance" />
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/tabLayout"
            android:layout_marginLeft="@dimen/_8sdp"
            android:layout_marginRight="@dimen/_8sdp"/>
    </RelativeLayout>
    

    主类文件

    tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.sales_order)));
        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.buyer_info)));
    
        final HomeAdapter adapter = new HomeAdapter(this, getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
    
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }
    
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
    
            }
    
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
    
            }
        });
    

    还有适配器

    public class HomeAdapter extends FragmentPagerAdapter {
    
    private int totalTabs;
    
    
    public HomeAdapter(MainActivity context, FragmentManager fm, int totalTabs) {
        super( fm );
        this.totalTabs = totalTabs;
    }
    
    @NonNull
    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new SalesListFragment();
            case 1:
                return new BuyerListFragment();
            default:
                return null;
        }
    }
    
    // this counts total number of tabs
    @Override
    public int getCount() {
        return totalTabs;
    }
    

    }

    希望一切顺利。

    【讨论】:

    • 我推荐一些你可能没有注意到的东西。我说我必须使用我的MainActivity 中的那些fragments 但是,当我使用ViewPager 时,我必须使用他们的fragment 类中的那些fragments。我无法使用来自MainActivity 的访问这些片段。这就是为什么我必须使用TabHostTabWidgetTabContent
    • TabHost 和 Tabwidget 在 API 级别 30 中均已弃用。在我的示例中,我使用 viewpager 使用了 2 个片段 SaleesListFragment 和 BuyerListFragment。我在适配器(HomeAdapter)中声明了它们。
    • 你不知道我想。我也问过一个问题。也请访问questionthis answer。正如他所说,TabHost 未被弃用,TabActivity 已被弃用。我也问了很多关于这个话题的问题。 1,2,3
    • 4
    • 请遵循 Android API 文档。它说 tabHost 在 API 30 developer.android.com/reference/android/widget/TabHost 中已弃用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多