这是一道安卓编程题。 我关注the offical example并编写一个由两个显示列表菜单和内容的 fragment 组成的 Activity 。 当用户单击列表中的项目时,详细信息将显示在内容 fragment 中。 现在,我想在 Actionbar 中添加一个刷新按钮。当用户点击它时,内容 fragment 中的内容将被重新加载。
但是不知道如何获取正在运行的内容 fragment 的引用,并调用它来刷新。 (假设所有的内容 fragment 都已经实现了一个叫refresh()的方法)
public class FragmentLayout extends Activity {
.....
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_reload: //refresh button is clicked
//how can I call the DetailFragment to reload here?
}
public static class TitlesFragment extends ListFragment {
....
}
public static class DetailFragment extends Fragment {
...
}
简而言之,fragment和包含fragment的activity是如何进行通信的?
最佳答案
在 fragment 而不是 Activity 中覆盖 onOptionsItemSelected
,并在内部对那里的刷新事件使用react。
关于java - 刷新 fragment 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9030460/