带有标签Android_Java的NavigationDrawer

时间:2019-02-07 20:34:30

标签: java android android-viewpager android-tablayout android-navigation-drawer

我的应用程序中有一个导航抽屉和选项卡布局,每当我单击该导航抽屉中的每个菜单项时,它都会打开右侧选项卡并显示recyclerview列表,当我第一次打开ViewPager选项卡时,一切工作正常,但是导航到其他选项卡之一后,当我导航回它时,它什么也不显示,只显示一个空片段。 有人可以帮忙吗?!!

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
private ViewPager viewPager;
private Toolbar toolbar;
private TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar=findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    DrawerLayout drawerLayout=findViewById(R.id.drawer_layout_id);
    ActionBarDrawerToggle toggle_btn=new ActionBarDrawerToggle(MainActivity.this,drawerLayout,toolbar,R.string.open,R.string.close);
    drawerLayout.addDrawerListener(toggle_btn);
    toggle_btn.syncState();

    viewPager=findViewById(R.id.viewpager);
    tabLayout=findViewById(R.id.sliding_tabs);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setupWithViewPager(viewPager);

    NavigationView navigationView=findViewById(R.id.nav_view_id);
    assert navigationView!=null;
    navigationView.setNavigationItemSelectedListener(this);
    onNavigationItemSelected(navigationView.getMenu().getItem(0).setChecked(true));

    ViewPagerAdapter viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(viewPagerAdapter);

    tabLayout.getTabAt(0).setIcon(R.mipmap.ic_ho);
    tabLayout.getTabAt(1).setIcon(R.mipmap.ic_spo);
    tabLayout.getTabAt(2).setIcon(R.mipmap.ic_mu);
    tabLayout.getTabAt(3).setIcon(R.mipmap.ic_mov);
    tabLayout.getTabAt(4).setIcon(R.mipmap.ic_po);
    tabLayout.getTabAt(5).setIcon(R.mipmap.ic_tec);
    tabLayout.getTabAt(6).setIcon(R.mipmap.ic_bu);

}
@Override
public void onBackPressed() {
    DrawerLayout drawerLayout=findViewById(R.id.drawer_layout_id);
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.home) {
        viewPager.setCurrentItem(0);
        getSupportActionBar().setTitle(R.string.home);
    } else if (id == R.id.sport) {
        viewPager.setCurrentItem(1);
        getSupportActionBar().setTitle(R.string.sport);
    } else if (id == R.id.music) {
        viewPager.setCurrentItem(2);
        getSupportActionBar().setTitle(R.string.music);
    } else if (id == R.id.film) {
        viewPager.setCurrentItem(3);
        getSupportActionBar().setTitle(R.string.film);
    } else if (id == R.id.politics) {
        viewPager.setCurrentItem(4);
        getSupportActionBar().setTitle(R.string.politics);
    } else if (id == R.id.tech) {
        viewPager.setCurrentItem(5);
        getSupportActionBar().setTitle(R.string.tech);
    } else if (id == R.id.business) {
        viewPager.setCurrentItem(6);
        getSupportActionBar().setTitle(R.string.business);
    }
    DrawerLayout drawer = findViewById(R.id.drawer_layout_id);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

public class ViewPagerAdapter extends FragmentPagerAdapter {
private static final int HOME = 0;
private static final int SPORT = 1;
private static final int MUSIC = 2;
private static final int FILM = 3;
private static final int POLITICS = 4;
private static final int TECHNOLOGY = 5;
private static final int BUSINESS = 6;

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    switch (position){
        case HOME:
            return new HomeFragment();
        case SPORT:
            return new SportFragment();
        case MUSIC:
            return new MusicFragment();
        case FILM:
            return new FilmFragment();
        case POLITICS:
            return new PoliticsFragment();
        case TECHNOLOGY:
            return new TechnologyFragment();
        case BUSINESS:
            return new BusinessFragment();
        default:
            return null;
    }
}

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

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    return "";
    }
}

0 个答案:

没有答案