ListFragment Layout from xml

前端 未结 2 556
孤城傲影
孤城傲影 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 13:55

    I've converted the created layout to xml, you can add/remove your widgets or e.g. add pulltorefresh solutions. You shouldn't change the needed ids. Because of internal ids needed for some of the widgets an helper function needs to be called and it needs to be in support v4 namespace since constants are internal with default visibility.

    list_loader.xml

    
    
    
        
    
            
        
    
        
    
            
    
            
            
        
    
    

    and the class ListFragmentLayout in android.support.v4.app (in your project, you don't need to modify support v4 jar)

    package android.support.v4.app;
    
    import your.package.R;
    import android.view.View;
    
    public class ListFragmentLayout
    {
        public static void setupIds(View view)
        {
            view.findViewById(R.id.empty_id).setId(ListFragment.INTERNAL_EMPTY_ID);
            view.findViewById(R.id.progress_container_id).setId(ListFragment.INTERNAL_PROGRESS_CONTAINER_ID);
            view.findViewById(R.id.list_container_id).setId(ListFragment.INTERNAL_LIST_CONTAINER_ID);
        }
    }
    

    in your fragment that extends ListFragment

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

提交回复
热议问题