我想根据点击的项目替换包含项目列表的片段和新片段。这是我的代码,我该如何处理?我应该在onCreateView或onActivityCreated中声明我的ListView,Adapter等吗?如果容器有所不同,那么容器就是ViewPager。
public class PreferenceListFragment extends SherlockFragment
{
ListView lv;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return (LinearLayout)inflater.inflate(R.layout.settings, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lv = (ListView)getView().findViewById(R.id.SettingsLV);
lv.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Preferences.TITLES));
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.i("FragmentList", "Item clicked: " + arg2);
// Switch case that starts fragment depending on 'position'.
Fragment1 f1 = new Fragment1();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.pager, f1);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
}
);
}
}
链接到某些非常weird behaviour from my app。
答案 0 :(得分:0)
适配器和列表视图的设置应该在onActivityCreated中完成。 OnCreateView只是第一次绘制用户界面。处理应该在onActivityCreated()中。请参阅http://developer.android.com/guide/topics/fundamentals/fragments.html