Android Fragment在错误的地方

时间:2016-04-05 07:31:14

标签: android android-fragments

我有一个奇怪的问题。

在主要活动中,我使用此代码:

    public class MyPagerAdapter extends FragmentPagerAdapter {

    private final String[] TITLES = {"Laatste Nieuws", "Uitrukken", "Voertuigen", "Contact", "Top Grossing"};

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

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public Fragment getItem(int position) {

        Fragment fragment = null;

        switch (position) {
            case 0:
                fragment = NaviContentFragment.newInstance(position);
                break;
            case 1:
                fragment = NaviContentFragmentTwo.newInstance(position);
                break;
            case 2:
                fragment = NaviContentFragment.newInstance(position);
                break;
            case 3:
                fragment = NaviContentFragment.newInstance(position);
                break;
            case 4:
                fragment = NaviContentFragment.newInstance(position);
                break;
            default:
                break;
        }

        return fragment;
    }
}

因此,当我打开一个标签时,它会打开一个新片段。当我打开标签为“Uitrukken”的标签1时,它应该打开另一个片段而不是其他片段。这个片段:

public class NaviContentFragmentTwo extends Fragment {

private static final String ARG_POSITION = "position";

public static NaviContentFragment newInstance(int position) {

    NaviContentFragment fragment = new NaviContentFragment();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    fragment.setArguments(b);

    return fragment;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = rootView = inflater.inflate(R.layout.navi_content_two, container, false);

    TextView textViewNaviConTwo = (TextView) rootView.findViewById(R.id.textViewNaviConTwo);

    textViewNaviConTwo.setText("HALLLOOOTJES :)");

    return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}
}

使用文本“HALLLOOOTJES :)”。只有那个片段。但另一种方式发生了。除了标签号1之外,此文本显示在所有选项卡上。

我当然错过了什么。

截图:

It should say "HALLLOOOTJES :)"

2 个答案:

答案 0 :(得分:1)

NaviContentFragmentTwo课程中,您将从NaviContentFragment方法返回newInstance

我的猜测是你应该返回NaviContentFragmentTwo个实例。

所以它应该是......

public class NaviContentFragmentTwo extends Fragment {

  private static final String ARG_POSITION = "position";
  public static NaviContentFragmentTwo newInstance(int position) {
       NaviContentFragmentTwo fragment = new NaviContentFragmentTwo ();
       Bundle b = new Bundle();
       b.putInt(ARG_POSITION, position);
       fragment.setArguments(b);

       return fragment;
  }

答案 1 :(得分:0)

此代码可能是可能的原因:

public static NaviContentFragment newInstance(int position) {

NaviContentFragment fragment = new NaviContentFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
fragment.setArguments(b);

return fragment;
}

尝试将NaviContentFragment替换为NaviContentFragmentTwo。