Search code examples
androidandroid-listfragmentandroid-progressbar

Progress bar is not displayed when using addFooterView


I try to display a progress bar at the bottom of a list view.

When I scroll to the bottom of the list, more items are load and the progress bar is displayed, the progress bar is hidden when loading is done.

But at the moment I am stuck at displaying the progress bar (not caring about displaying or hiding it yet as they are easy to handle). I applied many solutions on StackOverflow but they just didn't work.

This is how I add the footer view in my list fragment:

public class ItemListFragment extends ListFragment {
    protected View footerView;

    public ItemListFragment() {

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final LayoutInflater li = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.footerView = li.inflate(R.layout.progress_bar, null);
        getListView().addFooterView(this.footerView);
    }
}

My progress_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/progress_bar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

My fragment_item_list.xml is the default file created by Android Studio (I post just in case you need it)

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

If you have a solution please post your answer. Thanks!


Solution

  • Well, I solved the problem by switching to use Fragment instead of ListFragment.

    My fragment:

    public class TestFragment extends Fragment {
        static final String[] numbers = new String[] { "one", "two", "three",
                "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
                "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
                "seventeen", "eighteen", "nineteen", "twenty", "twenty one",
                "twenty two" };
    
        public TestFragment() {
    
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.test_list_view, container, false);
    
            ListView listView = (ListView) rootView.findViewById(R.id.test_list_view);
    
            View footer = View.inflate(getActivity().getApplicationContext(), R.layout.test_footer, null);
            listView.addFooterView(footer);
    
            ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, numbers);
    
            listView.setAdapter(adapter);
    
            return rootView;
        }
    }
    

    test_list_view.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ListView
            android:id="@+id/test_list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>
    

    test_footer.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
       <ProgressBar
           android:layout_width="match_parent"
           android:layout_height="wrap_content" />
    
    </LinearLayout>