我在我的应用程序中使用了最新设计支持库中的TabLayout.选项卡附加到viewpager,后者为每个选项卡加载片段.我想禁用所有选项卡,直到viewpager为用户选择的选项卡加载片段.我无法禁用tablayout或使其无法点击.我曾经使用过setEnabled(false)和setClickable(false),但是它没有用.我可以通过使用setVisiblity(View.GONE)使其不可见但我希望标签始终可见.
tabLayout = (TabLayout) findViewById(R.id.tabLayout); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); tabLayout.setTabMode(TabLayout.MODE_FIXED); tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.near_me_hover).setTag(1)); tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.all_hostels).setTag(2)); tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.top_five).setTag(3)); tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.advanced_search).setTag(4)); tabLayout.setEnabled(false); tabLayout.setClickable(false);
XML
android.support.design.widget.TabLayout
android:id=”@+id/tabLayout”
android:layout_width=”match_parent”
android:layout_height=”0dp”
android:layout_weight=”0.15″
android:scrollbars=”horizontal”
android:splitMotionEvents=”false” >
@Override public void onTabSelected(TabLayout.Tab tab) { switch (tab.getPosition()) { case 0: viewPager.setCurrentItem(tab.getPosition()); tab.setIcon(R.drawable.near_me_hover); break; case 1: viewPager.setCurrentItem(tab.getPosition()); tab.setIcon(R.drawable.all_hostels_hover); break; case 2: viewPager.setCurrentItem(tab.getPosition()); tab.setIcon(R.drawable.top_five_hover); break; case 3: viewPager.setCurrentItem(tab.getPosition()); tab.setIcon(R.drawable.advanced_search_hover); break; } } @Override public void onTabUnselected(TabLayout.Tab tab) { switch (tab.getPosition()) { case 0: tab.setIcon(R.drawable.near_me); break; case 1: tab.setIcon(R.drawable.all_hostels); break; case 2: tab.setIcon(R.drawable.top_five); break; case 3: tab.setIcon(R.drawable.advanced_search); break; } } @Override public void onTabReselected(TabLayout.Tab tab) { } }); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels) { } @Override public void onPageSelected(int position) { tabLayout.getTabAt(position).select(); } @Override public void onPageScrollStateChanged(int state) { } });