从片段中删除子视图

时间:2014-01-27 13:48:29

标签: android android-layout android-fragments android-view android-inflate

我在Android 2.3.3上试图以编程方式从其容器中删除子视图节点。 我在一个片段里面。 removeView的方法似乎不起作用...... 方法getCategories是一个异步的http任务,它返回对象的结果(我觉得这里没什么用,但万一我可以发布代码):

片段

public class CategoriesFragment extends ListFragment {

    private CategoriesAdapter adapter;
    private CategorySelectedListener listener;
    private DataHelper dataHelper;
    private List<Category> categories;
    private List<Category> subCategories;
    private Parcelable listState;
    private LayoutInflater inflater;
    private ViewGroup container;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        this.inflater = inflater;
        this.container = container;

        View view = inflater.inflate(R.layout.fragment_list_categories,
                container, false);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        dataHelper = new DataHelper(new LoadDataTaskCompletedListener());

        inflater.inflate(R.layout.progress_bar, container, true); // ADD CIRCLE PROGRESS BAR INSIDE CONTAINER
        dataHelper.getCategories();
    }

    private void setCategories(List<Category> categories) {
        if (isAdded()) {
            container.removeView(getActivity().findViewById(R.id.progressBar)); // NOT WORKING
            if (categories != null) {
                adapter = new CategoriesAdapter(getActivity(),
                        R.layout.row_category, categories);

                setListAdapter(adapter);
            }
        }
    }

    public class LoadDataTaskCompletedListener implements
            TaskCompletedListener<Object> {

        @Override
        public void onTaskCompleted(Object result) {
            if (result != null) {
                Bundle bundle = (Bundle) result;

                if (bundle.containsKey("categories")) {

                    categories = Arrays.asList((Category[]) bundle
                            .getParcelableArray("categories"));

                    setCategories(categories);
                }
            } else {
                setCategories(null);
            }
        }
    }
}

我在root容器中对此进度条进行了充气:

进度条

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/progressBar"
    style="Widget.ProgressBar.Large.Inverse"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >

</ProgressBar>

我想避免显示/隐藏xml节点。 我哪里错了?我的调用是在UI线程内? 我应该调用invalidate还是postInvalidate? 非常感谢。

编辑:奇怪的事情:如果我添加以下行:

container.removeView(container.findViewById(R.id.progressBar));

在onTaskCompleted内,在setCategories()里面(两次),正在正确删除进度条。

2 个答案:

答案 0 :(得分:1)

而不是

getActivity().findViewById

你应该使用

container.findViewById

答案 1 :(得分:0)

试试这个:

getActivity().findViewById(R.id.progressBar).setVisibility(View.GONE);