简体   繁体   中英

Android find a view after it is inflated

I am creating some views programmatically using ViewStubs , as shown below.

RelativeLayout allCommissionsContainer = (RelativeLayout) mBaseLayout.findViewById(R.id.allCommissionsContainer);

        ViewStub viewStubCommission = new ViewStub(mActivity);

        viewStubCommission.setLayoutResource(R.layout.viewstub_commission_container);
        allCommissionsContainer.addView(viewStubCommission);

        viewStubCommission.inflate();

But how do I find that inflated view (a RelativeLayout ) later in the code? If I use viewStubCommission.setTag("MyTag"); won't it be applied to the actual ViewStub and not the inflated View? I tried this but it gave me a null pointer exception .

I need something like allCommissionsContainer.findViewById(R.id.someId) or allCommissionsContainer.findViewWithTag("MyTag") .

After some testing I realized I can use

RelativeLayout myRelativeLayout = (RelativeLayout) viewStubCommission.inflate();

and then set a tag or id

myRelativeLayout.setTag("MyTag");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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