将ListFragment替换为另一个片段

时间:2016-01-09 03:31:14

标签: android

我正在学习片段并尝试实现我自己的任务,也遵循这些教程:developer.android

这里我正在动态地将ListFragment添加到容器中。然后在单击ListFragment中的任何项后,新的EditFragment应该替换容器中的当前片段。但是在点击列表项后我收到了IllegalStateException。这是因为我对两个片段都使用相同的TransactionManager吗?

这是我的代码:

MyListFragment:

public class MyListFragment extends ListFragment {

    Communicator communicator;
    private MenuItem addItem;
    private MenuItem clearAll;
    private String name;

    ArrayList list1 = new ArrayList();
    String[] list2 = {"Sunday", "Monday",
            "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    ArrayAdapter<String> adapter;
    int randomNum;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        communicator = (Communicator) getActivity();
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list1);
        setListAdapter(adapter);
        setHasOptionsMenu(true);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.list_fragment, menu);

        addItem = menu.findItem(R.id.add_string);
        clearAll = menu.findItem(R.id.clear);

        addItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Random rand = new Random();
                randomNum = rand.nextInt(list2.length);
                list1.add(list2[randomNum]);
                adapter.notifyDataSetChanged();
                return true;
            }
        });

        clearAll.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                list1.clear();
                adapter.notifyDataSetChanged();
                return true;
            }
        });
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        communicator.onMyListItemClick(position);
    }

    public interface Communicator{
        public void onMyListItemClick(int position);
    }
}

MainActivity:

public class MainActivity extends AppCompatActivity implements MyListFragment.Communicator{

    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragContainer, new MyListFragment());
        fragmentTransaction.commit();
    }

    @Override
    public void onMyListItemClick(int position) {
        EditFragment editFragment = new EditFragment();

        Bundle args = new Bundle();
        args.putInt("position", position);
        editFragment.setArguments(args);
        fragmentTransaction.replace(R.id.fragContainer, editFragment);

        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}

1 个答案:

答案 0 :(得分:2)

问题不是您使用相同的FragmentManager,而是在调用commit()之后尝试重用FragmentTransaction。

您需要在onMyListItemClick()中创建一个新的事务,如下所示:

public void onMyListItemClick(int position) {
    MainActivityFragment editFragment = new MainActivityFragment();

    Bundle args = new Bundle();
    args.putInt("position", position);
    editFragment.setArguments(args);

    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragContainer, editFragment);

    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}