Android学习笔记:实现上拉加载更多数据的ListView

    实现原理:为ListView添加一个底部显示加载状态的FooterView,根据ListView的滚动状态来判断是否显示这个FooterView。有很多很多实现这种功能的开源控件,一般是把下拉刷新及上拉加载写在同一个Listview中的,直接贴代码啦~~

/**
 * 上拉加载更多ListView实现
 * @description:
 * @date 2015-11-5 上午10:26:22
 */
public class PullLoadMoreListView extends ListView implements OnScrollListener {
/* 加载底部* */
private View mFooterView;
/* 是否正在加载中* */
private boolean isLoad = false;
/* 底部footer布局* */
private RelativeLayout footRela;
/* 总共item数量* */
private int totalItemCount;
/* 当前可见的最后一个item* */
private int lastvisibleItem;
private LoadMoreListener mlLoadMoreListener;


public void setLoadMoreListener(LoadMoreListener mlLoadMoreListener) {
this.mlLoadMoreListener = mlLoadMoreListener;
}


public PullLoadMoreListView(Context context) {
super(context);
initFooterView(context);
}


public PullLoadMoreListView(Context context, AttributeSet attrs) {
super(context, attrs);
initFooterView(context);
}


public PullLoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initFooterView(context);
}


/**
* 初始化底部
* @description:
* @author ldm
* @date 2015-11-5 上午10:28:32
*/
private void initFooterView(Context context) {
mFooterView = LayoutInflater.from(context).inflate(R.layout.load_footer, null);
footRela = (RelativeLayout) mFooterView.findViewById(R.id.foot_rela);
footRela.setVisibility(View.GONE);
addFooterView(mFooterView);
setOnScrollListener(this);
}


@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.totalItemCount = totalItemCount;
this.lastvisibleItem = firstVisibleItem + visibleItemCount;// 获取当前可见的最后一条item位置
}


@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// 滚动状态改变时,监听加载
if (totalItemCount == lastvisibleItem && scrollState == SCROLL_STATE_IDLE) {// 当屏幕停止滚动时
if (!isLoad) {
isLoad = true;
mFooterView.setVisibility(View.VISIBLE);
}
// 加载更多数据
if (mlLoadMoreListener != null) {
mlLoadMoreListener.loadMoreData();
}
}
}


/**
* 加载完成处理
* @description:
* @date 2015-11-5 上午10:54:49
*/
public void loadMoreCompelete() {
isLoad = false;
mFooterView.setVisibility(View.GONE);
}


public interface LoadMoreListener {
void loadMoreData();// 加载更多数据接口实现
}
}

------------在Activity中简单使用--------------------

public class MainActivity extends Activity {
private PullLoadMoreListView mLoadMoreListView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLoadMoreListView=(PullLoadMoreListView) findViewById(R.id.loadmore_lv);
//为ListView添加数据,省略
//实现上拉加载功能
mLoadMoreListView.setLoadMoreListener(new LoadMoreListener() {

@Override
public void loadMoreData() {
//加载更多数据
//刷新ListViewo数据
//调用刷新完成方法
mLoadMoreListView.loadMoreCompelete();
}
});
}
}

-----------FooterView布局load_footer.xml,根据自己需求而定--------------------------------

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp" >


    <RelativeLayout
        android:id="@+id/foot_rela"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="20dp" >


        <ProgressBar
            android:id="@+id/item_progress"
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />


        <TextView
            android:id="@+id/item_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@id/item_progress"
            android:text="正在加载中..." />
    </RelativeLayout>


</LinearLayout

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值