如何使用onActivityCreated在ListFragment上实现SwipeRefreshLayout

时间:2015-11-14 19:21:32

标签: android android-fragments android-listfragment swiperefreshlayout

A我的应用上有一个ListFragment,它有一个onActivityCreated方法,在创建活动时运行:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    readJson(); //populates the list
    featured = new ArrayList();
}

我想实现SwipeRefreshLayout,因此我使用了Android Docs中的SwipeRefreshListFragment。但是,我注意到在Android文档中,他们的片段使用onCreateonViewCreated方法来实现此功能。但是,由于我使用onActivityCreated,我无法模仿他们的设置。怎么会在这里使用SwipeRefreshLayout?我尝试将代码从onActivityCreated更改为onCreate,但由于我使用onActivityCreated方法构建了片段,因此无效。

1 个答案:

答案 0 :(得分:0)

我无法看到他们在片段onCreate中做了什么。 "一切"就像你期望的那样onCreateView。我已经"继承了"旧的,糟糕的代码就像你想要的那样。唯一区别:使用getActivity().findViewById(...)搜索SwipeRefreshLayout。我不知道为什么他们没有在onCreateView()中保存对片段视图的引用,但我不在乎,因为我把代码移到那里......

"我使用onActivityCreated"构建了片段。对我没有任何意义。 onActivityCreated()是片段的一种方法 - 所以它已经构建好了。您应该在onCreateView()中构造适配器,然后在那里调用readJson()。