Android-PullToRefresh(github地址)是个十分方便的下拉刷新库(也有上拉加载的功能),各个app中常见到滑到底部自动加载的功能,这里提供一个简单的实现思路:
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/id_pull_to_refresh_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@color/gray_light"
android:divider="@color/gray_light"
android:dividerHeight="@dimen/spacing_normal"
android:fadingEdge="none"
android:fadingEdgeLength="0dp"
android:scrollingCache="false"
ptr:ptrAnimationStyle="flip"
ptr:ptrHeaderTextAppearance="?android:attr/textAppearanceMedium" />
mRefreshListView=(PullToRefreshListView)findViewById(R.id.id_pull_to_refresh_listview);
mRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
footer = LayoutInflater.from(getActivity()).inflate(R.layout.footer,null,false);
mRefreshListView.getRefreshableView().addFooterView(footer);
//该回调会在滑到最后一个可见view的时候会被调用,此时我们让添加的footer可见
mRefreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
footer.setVisibility(View.VISIBLE);
//后面执行加载数据的请求,完成后调用footer.setVisibility(View.GONE)
}
});
<?xml version="1.0" encoding="utf-8"?>
<!--R.layout.footer -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="正在加载中...."
android:textSize="@dimen/common_text_size" />
注意:如果footer.setVisibility(View.GONE),扔占据空间的话给他设置一个父view
思路比较简单,希望对家有帮助