Java 向片段视图组添加视图

Java 向片段视图组添加视图,java,android,android-fragments,views,Java,Android,Android Fragments,Views,我想更新某个事件的片段视图层次结构(按钮按下等) 这是我的片段代码: public class MyFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this

我想更新某个事件的片段视图层次结构(按钮按下等)

这是我的片段代码:

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);
没有结果


我做错了什么?

奇怪的是,您的代码不工作,因为我测试了相同的代码,它工作了。例如,若您将测试视图的高度和宽度设置为50px会怎么样?容器布局描述中有错误