如何通过夸大它只有一次同样的观点加入到父母多次有一次、它只、观点、父母

2023-09-05 10:42:51 作者:No escape(无处可逃)

我有一个垂直方向为父的LinearLayout,我希望以编程方式多次添加一些以这个父。现在我每次充气的孩子越来越增加了家长面前的每一个UI元素新的参考。这似乎并不非常有效的,是有这样做的更好方法。

目前的code我现在用的就是下面的,如果我吹一次才for循环,我得到运行时错误,他指定的孩子已经有一个家长,你必须先调用removeView()孩子的父母。

  LayoutInflater充气= LayoutInflater.from(getBaseContext());
        的LinearLayout parentPanel = findViewById(R.id.parent_pannel);

        ArrayList的<字符串> myList上=的getData();
        对于(字符串数据:myList中){
            //充气儿童
            查看项目= inflator.inflate(R.layout.list_item,NULL);
            //初始化审查UI
            TextView的dataText =(TextView中)item.findViewById(R.id.data);
            //设置数据
            dataText.setText(数据);
            //添加子
            parentPanel.addView(项目);
        }
 

解决方案

你实际上是否充气是慢?据我所知,膨胀的观点是非常快的(几乎一样快,手动创建视图)。

这可能是令人惊讶的,你听到的,但夸大其实在所有不分析XMLS。 XMLS布局被解析和$ P $对 - 处理在编译时 - 它们被存储在一个二进制形式,这使得视图通货膨胀非常有效的(这就是为什么你不能膨胀从在运行时生成的XML的图)

I have a LinearLayout with vertical orientation as parent, I want to add some view programmatically multiple times to this parent. Right now I am inflating the child every time getting new references to every UI element before adding to parent. This doesn't seem to be very efficient, is there any better way of doing this.

夸大培训效果 宣传错误观念 制造家长焦虑 一批违法教育培训机构曝光

Current code I am using is below, If I inflate only once before for loop I get runtime error "he specified child already has a parent. You must call removeView() on the child's parent first."

        LayoutInflater inflator = LayoutInflater.from(getBaseContext());
        LinearLayout parentPanel = findViewById(R.id.parent_pannel);

        ArrayList<String> myList = getData();
        for(String data : myList) {
            // inflate child
            View item = inflator.inflate(R.layout.list_item, null);
            // initialize review UI
            TextView dataText = (TextView) item.findViewById(R.id.data);
            // set data
            dataText.setText(data);
            // add child
            parentPanel.addView(item);
        }

解决方案

Did you actually check if inflate is slow? As far as I know, inflating view is very fast (almost as fast as creating views manually).

It might be surprising for you to hear but inflate in fact does not parse the XMLs at all. XMLs for layout are parsed and pre-processed at compile time - they are stored in a binary form which makes view inflation very efficient (that's why you cannot inflate a view from an XML generated at runtime).