Tabs Not Showing in tabview

458 Views Asked by At

I have created a tabview inside a navigation drawer activity. I used a simple fragment page adapter to manage it. the problem is that my tabs are not visible. Since the activity_main.xml contains the navigation drawer, I used the content_main.xml to create the tab view and the viewPager.

Code for the adapter is,

public class SimpleFragmentPageAdapter extends FragmentPagerAdapter {
private String[] tabtitle=new String[]{"Tab1", "Tab2", "Tab3"};
Context context;
private int pagecount=3;
public SimpleFragmentPageAdapter(FragmentManager fm, Context context) {
    super(fm);
    this.context=context;
}

@Override
public CharSequence getPageTitle(int position) {
    return tabtitle[position];
}

@Override
public Fragment getItem(int position) {
    switch (position) {

        // Open FragmentTab1.java
        case 0:
            FragmentSc fragmentSc = new FragmentSc();
            return fragmentSc;

        // Open FragmentTab2.java
        case 1:
            Entertainment entertainment = new Entertainment();
            return entertainment;

        // Open FragmentTab3.java
        case 2:
            Sports sports = new Sports();
            return sports;
    }
    return null;
}

@Override
public int getCount() {
    return 0;
}


}

Code for the MainActivity.

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {


TabLayout tabLayout;

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final ViewPager viewPager= (ViewPager) findViewById(R.id.view_pager);
    viewPager.setAdapter(new SimpleFragmentPageAdapter(getSupportFragmentManager(),this));

    tabLayout= (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);


    getSupportActionBar().setTitle(null);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);



    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void dispaySelectedScreen(int id) {
    Fragment fragment = null;

    switch (id) {
        case R.id.facebook_login:
            fragment = new FacebookLogin();
            break;
        case R.id.memes:
            fragment = new Memes();
            break;
        case R.id.submit_image:
            fragment = new SubmitImage();
            break;
        case R.id.discussion:
            fragment = new Discussions();
            break;
        case R.id.invite:
            fragment = new Invite();
            break;
        case R.id.connect_fb:
            fragment = new FacebookConnect();
            break;
        case R.id.connect_twitter:
            fragment = new TwitterConnect();
            break;
        case R.id.connect_instagram:
            fragment = new InstaConnect();
            break;
    }

    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_main, fragment);
        ft.addToBackStack(null);
        ft.commit();




    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);









}



@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();


    dispaySelectedScreen(id);
    return true;
}

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
public Action getIndexApiAction() {
    Thing object = new Thing.Builder()
            .setName("Main Page") // TODO: Define a title for the content shown.
            // TODO: Make sure this auto-generated URL is correct.
            .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
            .build();
    return new Action.Builder(Action.TYPE_VIEW)
            .setObject(object)
            .setActionStatus(Action.STATUS_TYPE_COMPLETED)
            .build();
}

@Override
public void onStart() {
    super.onStart();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client.connect();
    AppIndex.AppIndexApi.start(client, getIndexApiAction());
}

@Override
public void onStop() {
    super.onStop();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    AppIndex.AppIndexApi.end(client, getIndexApiAction());
    client.disconnect();
}


}

Please suggest what am I missing.

1

There are 1 best solutions below

0
Shakti Patra On

Got it... I missed the getcount override method..

i need to pass return pagecount...

Working now