Android如何从TabLayout隐藏选项卡

时间:2015-10-15 10:07:41

标签: android android-tablayout

我需要隐藏第一个标签。第一页应该可以工作,但是当用户选择它时,它应该看起来像是在选项卡上。我怎么能这样做?

我找到了TabHost的一些解决方案,对我来说没用。

public class TabFragmentClients extends Fragment {

public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 5 ;
FinanceClients FinanceClients;

public ClientsFragment clientsFragment;
public FinanceFragment financeFragment;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /**
     *Inflate tab_layout and setup Views.
     */
    final View x =  inflater.inflate(R.layout.tab_layout_clients,null);
    tabLayout = (TabLayout) x.findViewById(R.id.tabs);
    viewPager = (ViewPager) x.findViewById(R.id.viewpager);

    /**
     *Set an Apater for the View Pager
     */
    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

    /**
     * Now , this is a workaround ,
     * The setupWithViewPager dose't works without the runnable .
     * Maybe a Support Library Bug .
     */
    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });
    return x;

}

2 个答案:

答案 0 :(得分:1)

tabLayout = (TabLayout) findViewById(R.id.tabs);

((ViewGroup) tabLayout.getChildAt(0)).getChildAt(desiredPosition).setVisibility(View.GONE);//hides the tab

答案 1 :(得分:0)

你试过这个吗?

 tabLayout.setupWithViewPager(viewPager);
 tabLayout.removeTabAt(0);