Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable of select some tab when using TabLayout ?

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

like image 616
Yanshof Avatar asked Sep 02 '17 00:09

Yanshof


2 Answers

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

like image 165
akshay_shahane Avatar answered Sep 27 '22 17:09

akshay_shahane


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!

like image 33
Mạnh Hoàng Huynh Avatar answered Sep 27 '22 17:09

Mạnh Hoàng Huynh