Hide footer view in ListView?

前端 未结 13 2107
既然无缘
既然无缘 2020-12-02 06:49

I have a ListView. The data behind it is fetched from the Internet, in sets of 10-30 items whenever the user scrolls all the way to the bottom. In order to indi

13条回答
  •  失恋的感觉
    2020-12-02 06:50

    As @YoniSamlan pointed out, it can be achieved in a simple way. You have to specify

    android:layout_height="wrap_content" 
    

    in the ViewGroup that contains the "Load More" button. Doesn't have to be FrameLayout, see below for a simple -working- example that uses a LinearLayout.

    Both images show a screen that is scrolled all the way to the bottom. First one has a visible footer that wraps around the "load more" button. Second images shows that the footer collapses if you set button's visibility to GONE.

    You can show again the footer (inside some callback) by changing the visibility:

    loadMore.setVisibility(View.VISIBLE);  // set to View.GONE to hide it again
    

    listView with visible footer listView with footer gone

    Perform listView initialization as usual

        // Find View, set empty View if needed
        mListView = (ListView) root.findViewById(R.id.reservations_search_results);
        mListView.setEmptyView(root.findViewById(R.id.search_reservations_list_empty));
    
        // Instantiate footerView using a LayoutInflater and add to listView
        footerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.load_more_footer_view, null, false);
        // additionally, find the "load more button" inside the footer view
        loadMore = footerView.findViewById(R.id.load_more);
        loadMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fetchData();
            }
        });
    
        // add footer view to the list
        mListView.addFooterView(footerView);
    
        // after we're done setting the footerView, we can setAdapter
        adapter = new ReservationsArrayAdapter(getActivity(), R.layout.list_item_reservations_search, reservationsList);
        mListView.setAdapter(adapter);
    

    load_more_footer_view.xml

    
    
    
    

提交回复
热议问题