我正在尝试使用ListFragment和自定义适配器显示列表。每行都包含一个TextView和一个按钮。看起来像这样:
单击按钮时,我希望列表的元素更改。为此,我想用另一个替换现有的ListFragment。我的问题是第二个片段被添加到第一个片段上,像这样:
这是单击自定义ArrayAdapter中的按钮时执行的代码:
public void onClick(View view) {
//Some unrelated stuff
//I want to replace the old ListGeneratedFragment by a new one
ListGeneratedFragment fragment = new ListGeneratedFragment();
((MainActivity)getContext())
.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_list_meals, fragment)
.commit();
}
});
我已经在代码的其他部分中使用了相同的技术,并且效果很好。我不明白为什么旧的片段在这里没有被替换。