简单好用的上拉加载下拉刷新 BaseRecyclerViewAdapterHelper

一个强大并且灵活的 Android  RecyclerViewAdapter。

  • 优化Adapter代码(减少百分之70%代码)
  • 添加点击item点击、长按事件、以及item子控件的点击事件
  • 添加加载动画(一行代码轻松切换5种默认动画)
  • 添加头部、尾部、下拉刷新、上拉加载(感觉又回到ListView时代)
  • 设置自定义的加载更多布局
  • 添加分组(随心定义分组头部)
  • 自定义不同的item类型(简单配置、无需重写额外方法)
  • 设置空布局(比Listview的setEmptyView还要好用!)
1、可以先到http://www.open-open.com/news/tags/Android下下载demo,这里只用到下拉刷新和上拉加载就好,
首先将用到的library导入工程
简单好用的上拉加载下拉刷新 BaseRecyclerViewAdapterHelper_第1张图片

2、QuickAdapter 代码
package com.example.apple.pulltorefresh.adapter;

import android.content.Context;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.apple.pulltorefresh.R;
import com.example.apple.pulltorefresh.data.DataServer;
import com.example.apple.pulltorefresh.entity.Status;

/**
 * https://github.com/CymChad/BaseRecyclerViewAdapterHelper
 */
public class QuickAdapter extends BaseQuickAdapter {
//    public QuickAdapter(Context context) {
//        super(context, R.layout.tweet, DataServer.getSampleData(100));
//    }

    public QuickAdapter(Context context, int dataSize) {
        super(context, R.layout.tweet, DataServer.getSampleData(dataSize));
    }

    @Override
    protected void convert(BaseViewHolder helper, Status item) {
        helper.setText(R.id.tweetName, item.getUserName())
                .setText(R.id.tweetText, item.getText())
                .setText(R.id.tweetDate, item.getCreatedAt())
                .setImageUrl(R.id.tweetAvatar, item.getUserAvatar())
                .setVisible(R.id.tweetRT, item.isRetweet())
                .linkify(R.id.tweetText);
    }
}

3、主界面代码
package com.example.apple.pulltorefresh;

import android.os.Handler;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.apple.pulltorefresh.adapter.QuickAdapter;
import com.example.apple.pulltorefresh.data.DataServer;

public class MainActivity extends AppCompatActivity implements BaseQuickAdapter.RequestLoadMoreListener,SwipeRefreshLayout.OnRefreshListener{

    private RecyclerView mRecyclerView;
    private QuickAdapter mQuickAdapter;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    private static final int TOTAL_COUNTER = 18;
    private static final int PAGE_SIZE = 6;
    private int mCurrentCounter = 0;

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

        initView();

    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        initAdapter();
       // addHeadView();
        mRecyclerView.setAdapter(mQuickAdapter);
    }

    /**
     * 添加headView
     */
    private void addHeadView() {
        View headView = getLayoutInflater().inflate(R.layout.head_view, null);
        headView.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
       //headView添加监听
        headView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "click HeadView", Toast.LENGTH_LONG).show();
            }
        });
        mQuickAdapter.addHeaderView(headView);
    }

    /**
     * 刷新listView
     */
    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                initAdapter();
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }, 1000);
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMoreRequested() {
        if (mCurrentCounter >= TOTAL_COUNTER) {
            mRecyclerView.post(new Runnable() {
                @Override
                public void run() {
                    mQuickAdapter.isNextLoad(false);
                }
            });

        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    DataServer.addData(mQuickAdapter.getData(), PAGE_SIZE);
                    mCurrentCounter = mQuickAdapter.getItemCount();
                    mQuickAdapter.isNextLoad(true);

                }
            }, 1000);//一秒刷新
        }
    }

    /**
     * 初始化adapter
     */
    private void initAdapter() {
        mQuickAdapter = new QuickAdapter(MainActivity.this, PAGE_SIZE);
        mQuickAdapter.openLoadAnimation();
        mRecyclerView.setAdapter(mQuickAdapter);
        mCurrentCounter = mQuickAdapter.getItemCount();
        mQuickAdapter.setOnLoadMoreListener(PAGE_SIZE, this);
      //  addHeadView();
        //item添加监听
        mQuickAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MainActivity.this, Integer.toString(position), Toast.LENGTH_LONG).show();
            }
        });
    }

}

4、导入library记得在build.gradle中添加
compile project(path: ':library')

剩下的几个文件是数据,详细可下载代码自己看: http://download.csdn.net/detail/u011324501/9722970
简单好用的上拉加载下拉刷新 BaseRecyclerViewAdapterHelper_第2张图片

你可能感兴趣的:(android,Adapter,下拉刷新上拉加载,recyclerVie)