如何删除horizo​​ntalview的孩子在android系统 [英] How to remove child of horizontalview in android

查看:137
本文介绍了如何删除horizo​​ntalview的孩子在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临无法删除子horizo​​ntalview内。如果已经有horizo​​ntalview的孩子然后删除或删除,如果没有孩子,然后addview。请参阅code,我做了什么。但为什么这不工作。

结果:其追加在horizo​​ntall布局previous视图

 第一次:
Horizo​​ntalview
1 4 7第二次
Horizo​​ntalview
1 4 7 6 0 7 8

但我想

 第二次
Horizo​​ntalview
 6 0 7 8第三次
Horizo​​ntalview
 2 5 9

我的code

 如果(horizo​​ntalScrollview.getChildCount()0){
        horizo​​ntalScrollview.removeAllViews();
        horizo​​ntalScrollview.addView(dataLayout);
    }其他{
        horizo​​ntalScrollview.addView(dataLayout);
    }


解决方案

HorizantalScrollView 仅包含一个子视图,这孩子可能添加删除孩子的意见,试试这个

 如果(horizo​​ntalScrollview.getChildCount()0){
            horizo​​ntalScrollview.removeAllViews();
            horizo​​ntalScrollview.addView(dataLayout);
        }其他{
            horizo​​ntalScrollview.addView(dataLayout);
        }

 的ViewGroup parentLayout =(ViewGroup中)horizo​​ntalScrollview.getChildAt(0);    如果(parentLayout.getChildCount()大于0){
        的for(int i = 0; I< parentLayout.getChildCount();我++){
            parentLayout.removeView(parentLayout.getChildAt(I));
        }
    }其他{
        parentLayout.addView(dataLayout);
    }

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);
    }

解决方案

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);
    }

这篇关于如何删除horizo​​ntalview的孩子在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆