How many ViewStubs is too many for a single layout XML file?

橙三吉。 提交于 2019-11-30 20:28:43

I don't think you'll see a big performance hit. It's still cheaper than having all of them inflated from the begining.

The downside of having so many stubs is that you could lose sight of the whole design. Maybe it makes more sense to group several views/items into one viewgroup. Maybe you could explain what you're attempting to do and see if there is a better way to realize it

edit: Well, instead of having multiple ViewStubs which include different subviews like

<ViewStub android:id="@+id/stub"
           android:inflatedId="@+id/activity1"
           android:layout="@layout/myActivity1"
           />
<ViewStub android:id="@+id/stub2"
           android:inflatedId="@+id/activity2"
           android:layout="@layout/myActivity2"
           />

just have a single ViewStub and in your acitivities onCreate() do something like

setContentView(R.layout.base_layout);
ViewStub stub = (ViewStub)findViewById(R.id.stub);

stub.setInflateId(R.id.activity1);
stub.setLayoutResource(R.layout.myActivity2);
stub.inflate();

This way you'd still have only one ViewStub in your base_layout, which you could setup in code before inflating.

LinearLayout or RelativeLayout is extend of ViewGroup

so i used ViewGroup as parameter this is my solution

public CategoryViewController(Context context, ViewGroup containerView) {
        this.context = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        View categoryLayout = inflater.inflate(R.layout.category_section, null, false);
        containerView.addView(categoryLayout);
        this.categoryView = categoryLayout;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!