从不同的活动中启动选项卡布局(带切换案例)的特定选项卡

时间:2018-03-11 15:42:12

标签: android android-tablayout android-tabs tabactivity

我在仪表板活动的卡片视图上有这个onClickListener,它将打开选项卡布局的特定选项卡。 将其从仪表板活动调用到类别活动。这就是我在onclicklistener中所做的。

        grocery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(DashboardActivity.this, CategoryActivity.class);
            intent.putExtra("position", 2);
            startActivity(intent);

        }
    });

这是我的类别活动

public class CategoryActivity extends AppCompatActivity {

public SectionsPagerAdapter mSectionsPagerAdapter;
public ViewPager mViewPager;

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

    Intent intent = getIntent();
    if(intent == null) {
        extrasPosition = 0;
    } else {
        extrasPosition = intent.getIntExtra("position",0);
    }
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
//display the desired fragment
    FragmentTransaction frt = getSupportFragmentManager().beginTransaction();
    frt.replace(R.id.container, mSectionsPagerAdapter.getItem(extrasPosition));
    frt.addToBackStack(null);
    frt.commit();

}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // returning current tabs using switch case
        switch (position){
            case 0:
                viewClothing tab0 = new viewClothing();
                return tab0;
            case 1:
                viewElectronics tab1 = new viewElectronics();
                return tab1;
            case 2:
                viewFurniture tab2 = new viewFurniture();
                return tab2;
            case 3:
                viewGrocery tab3 = new viewGrocery();
                return tab3;
            case 4:
                viewHardware tab4 = new viewHardware();
                return tab4;
            case 5:
                viewStationary tab5 = new viewStationary();
                return tab5;
            case 6:
                viewOthers tab6 = new viewOthers();
                return tab6;
                default:
                    return null;
        }
    }
    @Override
    public int getCount() {
        // Show 7 total pages.
        return 7;
    }
}
}

我想要的是打开案例3的杂货标签。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

首先从您发送的'Test-ServiceFabricClusterManifest' is not recognized as a cmdlet... 获得int额外费用。然后,如果它不为null,则使用默认值0调用intent,如果不是,则传递位置并显示所需的片段。

getItem(position)