如何在点击时切换标签 [英] how to switch tab on click

查看:83
本文介绍了如何在点击时切换标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个选项卡,主选项卡上有导航抽屉,在其上单击特定项目后,我想使用意图转到Tab3,但它不起作用. 我的代码是:

I have three tab and on home tab I have navigation drawer,on which on clicking on a particular item i want to go to tab3 using intent but it doesn't work. My code is :

//Initializing the tablayout
tabLayout = (TabLayout) findViewById(R.id.tabLayout);

    //Adding the tabs using addTab() method
    tabLayout.addTab(tabLayout.newTab().setText("Home"));
    tabLayout.addTab(tabLayout.newTab().setText("Read"));
    tabLayout.addTab(tabLayout.newTab().setText("Me"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    //Initializing viewPager
    viewPager = (ViewPager) findViewById(R.id.pager);
  //  viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    //Creating our pager adapter
    Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());

    //Adding adapter to pager
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    //Adding onTabSelectedListener to swipe views

    tabLayout.setOnTabSelectedListener(tabLayout.this);


    //// drawable navbar


    mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); // Assigning the RecyclerView Object to the xml View

    mRecyclerView.setHasFixedSize(true);                            // Letting the system know that the list objects are of fixed size
    mAdapter = new MyAdapter(TITLES,ICONS,NAME,EMAIL,PROFILE);       // Creating the Adapter of MyAdapter class(which we are going to see in a bit)
    // And passing the titles,icons,header view name, header view email,
    // and header view profile picture

    mRecyclerView.setAdapter(mAdapter);                              // 
    mLayoutManager = new LinearLayoutManager(this);                 // 
    mRecyclerView.setLayoutManager(mLayoutManager);                 // 

    mRecyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(tabLayout.this, new 
   RecyclerItemClickListener.OnItemClickListener() {
                @Override public void onItemClick(View view, int position) {
                    switch(position){
                        case 1:
                            Intent go=new Intent(tabLayout.this,Tab1.class);
                             startActivity(go);
                            break;` 

推荐答案

您可以通过以下方法转到ViewPager中的不同项目:

You can go to different items in your ViewPager via:

viewpager.setcurrentitem(position,boolean);

这篇关于如何在点击时切换标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆