如何在点击时切换标签

时间:2016-06-30 06:54:50

标签: android android-intent

我有三个标签,在主页选项卡上我有导航抽屉,在点击特定项目时我想使用意图转到tab3,但它不起作用。 我的代码是:

//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;` 

2 个答案:

答案 0 :(得分:0)

您正在切换片段而不是活动。 在您的onclick中调用以下方法

public void switchfragment() {
    FragmentManager manager = getActivity().getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    ft.replace(android.R.id.tabcontent, new Tab1());
    ft.commit();
}

答案 1 :(得分:0)

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

viewpager.setcurrentitem(position,boolean);