简体   繁体   English

在linearlayout中添加视图以编程方式缓慢地工作

[英]add view in linearlayout programmatically working slowly

My goal is to add my custom view inside LinearLayout.I have custom arrayList and I would to add custom views with for loop.Here is a my code snippet 我的目标是将自定义视图添加到LinearLayout中。我有自定义arrayList,我将使用for循环添加自定义视图。这是我的代码段

    public void replaceCustomView() {
    for (int i = 0; i < insertDataItems().size(); i++) {
        final LayoutInflater inflater = LayoutInflater.from(this);
        final View reView = inflater.inflate(R.layout.item_parent_child_listing, null, false);
        final TextView parentName = reView.findViewById(R.id.tv_parentName);
        final ImageView headerImageView = reView.findViewById(R.id.header_imageView);
        final LinearLayout linearLayout_childItems = reView.findViewById(R.id.ll_child_items);
        final RelativeLayout headerLayout = reView.findViewById(R.id.header_layout);
        final RelativeLayout headerImageLayout = reView.findViewById(R.id.header_image_layout);

        parentName.setText(insertDataItems().get(i).getParentName());

        if (insertDataItems().get(i).getChildDataItems() != null) {
            headerImageLayout.setVisibility(View.VISIBLE);
            headerImageLayout.setVisibility(View.VISIBLE);
            for (int j = 0; j < insertDataItems().get(i).getChildDataItems().size(); j++) {
                final LayoutInflater childInflater = LayoutInflater.from(this);
                final View childView = childInflater.inflate(R.layout.z_history_child_item, null, false);

                final TextView key = childView.findViewById(R.id.u_key);
                final TextView value = childView.findViewById(R.id.u_value);
                key.setText(insertDataItems().get(i).getChildDataItems().get(j).getKey());
                value.setText(insertDataItems().get(i).getChildDataItems().get(j).getValue());
                linearLayout_childItems.addView(childView);

            }
        } else {
            headerImageLayout.setVisibility(View.GONE);
            headerLayout.setBackgroundColor(Color.parseColor("#e8e8e8"));
        }
        linearLayout_childItems.setVisibility(View.GONE);

        if (insertDataItems().get(i).getParentName().length() > 0) {
            if (insertDataItems().get(i).isAllowDisable()) {
                headerImageView.setVisibility(View.GONE);
                linearLayout_childItems.setVisibility(View.VISIBLE);
            } else {
                headerImageView.setVisibility(View.VISIBLE);
                linearLayout_childItems.setVisibility(View.GONE);

            }


        } else {
            linearLayout_childItems.setVisibility(View.VISIBLE);
            headerLayout.setVisibility(View.GONE);
        }
        replaceLayout.post(() -> replaceLayout.addView(reView));

    }
}

I call this function like this 我这样称呼这个功能

 runOnUiThread(() -> replaceCustomView());

Custom views adding successfully,but my problem is that in a first time activity is slowly.Android need to much time to add view.My custom array's size is 20.Is it a any way to add views step by step ,not add all views each time? 自定义视图添加成功,但是我的问题是第一次活动很慢。Android需要很多时间来添加视图。我的自定义数组的大小为20。可以通过任何方式逐步添加视图,而不是添加所有视图每一次? What's a best practice ? 什么是最佳做法? thanks 谢谢

Please do not use the concept of adding runtime components when the items are more. 当项目更多时,请不要使用添加运行时组件的概念。 This will cause the activity to share the memory with view rendering logic and you'll be able to recycle the views. 这将使活动与视图渲染逻辑共享内存,并且您将能够回收视图。 this kind of implementation can be used only when there are a few (may be 2 to 5) items and can't be achieved with recyclerview due to some UI limitations. 这种实现只能在有少量(可能是2到5)项时使用,并且由于UI的限制,无法通过recyclerview实现。

Hence use a RecyclerView to load the items and you can use the concept of adding custom items inside a child item to achieve this functionality. 因此,请使用RecyclerView加载项目,并且可以使用在子项目中添加自定义项目的概念来实现此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM