ListFragment Layout from xml

前端 未结 2 558
孤城傲影
孤城傲影 2020-12-06 13:22

How to create a ListFragment (of the support v4 library) layout from xml? ListFragment sets up its layout programmatically, so

2条回答
  •  醉话见心
    2020-12-06 13:47

    From the android sdk (ListFragment) (http://developer.android.com/reference/android/app/ListFragment.html):

    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

    Provide default implementation to return a simple list view. Subclasses can override to replace with their own layout. If doing so, the returned view hierarchy must have a ListView whose id is android.R.id.list and can optionally have a sibling view id android.R.id.empty that is to be shown when the list is empty.

    If you are overriding this method with your own custom content, consider including the standard layout list_content in your layout file, so that you continue to retain all of the standard behavior of ListFragment. In particular, this is currently the only way to have the built-in indeterminant progress state be shown.

    So when you want your own layout.xml for a FragmentList overide onCreateView and inflate your own layout.

    For example:

    Create a android layout (yourlistlayout.xml) and put the following xml on the position where you want to show your ListFragment.

        
        
    

    in your ListFragment class put the following code:

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

提交回复
热议问题