I using the control android.support.design.widget.TabLayout
I have 4 tabs - on each tab i show some ViewPager - ( using fragment to show the different viewPager )
I want to disable all the tabs until the user will add some data that exist on the first tab.
I don't find any way to disable the tabs.
The code:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/detailsElementBackground"
android:clickable="true"
app:tabGravity="center"
app:tabMode="scrollable"
app:tabTextAppearance="@style/MineCustomTabText" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.AppBarLayout>
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Fragment1(), "Fragment1");
adapter.addFragment(new Fragment2(), "Fragment2");
adapter.addFragment(new Fragment3(), "Fragment3");
adapter.addFragment(new Fragment4(), "Fragment4");
// need to disable Fragment2 & Fragment3 & Fragment4 until the user will add some string that exist on Fragment1
Try this:
LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
Reference: Disable TabLayout
there's my solution. I use Kotlin language, Java is the same
private fun disableTab(tabLayout: TabLayout, index: Int) {
(tabLayout.getChildAt(0) as ViewGroup).getChildAt(index).isEnabled = false
}
It's worked for me!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With