Android + ListFragment with a custom view hierarchy

后端 未结 5 848
名媛妹妹
名媛妹妹 2021-02-12 22:28

I\'m attempting to customize the fragment layout by returning my own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). This inflates my custom v

相关标签:
5条回答
  • 2021-02-12 22:49

    use this pattern, works perfectly!

    inflater.inflate(R.layout.list_layout, container, false);
    

    public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)

    attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

    0 讨论(0)
  • 2021-02-12 22:55

    I think you'd better

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState) {
     View root = inflater.inflate(R.layout.main, container, false);
     return root;
     }
    
    0 讨论(0)
  • 2021-02-12 23:03

    optimized way and shot code

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.list_alphabet, container, false);
        }
    
    0 讨论(0)
  • 2021-02-12 23:04

    I wasn't returning the new view within the onCreateView method of the ListFragment class. For instance:

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
                View view = inflater.inflate(R.layout.main, null);
                return view;
            }
    

    All works well now in the land of Android!

    0 讨论(0)
  • 2021-02-12 23:06
    getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
    

    List is not accepted, it's looking for a fragment.

    0 讨论(0)
提交回复
热议问题