1

どのタブがアクティブであるかを知るにはどうすればよいですか

public View onCreateView(LayoutInflater インフレータ、ViewGroup コンテナ、Bundle savedInstanceState) 関数???

次のコードは最初のナビゲーションのみで機能しますが、その後はすべてのタブで最後のレイアウトが表示されます。

public final class RobozoxFragment extends Fragment {
private static final String KEY_CONTENT = "RobozoxFragment:Content";
private static int currentPosition = 100;
public static RobozoxFragment newInstance(String content,int position) {
    currentPosition = position;
    RobozoxFragment fragment = new RobozoxFragment();
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < 20; i++) {
        builder.append(content).append(" ");
    }
    builder.deleteCharAt(builder.length() - 1);
    fragment.mContent = builder.toString();
    return fragment;
}
@Override

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

    View myFragmentView;

    if(currentPosition == 1){
        myFragmentView = inflater.inflate(R.layout.first, container, false);
    }else if(currentPosition == 2){
        myFragmentView = inflater.inflate(R.layout.second, container, false);
    }else if(currentPosition == 3){

        myFragmentView = inflater.inflate(R.layout.third, container, false);
    }else if(currentPosition == 4){
        myFragmentView = inflater.inflate(R.layout.fourth, container, false);
    }else if(currentPosition ==5){
        myFragmentView = inflater.inflate(R.layout.fifth, container, false);
    }else{
        myFragmentView = inflater.inflate(R.layout.activity_main, container, false);
    }
    return myFragmentView;
}

onCreateViewアクティブなタブの場所を取得できない機能に問題があると思います。

どうすればこのことを成し遂げることができますか??

4

1 に答える 1

0

解決策を手に入れました:)バンドルを使用して引数を渡し、現在の位置を取得します。以下のサンプルコード..

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView;
    Bundle args = getArguments();
    int currentView = args.getInt(Constants.ARG_SECTION_NUMBER)-2;

    if(currentView == 0){
        rootView = inflater.inflate(R.layout.lay1, container, false);
    }else if(currentView == 1){
        rootView = inflater.inflate(R.layout.lay2, container, false);
    }else {
        rootView = inflater.inflate(R.layout.activity_main, container, false);
    }

    return rootView;
}


@Override
public Fragment getItem(int i) {
    switch (i) {
    case 0:
        // The first section of the app is the most interesting -- it offers
        // a launchpad into the other demonstrations in this example
        // application.
        return new LaunchpadSectionFragment();

    default:
        // The other sections of the app are dummy placeholders.
        Fragment fragment = new RobozoxSectionFragment();
        Bundle args = new Bundle();
        args.putInt(Constants.ARG_SECTION_NUMBER, i + 1);
        fragment.setArguments(args);
        return fragment;
    }
}
于 2012-11-01T12:11:26.610 に答える