如何将每个标签链接到导航抽屉?

时间:2015-09-12 13:36:28

标签: javascript android android-fragments navigation-drawer pagerslidingtabstrip

由于我是android的新手,我遇到了一个小问题,实际上我不知道该怎么做。实际上我正在寻找一个链接到每个标签的导航抽屉菜单。例如:将标签滑动为' tab1',' tab2'和' tab3'和导航抽屉菜单也列为' tab1',' tab2'和; tab3'。所以,当我点击标签2'从导航抽屉,它应该带我到' tab2'滑动标签。请有人帮帮我。 感谢



   
//MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext=this;


        list=LoadingContentActivity.list;

        Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ActionBar actionar=getSupportActionBar();
        actionar.setDisplayHomeAsUpEnabled(true);
        actionar.setHomeAsUpIndicator(R.drawable.ic_menu);

        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null) {
            setupDrawerContent(navigationView);
        }

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        if (viewPager != null) {
            setupViewPager(viewPager);
        }

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


    }

    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        if (menuItem.getItemId() == R.id.tab_11) {
                            startActivity(new Intent(MainActivity.this, FragmentTab1.class));
                        } else if (menuItem.getItemId() == R.id.tab_22) {
                            startActivity(new Intent(MainActivity.this, FragmentTab2.class));
                        } else if (menuItem.getItemId() == R.id.tab_33) {
                            startActivity(new Intent(MainActivity.this, FragmentTab3.class));
                        }
                        return true;
                    }
                });
    }

    private void setupViewPager(ViewPager viewPager) {
        Adapter adapter = new Adapter(getSupportFragmentManager());
        adapter.addFragment(new FragmentTab1(), "CALLS");
        adapter.addFragment(new FragmentTab2(), "CHATS");
        adapter.addFragment(new FragmentTab3(), "CONTACTS");
        viewPager.setAdapter(adapter);
    }

//TabsPagerAdapter.java

public class TabsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int index) {

        switch (index) {
            case 0:
                return new FragmentTab1();
            case 1:
                return new FragmentTab2();
            case 2:
                return new FragmentTab3();
        }

        return null;
    }

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

}




1 个答案:

答案 0 :(得分:1)

您不必再次启动相同的活动,因为这会导致它被推入Backstack。我建议您只需更改ViewPager中的当前片段

{
  "filter" : {
    "exists" : { "field" : "quid_3" }
  },
  "_source": [ "qid_1"]
}