如何在Fragment中使用findViewById

如题所述

1、在Fragment中view.getActivity().findViewById(R.id.xx);
2、其中getActivity可以获取到该Fragment引用的Activity,
3、之后通过findViewById获取到其他与该Activity相关联的Fragment中的控件引用。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-03-08
Fragment中如何获取listview我的FileFragment继承了Fragment,现在我想要在FileFragment中获取获取file.xml

Fragment中如何获取listview
我的FileFragment继承了Fragment,现在我想要在FileFragment中获取获取file.xml中的一个listview,我用了下面两种方式:
//fileListView=(ListView)getActivity().findViewById(R.id.file_list);得到空指针
fileListView=(ListView)inflater.inflate(R.id.file_list, null);找不到资源

请指教在Fragment中如何获取listview。
[解决办法]

View rootView = inflater.inflate(R.layout.file, null); //先解析file.xml布局,得到一个view
ListView listView = (ListView) rootView.findViewById(R.id.file_list);