RemoveAllViews不删除视图

时间:2014-01-10 10:12:28

标签: android layout android-view

我尝试删除线性布局的所有子视图但没有删除任何东西,奇怪的是相同的代码在另一个应用程序中工作。以下是每当选择了一个选项卡时填充choiceHolder的方法的示例代码(我删除所有视图并标记所选视图并根据它填充:

/** a method to fill the upper bar where we choose the {@link CategoriesMyBox}
 * @param category
 */
private void fillNavigationBar( CategoriesGetter category) {
    TextView categoryTxt = new TextView(getActivity());
//      categoryTxt.setAllCaps(true);
        LinearLayout.LayoutParams txtParams;
        if (category.isSelected() /*|| (category.getId_categorie()==0 && allselected)*/) {
        txtParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        txtParams.gravity = Gravity.CENTER;
        categoryTxt.setGravity(Gravity.CENTER);
        categoryTxt.setTextSize(categoryTxt.getTextSize());
        txtParams.setMargins(10, 0, 10, 0);
        categoryTxt.setPadding(5, 5, 5, 5);
        if (category.getId_categorie() == 0) {
            categoryTxt.setText(getResources().getString(R.string.all_boxs));
        }else {
            categoryTxt.setText(category.getName_categorie());
        }
        categoryTxt.setTextColor(Color.GREEN);
        categoryTxt.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_categories_selected));
//          categoryTxt.setBackgroundColor(Color.parseColor("#E3E8E6"));
        categoryTxt.setTag(category);


    }else {

        txtParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        txtParams.setMargins(10, 0, 10, 0);
        categoryTxt.setGravity(Gravity.CENTER);
        categoryTxt.setPadding(5, 5, 5, 5);
        txtParams.gravity = Gravity.CENTER;
        if (category.getId_categorie() == 0) {
            categoryTxt.setText(getResources().getString(R.string.all_boxs));
        }else {
            categoryTxt.setText((category.getName_categorie()));
        }
        categoryTxt.setTextColor(Color.GRAY);
        categoryTxt.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_categories));
//          categoryTxt.setBackgroundColor(Color.parseColor("#777777"));
        categoryTxt.setTag(category);
    }
    choiceHolder.addView(categoryTxt, txtParams);
    categoryTxt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            CategoriesGetter cat = (CategoriesGetter)v.getTag();
            id_cat = cat.getId_categorie();
            for (int i = 0; i < categories.size(); i++) {
                categories.get(i).setSelected(false);
            }
                cat.setSelected(true);
    //              choiceHolder.removeAllViews();
            /*for(int i=0; i<((ViewGroup)choiceHolder).getChildCount(); ++i) {
                View nextChild = ((ViewGroup)choiceHolder).getChildAt(i);
                choiceHolder.removeView(nextChild);
            }*/
            while (((ViewGroup)choiceHolder).getChildCount() >0) {
                View nextChild = ((ViewGroup)choiceHolder).getChildAt(0);
                choiceHolder.removeView(nextChild);
            }
//              choiceHolder.removeAllViewsInLayout();
            for (int i = 0; i < categories.size(); i++) {

                fillNavigationBar(categories.get(i));
            }
            callbackCategory.selectCategory(cat.getId_categorie());





        }
    });
}

choiceHolder是LinearLayout

这是一张显示问题的图片:enter image description here

我的所有应用程序的行为方式都相同,即使更新内部列表,我仍然可以看到之前的列表。

5 个答案:

答案 0 :(得分:4)

我发现此问题的解决方案是从fullScreen中的活动中删除AndroidManifest主题标记,如下所示:

<application
    android:name="com.paperpad.mybox.ApplicationInit"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.paperpad.mybox.activities.SplashActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.crashlytics.ApiKey"
        android:value="023e7fe8a4a93f93ffe7510201929d081b125313" />

    <activity
        android:name="com.paperpad.mybox.activities.BoxsMainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/title_activity_boxs_main"
        android:theme="@style/FullscreenTheme" >
    </activity>
    <activity
        android:name="com.paperpad.mybox.LoginActivity"
        android:label="@string/title_activity_login"
        android:windowSoftInputMode="adjustResize|stateVisible" >
    </activity>
</application>

从活动android:theme="@style/FullscreenTheme"中移除BoxsMainActivity一切正常。希望这有助于某人...

答案 1 :(得分:2)

确保通过视图运行某些内容的最佳方法是通过帖子运行它: 例如

someView.post(new Runnable(){
    @Override
    public void run()
    {
        someView.removeAllViews();
    }
}

答案 2 :(得分:1)

您是否尝试在choiceHolder上使用.invalidate()或.postInvalidate()?

答案 3 :(得分:1)

在您的xml文件中,您可以为页面中的相同位置定义更多布局,假设您有一个页面底部的表面,您可以定义具有相同属性的布局1和布局2,并且可以使用可见性消失了,在您的代码中,您将在某些操作中更改此设置,并且您将切换可见性。

答案 4 :(得分:1)

问题可能是必须将choiceHolder声明为“最终”以在“onClick”中调用 - 将View调用放在runnable中,以便它不被声明为final应修复它。没有编译器错误,因为它调用方法是有效的,并且方法调用仍然可能导致视图出现在屏幕上,即使父布局(“choiceHolder”)没有保留对它的引用,允许您删除它。

相关问题