自定义上拉加载更多的ListView配合SwipeRefreshLayout实现列表刷新加载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012080791/article/details/68941684

效果图如下:

效果图

1,自定义ListView

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

/**
 * Created by HeDongDong on 2017/3/29.
 */

public class MyListView extends ListView implements OnScrollListener {

    private LoadMoreListener loadMoreListener;
    //是否可加载更多
    private boolean canLoadMore = true;
    //加载更多布局
    private LoadingMoreFooter loadingMoreFooter;
    //正在加载数据中
    private boolean isLoadingData = false;
    private int firstVisibleItem;

    public MyListView(Context context) {
        super(context, null);
        init(context);
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        init(context);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        loadingMoreFooter = new LoadingMoreFooter(context);
        //设置自定义加载中效果
        ProgressView progressView = new ProgressView(context);
        progressView.setIndicatorId(ProgressView.BallPulse); //动画效果 自己可以尝试各种风格
        progressView.setIndicatorColor(ContextCompat.getColor(context, R.color.colorAccent));//动画颜色
        loadingMoreFooter.addFootLoadingView(progressView);
    }

    //移除加载更多布局
    public void removeFooter() {
        canLoadMore = false;
        this.removeFooterView(loadingMoreFooter);
    }

    //添加加载更多布局 在removeFooter()后调用
    public void addFooter() {
        canLoadMore = true;
        this.addFooterView(loadingMoreFooter);
    }

    //下拉刷新后初始化底部状态
    public void refreshComplete() {
        if (loadingMoreFooter != null) {
            loadingMoreFooter.setGone();
        }
        isLoadingData = false;
    }

    //上拉加载后初始化底部状态
    public void loadMoreComplete() {
        if (loadingMoreFooter != null) {
            loadingMoreFooter.setGone();
        }
        isLoadingData = false;
    }


    //到底了
    public void loadMoreEnd() {
        if (loadingMoreFooter != null) {
            loadingMoreFooter.setEnd();
        }
    }

    //设置底部加载中效果
    public void setFootLoadingView(View view) {
        if (loadingMoreFooter != null) {
            loadingMoreFooter.addFootLoadingView(view);
        }
    }

    //设置加载更多监听
    public void setLoadMoreListener(LoadMoreListener listener) {
        loadMoreListener = listener;
        addFooterView(loadingMoreFooter);
        loadingMoreFooter.setGone();
        setOnScrollListener(this);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        //判断 isLoadingData:不在加载中 ,scrollState:整个滚动事件结束,firstVisibleItem:条目至少上滑一条  canLoadMore:可以加载更多
        if (!isLoadingData && scrollState == OnScrollListener.SCROLL_STATE_IDLE &&
                firstVisibleItem != 0 && canLoadMore) {
            if (loadMoreListener != null) {
                if (loadingMoreFooter != null) {
                    loadingMoreFooter.setVisible();
                }
                isLoadingData = true;
                loadMoreListener.onLoadMore();
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int
            totalItemCount) {
        //firstVisibleItem表示在屏幕第一个ListItem(部分显示的ListItem也算)在整个ListView的位置(下标从0开始)
        this.firstVisibleItem = firstVisibleItem;
    }
}

2,用法

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private SwipeRefreshLayout swipeRefreshLayout;
    private MyListView myListView;
    private List<String> list = new ArrayList();
    private MainAdapter adapter;
    private int mIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl);
        //设置圈圈颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary,
                R.color.colorAccent, R.color.colorPrimary,
                R.color.colorAccent);

        myListView = (MyListView) findViewById(R.id.lv);
        adapter = new MainAdapter(this, list);
        myListView.setAdapter(adapter);

        generateData();
        setOnClickListener();

    }

    private void setOnClickListener() {
        /**
         * 下拉刷新
         */
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mIndex = 0;
                        list.clear();
                        generateData();
                        adapter.notifyDataSetChanged();
                        //刷新完成
                        swipeRefreshLayout.setRefreshing(false);
                        myListView.refreshComplete();
                    }
                }, 1500);
            }
        });

        /**
         * 上拉加载更多
         */
        myListView.setLoadMoreListener(new LoadMoreListener() {
            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mIndex < 30) {
                            generateData();
                            adapter.notifyDataSetChanged();
                            //设置loading完成
                            myListView.loadMoreComplete();
                        } else {
                            //设置加载到底
                            myListView.loadMoreEnd();
                        }
                    }
                }, 1500);

            }
        });

    }

    /**
     * 生成数据
     */
    private void generateData() {
        for (int i = 0; i < 15; i++) {
            list.add("数据" + mIndex++);
        }
    }
}

3,其中加载效果使用的是开源库 在Module gradle加上

 compile 'com.wang.avi:library:1.0.1'

4, 详见demo

DEMO下载链接

最后感谢fangxiao大神,从他的Haorecyclerview中吸取很多东西!却忘了链接,知道的帮忙贴一下,谢谢!

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页