正如@YoniSamlan 指出的那样,它可以通过简单的方式实现。你必须指定
android:layout_height="wrap_content"
在包含“加载更多”按钮的 ViewGroup 中。不必是 FrameLayout,请参见下面的使用 LinearLayout 的简单工作示例。
两张图片都显示了一个一直滚动到底部的屏幕。第一个有一个可见的页脚,围绕“加载更多”按钮。第二张图片显示,如果您将按钮的可见性设置为 GONE,则页脚会折叠。
您可以通过更改可见性再次显示页脚(在一些回调中):
loadMore.setVisibility(View.VISIBLE); // set to View.GONE to hide it again
照常执行listView初始化
// 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
<?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="wrap_content">
<Button
android:id="@+id/load_more"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="9dp"
android:gravity="center"
android:layout_gravity="center"
android:background="@drawable/transparent_white_border"
android:textColor="@android:color/white"
android:text="@string/LOAD_MORE"/>