从父活动到ViewPager中的片段的访问ID

时间:2016-12-10 11:41:32

标签: android android-fragments android-activity android-viewpager

我遇到过这个问题,也许已经出现了同样的问题,但我在这个页面找不到。所以我的问题是我有我的活动,初始化ViewPager,我在这个viewpager中有2个片段。

我的活动:

public class ShoppingDetailsActivity extends BaseActivity {

ViewPagerAdapter adapter;

@Bind(R.id.view_pager)
ViewPager viewPager;
@Bind(R.id.pager_tabs)
PagerSlidingTabStrip pagerSlidingTabStrip;

int shop_id;
int id;
int shop_sub_id;
int subId;
int shop_sub_sub_id;
int subSubId;
int shop_detail_id;
int detailId;

public static final String SHOP_ID = "shop_id";
public static final String SHOP_SUB_ID = "shop_sub_id";
public static final String SHOP_SUB_SUB_ID = "shop_sub_sub_id";
public static final String SHOP_DETAIL_ID = "shop_detail_id";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activityComponent().inject(this);
    setContentView(R.layout.activity_shopping_details);
    ButterKnife.bind(this);

    adapter = new ViewPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(adapter);
    pagerSlidingTabStrip.setViewPager(viewPager);

    shop_id = getIntent().getIntExtra(SHOP_ID, id);
    shop_sub_id = getIntent().getIntExtra(SHOP_SUB_ID, subId);
    shop_sub_sub_id = getIntent().getIntExtra(SHOP_SUB_SUB_ID, subSubId);
    shop_detail_id = getIntent().getIntExtra(SHOP_DETAIL_ID, detailId);
}

}

我的适配器:

public class ViewPagerAdapter extends FragmentPagerAdapter {

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

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment = new InfoAdminFragment();
            break;
        case 1:
            fragment = new ViewAdminFragment();
            break;
        default:
            break;
    }

    return fragment;
}

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

@Override
public CharSequence getPageTitle(int position) {
    if (position == 0) {
        return "InfoAdminFragment";
    } else if (position == 1) {
        return "ViewAdminFragment";
    } else {
        return super.getPageTitle(position);
    }
}
}

正如你在My Activity中看到的那样,我使用 getIntent()获得了很多ID,并且成功地获得了它们,但我还需要以某种方式在ViewPager中传递给我的两个片段,我该如何制作解决这个问题?

2 个答案:

答案 0 :(得分:2)

将ID作为捆绑包从您的活动传递到ViewPager适配器

adapter = new ViewPagerAdapter(getSupportFragmentManager(), getIntent().getExtras());

修改您的ViewPager适配器,如下所示

public class ViewPagerAdapter extends FragmentPagerAdapter {
private Bundle bundle;

public ViewPagerAdapter(FragmentManager fm, Bundle bundle) {
    super(fm);
    this.bundle = bundle;
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment = new InfoAdminFragment();
            break;
        case 1:
            fragment = new ViewAdminFragment();
            break;
        default:
            fragment = new InfoAdminFragment();
            break;
    }
    fragment.setArguments(bundle);
    return fragment;
}

//other code

}

并检索片段中的包

Bundle bundle = getArguments();
shopId = bundle.getInt(ShoppingDetailsActivity.SHOP_ID);

答案 1 :(得分:1)

使用以下代码检查您的代码:

private int shop_id;
private int shop_sub_id;
private int shop_sub_sub_id;
private int shop_detail_id;

public void setExtraData(int shopId, int shopSubId, shopSubSubId, shopDetailId) {
    this.shop_id = shopId;
    this.shop_sub_id = shopSubId;
    this.shop_sub_sub_id = shopSubSubId;
    this.shop_detail_id = shopDetailId;
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment = new InfoAdminFragment();
            break;
        case 1:
            fragment = new ViewAdminFragment();
            break;
        default:
            break;
    }

    setBundleData(fragment);

    return fragment;
}

public void setBundleData(Fragment fragment){
    Bundle bundle = new Bundle();
    bundle.putInt("shop_id", shop_id);
    bundle.putInt("shop_sub_id", shop_sub_id);
    bundle.putInt("shop_sub_sub_id", shop_sub_sub_id);
    bundle.putInt("shop_detail_id", shop_detail_id);
    fragment.setArguments(bundle);
}

在你的活动中:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activityComponent().inject(this);
    setContentView(R.layout.activity_shopping_details);
    ButterKnife.bind(this);

    shop_id = getIntent().getIntExtra(SHOP_ID, id);
    shop_sub_id = getIntent().getIntExtra(SHOP_SUB_ID, subId);
    shop_sub_sub_id = getIntent().getIntExtra(SHOP_SUB_SUB_ID, subSubId);
    shop_detail_id = getIntent().getIntExtra(SHOP_DETAIL_ID, detailId);

    adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.setExtraData(shop_id, shop_sub_id, shop_sub_sub_id, shop_detail_id);
    viewPager.setAdapter(adapter);
    pagerSlidingTabStrip.setViewPager(viewPager);

}

在您的片段中,您可以获得:

getArguments().getInt(/*Key*/, /*Default value*/);

像:

getArguments().getInt("shop_id", -1);

您可以通过传递intent而不是单一值来实现相同的上述方案,就像我在适配器的setExtraData方法中所做的那样。