How to remove child of horizontalview in android

Deadly 提交于 2020-01-06 12:57:24

问题


I faced not delete child inside the horizontalview. if already have child of horizontalview then delete or remove and if not child then addview. please see code, what i did. but why this not working.

Result: it append previous view in horizontall layout.

first time:
Horizontalview
1 4 7

second time
Horizontalview
1 4 7 6 0 7 8

but i want

second time
Horizontalview
 6 0 7 8

third time
Horizontalview
 2 9 5

my code

   if(horizontalScrollview.getChildCount()>0){
        horizontalScrollview.removeAllViews();
        horizontalScrollview.addView(dataLayout);
    }else {
        horizontalScrollview.addView(dataLayout);
    }

回答1:


HorizantalScrollView contains only one child view , that child may add or remove child views, try this:

if (horizontalScrollview.getChildCount() > 0) {
            horizontalScrollview.removeAllViews();
            horizontalScrollview.addView(dataLayout);
        } else {
            horizontalScrollview.addView(dataLayout);
        }

to

    ViewGroup parentLayout = (ViewGroup) horizontalScrollview.getChildAt(0);

    if (parentLayout.getChildCount() > 0) {
        for (int i = 0; i < parentLayout.getChildCount(); i++) {
            parentLayout.removeView(parentLayout.getChildAt(i));
        }
    } else {
        parentLayout.addView(dataLayout);
    }


来源:https://stackoverflow.com/questions/27917329/how-to-remove-child-of-horizontalview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!