TabHost的替代 [英] Alternative of TabHost

查看:49
本文介绍了TabHost的替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里您可以看到我使用了 TabLayout TabHost .我在前两次图片中使用了 TabLayout .在第三张图片中,我使用了 TabHost .通常, TabLayout 看起来比 TabHost / TabWidget 更好. TabLayout 是'不推荐使用.这是一个问题,我问有人说这是重复的.我以为是.看,当我使用 ViewPager TabLayout 时,我不能直接在 MainActivity 中使用这些 layout 必须在这些 framgents 类中编写源代码才能在此处工作.但是,当我使用 TabHost TabWidget TabContent 时,我可以直接在 MainActivity中使用这些 layouts 我不必在其他地方编写源代码.就像我在 ViewPager Fragments 中使用的一样.因此,这是 TabLayout TabHost 之间的问题.而且, TabHost TabWidget 不可滚动,这意味着我无法像在 TabLayout 中那样滚动 TabWidget .还有另一个问题出现.因此, TabLayout TabHost 是不同的.因此,如果我不使用 Tabhost TabLayout 替代方法会更好.

Here as you can see I used TabLayout,TabHost. I used TabLayout in first twice pictures. In third picture I used TabHost. Usually, TabLayout is looking better than TabHost/TabWidget.TabHost as you can see TabHost is deprecated. So, It would be better if I don't use TabHost. TabLayout isn't deprecated. Here is a question I asked someone said it is duplicate. I thought it was. Look, while I am using ViewPager and TabLayout I can't use those layout directly in MainActivity I meant I have to write source code inside those framgents class to work right there. But, while I am using TabHost,TabWidget and TabContent I can use those layouts directly in MainActivity I don't have to write source code somewhere else. Like I used in ViewPager or Fragments. So, it is the problem between TabLayout and TabHost. And, TabHost or TabWidget isn't scrollable which means I can't scroll TabWidget like I did in TabLayout. There's another problem arise. So, TabLayout and TabHost isn't same. So, it would be better if I don't use TabLayout alternative of Tabhost.

在我的项目中,我需要像 TabLayout 这样的可滚动选项卡,而且我必须从我的 MainActivity 中访问这些布局.因此,我不能使用 TabLayout .虽然我尝试使用它.但是,有什么方法可以只在 ViewPager 内部使用布局,并从 MainActivity 访问这些布局.

In my project I need scrollable tabs like TabLayout and, I have to access those layouts from my MainActivity. So, I can't use TabLayout. Although I tried to use it. But, is there any possible way to use only layouts inside ViewPager and access those layouts from MainActivity.

推荐答案

这是我提出的可怕问题.有更好的方法来做到这一点.我在想怎么做.然后,我使用了 TabLayout FrameLayout .我认为 FrameLayout 将隐藏其他线性布局,而无需先进行 layout .然后,我已经添加到我的 activity_main.xml

It was terrible question I had asked. There's better way to do it. I was thinking how to do that. Then, I used TabLayout and FrameLayout. I thought FrameLayout will hide others linearlayout without first layout. Then, I had added to my 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" 上进行设置.

But, FrameLayout didn't work as I thought. So, I discouraged again. But, I keep thinking of it. I didn't remove anything. Then, something else came to my head. What if I hide others linearLayout without first ones. How I thought Framelayout should work. Then, I set on 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 时,它隐藏了其他没有主要布局的布局.因此,它为我工作.我别无选择.我会说这是我的答案.

So, while I am clicking on another tabs it is hiding others layout without main ones. So, it worked for me. There's no alternative I have get. I will say that it is my answer.

这篇关于TabHost的替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆