Can you use the merge tag with fragments?

前端 未结 1 458
一生所求
一生所求 2020-12-05 22:45

If I use the merge tag as the parent tag for my fragment\'s layout, I run into two issues:

  • first, in onCreateView(), if I specify

相关标签:
1条回答
  • 2020-12-05 23:07

    Is it possible to get around either of these rules to use the merge tag for a fragment's layout?

    No. As you already seen, when you inflate a layout file which has the merge tag as its root you must attach it to a valid parent ViewGroup. Attaching it to the container in the onCreateView is incorrect as the View returned by that method will be added by the framework.

    You could always just create a wrapper layout in the onCreateView method to which to attach the inflated layout(and return this wrapper layout), but this will make the merge tag optimization useless as you could add the wrapper layout in the xml layout file from the start:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         LinearLayout wrapper = new LinearLayout(getActivity()); // for example
         inflater.inflate(R.layout.layout_with_merge_as_root, wrapper, true);
         return wrapper;
    }
    
    0 讨论(0)
提交回复
热议问题