gpt4 book ai didi

android - 用另一个 Fragment 替换 ListFragment

转载 作者:行者123 更新时间:2023-11-29 20:11:48 32 4
gpt4 key购买 nike

我正在学习 Fragments 并尝试实现我自己的任务,同时遵循以下教程:developer.android

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

这是我的代码:

我的列表 fragment :

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);
}
}

主要 Activity :

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();
}
}

最佳答案

问题不在于您使用的是相同的 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();
}

关于android - 用另一个 Fragment 替换 ListFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34689412/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com