fragment中由于inflater获取不对引发的fragment中组件不能刷新内容的问题
程序框架如下:在一个activity内部有很多个fragment需要加载,其中有个fragment中有一个listview 点击listview中的某个项之后,需要更新在这个fragment布局中的一个组件的内容。此时通过
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); // 获取组件布局
View contactsLayout = inflater.inflate(R.layout.imaxtheatre_layout,null);
注意我这段代码的红色部分,我用了null,它实际是需要一个ViewGroup类型的对象的。但是我不知道如何获得这个对象。于是就先这么用了。后来通过
TextView currentPlay = (TextView) contactsLayout.findViewById(R.id.currentPlay);
获得了组件,开始设置内容currentPlay.setText("正在播放:" + film_name);
结果发现根本就不能刷新界面。后来经过调试发现原来一定要获得父类的容器才行,所以就将onCreateView这个方法里面获得的 ViewGroup container通过成员变量,接了出来。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
contactsLayout = inflater.inflate(R.layout.imaxtheatre_layout,
container, false);
initView(contactsLayout);
return contactsLayout;
}
最后,使用全局的变量contactsLayout ,获得组件之后,成功的刷新了界面