开发者

Android Compound Controls - Inflate from XML

开发者 https://www.devze.com 2023-03-07 13:20 出处:网络
I have created a compound control that I am reusing in multiple activities. This control contains a bunch of TextViews and Buttons, and most importantly a ListView. I define the XML in a layout file a

I have created a compound control that I am reusing in multiple activities. This control contains a bunch of TextViews and Buttons, and most importantly a ListView. I define the XML in a layout file and in the constructor to this compound control, I inflate the XML as such:

    String service = Context.LAYOUT_INFLATER_SERVICE;
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(service);
    inflater.inflate(R.layout.map_menu, this, true);

The layout XML contains a ListView, and also in the constructor this compound control will handle setting up the adapter (my adapter extends ArrayAdapter) and array for it, like so:

    ListView tableOfContentsList = (ListView) findViewById(R.id.tocListView);
    _layerAdapter = new LayerAdapter(context, R.layout.toc_layer_item, _layers);
    tableOfContentsList.setAdapter(_layerAdapter);

This compound control is used in two activites - one of these activities calls another. No relation between the two activities is intended.

Here is my problem. When the compound control is created in the initial activity, the above code is called to set the adapter of this control. Then, when the second activity is created and navigated to, the constructor is called again on this second instance of the control. This seems to have a side effect on the first control located in the initial activity. The second control seems to overwrite parts of the adapter from the first control - because basically the first adapter will not be functional once the constructor to the second control is called.

It is my guess that since I am referencing the resource ID of the ListView in both controls, Android is removing the adapter from the first ListView when the second ListView is created - because it sees both ListViews as having the same resource ID? Is this possible?

I have had trouble before in this exact same case - where multiple compound controls are used in different activities (and multiple times in a single activity) - and the problem was due to inflating from XML layout. My solution to that prior problem was to get rid of the inflating from layout, and instead creating the objects through code. This was acceptable because those compound controls were much simpler and contained only two views - however I feel in the above ListView case, where my compound control has at least ten views in it, it is not an acceptable solution to define each vi开发者_JS百科ew in code. I need the layout XML.

Has anyone ever experienced this sort of clashing behavior when using custom compound controls that are inflated from XML, and re-used in multiple instances?


From my understanding Android should create a new instance of the widgets each time you inflate the xml. Do you have any static members in you compound widget class?

0

精彩评论

暂无评论...
验证码 换一张
取 消