Click here to Skip to main content
15,171,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have 3 tabs in my TabLayout, my question is, for example, i want to delete or disable one or more particular Tab from TabLayout and ViewPager, is it possible to achieve, how can i do this?

<pre lang="C#">public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{<br />
<br />
TabLayout tabLayout;<br />
public static ViewPager viewPager;<br />
DBController db;<br />
<br />
@Override<br />
protected void onCreate(Bundle savedInstanceState) {<br />
    super.onCreate(savedInstanceState);<br />
    setContentView(R.layout.activity_main);<br />
<br />
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);<br />
    setSupportActionBar(toolbar);<br />
<br />
    tabLayout = (TabLayout) findViewById(R.id.tabLayout);<br />
<br />
    tabLayout.removeViewAt(3);<br />
<br />
    viewPager = (ViewPager) findViewById(R.id.pager);<br />
<br />
<br />
    Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());<br />
<br />
    viewPager.setAdapter(adapter);<br />
<br />
    tabLayout.setOnTabSelectedListener(this);<br />
<br />
    viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));<br />
    tabLayout.setupWithViewPager(viewPager);<br />
<br />
<br />
}<br />
<br />
@Override<br />
public void onTabSelected(TabLayout.Tab tab) {<br />
    viewPager.setCurrentItem(tab.getPosition());<br />
}<br />
<br />
@Override<br />
public void onTabUnselected(TabLayout.Tab tab) {<br />
<br />
}<br />
<br />
@Override<br />
public void onTabReselected(TabLayout.Tab tab) {<br />
<br />
}<br />
@Override<br />
public boolean onCreateOptionsMenu(Menu menu) {<br />
    // Inflate the menu; this adds items to the action bar if it is present.<br />
    getMenuInflater().inflate(R.menu.main, menu);<br />
    return true;<br />
<br />
}<br />
@Override<br />
public boolean onOptionsItemSelected(MenuItem item) {<br />
<br />
    int id = item.getItemId();<br />
<br />
    if (id == R.id.cartlist) {<br />
        Intent intent = new Intent(this,cart_list.class);<br />
        startActivity(intent);<br />
        return true;<br />
    }<br />
        return true;<br />
}<br />
public class SectionPagerAdapter extends FragmentPagerAdapter {<br />
<br />
    public SectionPagerAdapter(FragmentManager fm) {<br />
        super(fm);<br />
<br />
    }<br />
<br />
    @Override<br />
    public Fragment getItem(int position) {<br />
        switch (position) {<br />
                case 0:<br />
                    return new Tab1();<br />
                case 1:<br />
                     return new Tab2();<br />
                case 2:<br />
                    return new Tab4();<br />
            }<br />
        return  null;<br />
    }<br />
<br />
    @Override<br />
    public int getCount() {<br />
        return 3;<br />
    }<br />
<br />
    @Override<br />
    public CharSequence getPageTitle(int position) {<br />
        switch (position) {<br />
            case 0:<br />
                return "ALL";<br />
            case 1:<br />
                return "WESTERN";<br />
            case 2:<br />
                return "-";<br />
        }<br />
        return null;<br />
    }<br />
}</pre><br />


What I have tried:

is there any syntax to acheive or solve this problem?
Posted
Updated 28-Dec-16 1:01am

1 solution

   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900