多个片段实例

时间:2015-05-07 23:59:00

标签: android android-fragments

我创建了一个片段,我想添加多个实例:

public class myFragment extends Fragment {
    int id;
    public static myFragment newInstance(int id) {
        myFragment fragment = new myFragment();
        int id = id;
        return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
     View rootView = inflater.inflate(R.layout.fragment_main, container, false);
     TextView idText = (TextView)
               rootView.findViewById(R.id.idText);
     idText.setText(id);
     return rootView;
}

在我的主课堂中,

private int currentID;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
       currentId = 0;
        FragmentManager manager = getFragmentManager();
        FragmentTransaction fragmentTransaction =  
                  manager.beginTransaction();
        oneFragment = new myFragment();
        oneFragment.newInstance(currentId);
        currentId = 2;
        fragmentTransaction
                .replace(R.id.container, oneFragment)
                .commit();
        twoFragment = new myFragment();
        twoFragment.newInstance(currentId);
        currentId = 3;

问题是,oneFragment即将出现id 2.我不完全确定为什么会出现这种情况。容器是LinearLayout,具有垂直方向,Fragment也是线性的。我想重复使用我为myFragment为所有ID编写的代码。

2 个答案:

答案 0 :(得分:1)

您的MyFragment类需要像这样传输ID:

public class MyFragment extends Fragment {

    private final static String ID_KEY = "id_key";
    public int id;

    public static MyFragment newInstance(int id) {
        MyFragment fragment = new MyFragment();
        Bundle args = new Bundle();
        args.putInt(ID_KEY, id);
        fragment.setArguments(args);
        return fragment;
    }

    public MyFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getInt(ID_KEY);
        }
    }
}

我认为您不需要创建片段然后在其上调用newInstance()。我想你可以通过调用newInstance()来逃脱。试试这个:

private int currentID;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
       currentId = 0;
        FragmentManager manager = getFragmentManager();
        FragmentTransaction fragmentTransaction =  
                  manager.beginTransaction();
        MyFragment oneFragment = MyFragment.newInstance(currentId);
        currentId = 2;
        fragmentTransaction
                .replace(R.id.container, oneFragment)
                .commit();
        MyFragment twoFragment = MyFragment.newInstance(currentId);
        currentId = 3;

答案 1 :(得分:0)

您应该使用构造函数为每个片段传递id。因此,每个片段实例都将id保存在成员变量

public class MyFragment extends Fragment {
    private int id;

    // constructor
    public MyFragment() {
    }

    public MyFragment(int a) {
        this.id = a;
    }

    // this method is not requreied if you use above Constuctor
    public myFragment newInstance(int id) {
        return new myFragment(id);
    }

}

主类:

private int currentID = 0;
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
       currentId = 0;
        FragmentManager manager = getFragmentManager();
        FragmentTransaction fragmentTransaction =  
                  manager.beginTransaction();
        oneFragment = new myFragment(currentID);  // id = 0

        currentId = 2;
        fragmentTransaction
                .replace(R.id.container, oneFragment)
                .commit();
        twoFragment = new myFragment(currentID);  // id = 2

        currentId = 3;
    }