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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…