从不同 activity 启动选项卡布局的特定选项卡(带开关盒)

Start a specific tab of a tab layout (with switch cases) from different activity

我在仪表板 activity 的卡片视图上有这个 onClickListener,它将打开选项卡布局的特定选项卡。 从仪表板 activity 调用它到类别 activity。这就是我在 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);

        }
    });

这是我的分类activity

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 的杂货选项卡。 提前致谢。

首先从您要发送的 intent 中获得 int 额外费用。然后,如果它不为空,则使用默认值 0 调用 getItem(position),如果不是,则传递该位置并显示所需的片段。

 public class CategoryActivity extends AppCompatActivity {

public SectionsPagerAdapter mSectionsPagerAdapter;
public ViewPager mViewPager;
public int extrasPosition;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category);
    //get extra from intent;
    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
    mViewPager.setCurrentItem(extrasPosition);
}

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;
    }
}
}