簡體   English   中英

TabHost 的替代品

[英]Alternative of TabHost

選項卡布局 選項卡布局 標簽主機

如您所見,我使用TabLayoutTabHost 我在前兩張圖片中使用TabLayout 在第三張圖片中,我使用TabHost 通常, TabLayout看起來比TabHost / TabWidget更好。 如您TabHost 棄用。 所以,如果我不使用TabHost會更好。 TabLayout沒有被棄用。 這是我問過的一個問題,有人說它是重復的。 我以為是。 看,當我使用ViewPagerTabLayout時,我不能直接在MainActivity中使用這些layout ,我的意思是我必須在這些framgents class 中編寫源代碼才能在那里工作。 但是,當我使用TabHostTabWidgetTabContent時,我可以直接在MainActivity中使用這些layouts ,而不必在其他地方編寫源代碼。 就像我在ViewPagerFragments中使用的一樣。 所以,這是TabLayoutTabHost之間的問題。 而且, TabHostTabWidget不可滾動,這意味着我無法像在TabLayout TabWidget 又出現了一個問題。 所以, TabLayoutTabHost是不一樣的。 因此,如果我不使用TabLayoutTabhost替代方案會更好。

在我的項目中,我需要像TabLayout這樣的可滾動選項卡,並且必須從MainActivity訪問這些布局。 所以,我不能使用TabLayout 雖然我嘗試使用它。 但是,有沒有辦法只使用ViewPager中的布局並從MainActivity訪問這些布局。

這是我問的可怕問題。 有更好的方法來做到這一點。 我在想怎么做。 然后,我使用TabLayoutFrameLayout 我認為FrameLayout會隱藏其他沒有第一個布局的線性layout 然后,我添加到我的activity_main.xml

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

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

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:text="TextView" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
</FrameLayout>


<com.google.android.material.tabs.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.tabs.TabItem
        android:id="@+id/tabitem1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Monday" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/tabitem2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tuesday" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/tabitem3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Wednesday" />
</com.google.android.material.tabs.TabLayout>

但是, FrameLayout並沒有像我想象的那樣工作。 於是,我又泄氣了。 但是,我一直在想。 我沒有刪除任何東西。 然后,我的腦海里浮現出別的東西。 如果我在沒有第一個的情況下隱藏其他linearLayout怎么辦。 我認為Framelayout應該如何工作。 然后,我設置了LinearLayout Visibility = "GONE"

LinearLayout l1 = findViewById(R.id.tab1);
    LinearLayout l2 = findViewById(R.id.tab2);
    LinearLayout l3 = findViewById(R.id.tab3);

    TabLayout tabLayout = findViewById(R.id.tab_layout);

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            Log.d("TAB_POSITION", String.valueOf(tab.getPosition()));
            if (tab.getPosition()==0){
                l1.setVisibility(View.VISIBLE);
                if (l2.getVisibility()==View.GONE){

                }else{
                    l2.setVisibility(View.GONE);
                }
                if (l3.getVisibility()==View.GONE){

                }else{
                    l3.setVisibility(View.GONE);
                }
            }else if (tab.getPosition()==1){
                l2.setVisibility(View.VISIBLE);
                if (l1.getVisibility()==View.GONE){

                }else{
                    l1.setVisibility(View.GONE);
                }
                if (l3.getVisibility()==View.GONE){

                }else{
                    l3.setVisibility(View.GONE);
                }
            }else if (tab.getPosition()==2){
                l3.setVisibility(View.VISIBLE);
                if (l2.getVisibility()==View.GONE){

                }else{
                    l2.setVisibility(View.GONE);
                }
                if (l1.getVisibility()==View.GONE){

                }else{
                    l1.setVisibility(View.GONE);
                }
            }
        }

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

        }

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

        }
    });

因此,當我單擊另一個tabs時,它隱藏了其他沒有主要布局的布局。 所以,它對我有用。 我沒有其他選擇。 我會說這是我的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM