Android自定义ListView实现分页加载

本文介绍了一种在Android应用中实现ListView分页加载的方法。通过自定义ListView并实现OnScrollListener接口,可在用户滚动到列表底部时自动加载更多数据。文章详细展示了如何添加footerView、设置回调接口,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用到分页加载一般在ListView滑动中,思路如下:
1.自定义ListView,并实现OnScrollListener接口

public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
//当第一个可见Item的位置 + 可见Item的数量 == 所有Item数量时
  if(firstVisibleItem + visibleItemCount == totalItemCount){
       //1.显示footerView
       //2.调用接口通知Activity加载数据
  }
}

2.在构造函数中,listview添加footerView,绑定滚动监听

public LoadListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private void initView(Context context) {
        footerView = LayoutInflater.from(context).inflate(
                R.layout.listview_footer, null);
        footerView.findViewById(R.id.load_layout).setVisibility(View.INVISIBLE);
        this.addFooterView(footerView);
        this.setOnScrollListener(this);
        this.setDividerHeight(dividerHeight);
    }

3.设一个回调接口,到时候让Activity实现这个接口

// 接口回调
    public interface ListViewOnLoaderListener {
        void onLoad();
    }

    public void setListViewOnLoaderListener(
            ListViewOnLoaderListener loaderListener) {
        this.loaderListener = loaderListener;
    }

4.设置一个公共方法,表示完成了加载,让footerView不可见

/**
     * 加载完毕
     */
    public void loadComplete() {
        isLoad = false;
        footerView.findViewById(R.id.load_layout).setVisibility(View.INVISIBLE);
    }

具体的完整的自定义ListView代码在下面

package com.king.mircosoftband.view;

import com.king.mircosoftband.R;
import com.king.mircosoftband.util.DisplayUtil;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

public class LoadListView extends ListView implements OnScrollListener {

    private boolean isLoad = false;
    private ListViewOnLoaderListener loaderListener;
    private View footerView;
    private int totalItemCount;// 总数量;
    private int lastVisibleItem;// 最后一个可见的item;
    private int dividerHeight = DisplayUtil.dip2px(getContext(), 8);

    public LoadListView(Context context) {
        this(context, null);
    }

    public LoadListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private void initView(Context context) {
        footerView = LayoutInflater.from(context).inflate(
                R.layout.listview_footer, null);
        footerView.findViewById(R.id.load_layout).setVisibility(View.INVISIBLE);
        this.addFooterView(footerView);
        this.setOnScrollListener(this);
        this.setDividerHeight(dividerHeight);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        this.lastVisibleItem = firstVisibleItem + visibleItemCount;
        this.totalItemCount = totalItemCount;

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (lastVisibleItem == totalItemCount) {
               if(!isLoad && loaderListener != null ){
                   isLoad = true;
                   footerView.findViewById(R.id.load_layout).setVisibility(View.VISIBLE);
                   loaderListener.onLoad();

               }
        }

    }

    /**
     * 加载完毕
     */
    public void loadComplete() {
        isLoad = false;
        footerView.findViewById(R.id.load_layout).setVisibility(View.INVISIBLE);
    }

    // 接口回调
    public interface ListViewOnLoaderListener {
        void onLoad();
    }

    public void setListViewOnLoaderListener(
            ListViewOnLoaderListener loaderListener) {
        this.loaderListener = loaderListener;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值