我想在某些事件(按下按钮等)上更新片段视图层次结构
这是我的片段代码:
public class MyFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_categories_preview, container, false);
}
private void updateMainView()
{
LayoutInflater inflater = (LayoutInflater) this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout container = (LinearLayout) this.getView().findViewById(R.id.images_preview_container);
container.removeAllViews();
int layoutId = R.layout.list_preview;
int cellLayoutId = R.layout.list_image_view;
resultView = (ListView) inflater.inflate(layoutId, null);
resultView.setAdapter(new ImagesListAdapter(inflater, cellLayoutId, this.imagesList));
resultView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
container.addView(resultView);
}
// Other functions
// ...
}
按下按钮时调用函数 updateMainView()。视图未出现在片段中。
我尝试添加空的彩色视图:
View test = new View(this.getActivity());
test.setBackgroundColor(0xFFFF0000);
test.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
container.addView(test);
没有结果。
我做错了什么?