TabHost 的替代品 [英] Alternative of TabHost

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

问题描述

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

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.

推荐答案

我问的这个问题很糟糕.有更好的方法来做到这一点.我在想怎么做.然后,我使用了 TabLayoutFrameLayout.我认为 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) {

        }
    });

因此,当我点击另一个 标签 时,它隐藏了其他布局而没有主要布局.所以,它对我有用.我别无选择.我会说这是我的答案.

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天全站免登陆