删除除第一个布局外的布局

sup*_*han 4 android android-layout

调用以下方法时,动态布局将填充到parent_view中。

public void Generate_tour_plan(String mark, OnClickListener o) {
        parent_view = (LinearLayout) rootView
                .findViewById(R.id.tour_iternary_wrapper);
        hiddenInfo1 = getActivity().getLayoutInflater().inflate(
                R.layout.tour_plan, null, false);
        Button minus = (Button) hiddenInfo1.findViewById(R.id.plus);
        minus.setText(mark);
        CustomAutoCompleteTextView dealer_search = (CustomAutoCompleteTextView) hiddenInfo1
                .findViewById(R.id.account_code);
        CustomAutoCompleteTextView town_search = (CustomAutoCompleteTextView) hiddenInfo1
                .findViewById(R.id.town);
        town_search.setAdapter(city_adapter);
        dealer_search.setAdapter(adapter);
        minus.setOnClickListener(o);

        parent_view.addView(hiddenInfo1, 0);

    }
Run Code Online (Sandbox Code Playgroud)

在这里,外部布局将像stack一样添加eg.(5,4,3,2,1),因此我需要删除除第一个布局之外的所有布局。

for (int j2 = parent.getChildCount() - 1; j2 > 0; j2--) {
                    Log.i("TAG IS", j2 + "");
                    ViewGroup vv = (ViewGroup) parent.getChildAt(j2);
                    vv.removeAllViews();

                }
Run Code Online (Sandbox Code Playgroud)

上面的代码是从上到下删除项目的,但这在下到上的工作。我想像堆栈一样删除项目。

Ahm*_*egy 6

您可以使用ViewGroup.removeViews(start,count)方法。

要删除除第一个视图以外的所有子视图/布局:

layout.removeViews(1, layout.getChildCount() - 1)


ved*_*ved 0

尝试removeViewAt(position)这样调用:

parent_view.removeViewAt(位置);