将活动导航抽屉中的不同值传递给相同的片段

时间:2016-01-19 07:54:23

标签: android android-fragments

我想将数据从活动传递到片段。我创建的活动是导航抽屉活动。点击菜单后,它将指向片段页面。想问一下如何将不同的数据传递给同一个片段?

hikariCP

1 个答案:

答案 0 :(得分:1)

Try like this
  AdapterClass:
  @Override
  public Fragment getItem(int index) {
try
{
    switch (index) {
    case 0:
        return GamesFragment.newInstance(0);
    case 1:
        return GamesFragment.newInstance(1);
    case 2:
        return GamesFragment.newInstance(2);
    case 3:
        return GamesFragment.newInstance(3);
    case 4:
        return GamesFragment.newInstance(4);
    case 5:
        return GamesFragment.newInstance(5);
    case 6:
        return GamesFragment.newInstance(6);
    case 7:
        return GamesFragment.newInstance(7);
    case 8:
        return GamesFragment.newInstance(8);
    case 9:
        return GamesFragment.newInstance(9);
    }
 }catch(Exception e)
{
e.printStackTrace();
}

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 10;
}

  Activity:
 public static GamesFragment newInstance(int page) {
    GamesFragment fragmentFirst = new GamesFragment();
    Bundle args = new Bundle();
    args.putInt("someInt", page);
    fragmentFirst.setArguments(args);
    return fragmentFirst;
}