including list_content into list layout to retain ListFragment functionality

后端 未结 3 570
南方客
南方客 2021-02-05 11:47

First of, I\'m using The Android Compatibility Library V4 rev.3 for my 1.6(Donut)

When you first create a ListFragment it displays an indeterminant progress indicator u

相关标签:
3条回答
  • 2021-02-05 12:05

    One possible work-around to incorporate default @android.R.layout/list_content into custom layout when using compatibility library is to create a dummy ListFragment:

    package com.example.app;
    
    import android.support.v4.app.ListFragment;
    
    public class ListContentFragment extends ListFragment {
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            //Supress default list and empty text behavior
            //super.onViewCreated(view, savedInstanceState);
        }
    }
    

    Then including this fragment (instead of recommended list_content layout) into your custom layout:

    ...
    <fragment class="com.example.app.ListContentFragment"
        android:layout_weight   ="1" 
        android:layout_width    ="fill_parent"
        android:layout_height   ="0dip" />
    ...
    

    This way you'll get a fully functional default behavior of the ListFragment while still having custom layout for it.

    0 讨论(0)
  • 2021-02-05 12:08

    I was frustrated to see that the list_content layout was not available using the support library. My approach was simply to reuse the default onCreateView method of the ListFragment and add it as a part of my custom layout:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
        View listContent = super.onCreateView(inflater, container,
                savedInstanceState);
    
        View v = inflater.inflate(R.layout.my_custom_layout, container,
                false);
    
        FrameLayout listContainer = (FrameLayout) v.findViewById(R.id.my_list_container);
    
        listContainer.addView(listContent);
    
        return v;
    }
    

    I added a FrameLayout where the ListView used to be in my custom layout.

    0 讨论(0)
  • 2021-02-05 12:15

    The ListFragment included in the compatibility library doesn't appear to use that layout (list_content) as the real one does. This might be because it is offered as a jar and it is not possible to package resources. Here are the contents of its onCreateView:

    ListFragment from compatibility library:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final Context context = getActivity();
    
        FrameLayout root = new FrameLayout(context);
    
        // ------------------------------------------------------------------
    
        LinearLayout pframe = new LinearLayout(context);
        pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
        pframe.setOrientation(LinearLayout.VERTICAL);
        pframe.setVisibility(View.GONE);
        pframe.setGravity(Gravity.CENTER);
    
        ProgressBar progress = new ProgressBar(context, null,
                android.R.attr.progressBarStyleLarge);
        pframe.addView(progress, new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    
        root.addView(pframe, new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    
        // ------------------------------------------------------------------
    
        FrameLayout lframe = new FrameLayout(context);
        lframe.setId(INTERNAL_LIST_CONTAINER_ID);
    
        TextView tv = new TextView(getActivity());
        tv.setId(INTERNAL_EMPTY_ID);
        tv.setGravity(Gravity.CENTER);
        lframe.addView(tv, new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    
        ListView lv = new ListView(getActivity());
        lv.setId(android.R.id.list);
        lv.setDrawSelectorOnTop(false);
        lframe.addView(lv, new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    
        root.addView(lframe, new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    
        // ------------------------------------------------------------------
    
        root.setLayoutParams(new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    
        return root;
    }
    

    ListFragment from Android 4.0:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(com.android.internal.R.layout.list_content, container, false);
    }
    

    Given the choice between copying the layout file from APIv11 source and including this view initialisation code, I think it is clear which one would be considered 'hackery' ;)

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