问题
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
.
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
.
回答1:
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>
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.
来源:https://stackoverflow.com/questions/66005820/alternative-of-tabhost