RecyclerView + 上拉加更多 + 下拉刷新

源码:点击打开链接

一.基类适配器

package refresh.recycview.control.control;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

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

import data.DataType;
import refresh.recycview.control.data.LoadStatus;
import refresh.recycview.control.pubinterface.PubDataType;
import refresh.recycview.control.view.BaseViewHolder;
import refresh.recycview.control.view.ItemView;
import refresh.recycview.control.view.LoadMoreView;
import view.ReceiveView;
import view.SendView;
import view.ViewHolder;

/**
 * 基类适配器,这个例子与前面那个的区别再于加入了下拉刷新,上拉加载更多的模板
 * 因为RecyclerView作为一个容器在加载不同的数据里可能都需要“下拉刷新,上拉加载更多”
 * 所以把这个功能封装在了基类适配器中,同时抛出不同的接口用来处理那些不需要这个功能
 * 的适配器
 * Created by Administrator on 2016/11/26.
 */
public class RecycViewBaseAdapter extends RecyclerView.Adapter {

    private LoadStatus mLoadStatus = LoadStatus.CLICK_LOAD_MORE;        //上拉加载的状态
    protected Context context;
    private List dataList;                                 //公用的数据集合
    private static final int VIEW_TYPE_FOOTER = 0;                      //底部类型

    private boolean openPullDownRefresh;
    private boolean openPullUpLoadMore;

    private LoadMoreView boottom;

    public boolean isOpenPullUpLoadMore() {
        return openPullUpLoadMore;
    }

    public void setOpenPullUpLoadMore(boolean openPullUpLoadMore) {
        this.openPullUpLoadMore = openPullUpLoadMore;
    }

    public boolean isOpenPullDownRefresh() {
        return openPullDownRefresh;
    }

    public void setOpenPullDownRefresh(boolean openPullDownRefresh) {
        this.openPullDownRefresh = openPullDownRefresh;
    }
    public RecycViewBaseAdapter(Context context) {
        this.context = context;
    }
    public void setLoadStatus(LoadStatus loadStatus) {
        this.mLoadStatus = loadStatus;
    }

    public void addData(List append) {
        getDataList().addAll(append);

    }

    public void clean() {
        if(getDataList().size() > 0)
        {
            getDataList().clear();
        }
    }

    /**
     * 子类适配器自己去重写,必须
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == VIEW_TYPE_FOOTER) {
            boottom = new LoadMoreView(context,parent);
            return boottom;

        } else if (viewType == 23) {
            return new ItemView(context,parent);
        }

        return null;
    }

    /**
     * 适配器中将数据绑定到view,子类需要重写setdata方法
     * 注意,如果子类适配器重写了父类的addData最好连下面这个方法也一同重写
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        holder.setData(getDataList().get(position));

        //数据加载完将加载更多隐藏,处理进度条的情况
        if(getItemViewType(position) == VIEW_TYPE_FOOTER)
        {
            switch (mLoadStatus) {
                case CLICK_LOAD_MORE:
                    boottom.mLoadingLayout.setVisibility(View.GONE);
                    boottom.mClickLoad.setVisibility(View.VISIBLE);
                    break;
                case LOADING_MORE:
                    boottom.mLoadingLayout.setVisibility(View.VISIBLE);
                    boottom.mClickLoad.setVisibility(View.GONE);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return getDataList().size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {   //最后一条为FooterView
            return VIEW_TYPE_FOOTER;
        }
        return getDataList().get(position).getItemType();
    }

    public List getDataList() {
        if (dataList == null) {
            dataList = new ArrayList<>();
        }
        return dataList;
    }
}


二基类视图制器:

package refresh.recycview.control.view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * 基类视图控制器,这里再一次扩展RecyclerView.ViewHolder主要是与前面那个ViewHolder做区分
 * Created by Administrator on 2016/11/26.
 */
public abstract class BaseViewHolder extends RecyclerView.ViewHolder
{

    protected Context mContext;

    public BaseViewHolder(Context context, ViewGroup parent, int layoutId) {
        super(LayoutInflater.from(context).inflate(layoutId, parent, false));
        mContext = context;
        findView(itemView);
    }

    public BaseViewHolder(View itemView) {
        super(itemView);
    }

    public abstract void setData(T data);   //由父类的的onBindViewHolder数据调用

    public abstract void findView(View itemView);
}


三数据类型接口:

package refresh.recycview.control.pubinterface;

/**
 * 与前面那个DateType功能无异,主要区分例子
 * Created by Administrator on 2016/11/26.
 */
public interface PubDataType {

    int getItemType();
}


三.item数据

package refresh.recycview.control.data;

import refresh.recycview.control.pubinterface.PubDataType;

/**
 * Created by Administrator on 2016/11/26.
 */
public class ItemData implements PubDataType {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int getItemType() {
        return 23;
    }
}
四:加载的状态

package refresh.recycview.control.data;

/**
 * 加载状态
 * Created by Administrator on 2016/11/26.
 */
public enum LoadStatus {

    CLICK_LOAD_MORE,        //上拉加载更多
    LOADING_MORE            //正在加载更多
}

五:ItemView视图

package refresh.recycview.control.view;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.recyclerviewdemo2.R;

/**
 * Created by Administrator on 2016/11/26.
 */
public class ItemViewHolder extends  RecyclerView.ViewHolder{

    public TextView mTextView;
    public ItemViewHolder(View itemView) {
        super(itemView);
        mTextView = (TextView) itemView.findViewById(R.id.textView);
    }
}

六加载更多视图:

package refresh.recycview.control.view;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.recyclerviewdemo2.R;

import refresh.recycview.control.pubinterface.PubDataType;

/**
 * 底部加载更多视图
 * 如果不想动态更新底部文本,下面这个类的方式也可这样写:
 * public class LoadMoreView extends baseViewHolder
 *
 * Created by Administrator on 2016/11/26.
 */
public class LoadMoreView extends BaseViewHolder
{
    public LinearLayout mLoadingLayout;
    public TextView mClickLoad;


    public LoadMoreView(Context context, ViewGroup parent) {
        super(context, parent, R.layout.footer_layout);
    }

    @Override
    public void setData(PubDataType data) {
    }

    @Override
    public void findView(View itemView) {
        mLoadingLayout=(LinearLayout) itemView.findViewById(R.id.loading);
        mClickLoad=(TextView) itemView.findViewById(R.id.click_load_txt);
    }
}

activity:

package mainActivity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.administrator.recyclerviewdemo2.R;

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

import refresh.recycview.control.control.MyAdapter;
import refresh.recycview.control.control.RecycViewBaseAdapter;
import refresh.recycview.control.data.ItemData;
import refresh.recycview.control.data.LoadStatus;

/**
 * Created by Administrator on 2016/11/26.
 */
public class RefreshViewActivity extends Activity {

    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private MyAdapter mAdapter;
    private int mLastVisibleItemPosition = 0;
    private LinearLayoutManager mLayoutManager;

    private RecycViewBaseAdapter adapter;   //暂时用基类适配器来测试

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

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        //适配器1
        mAdapter = new MyAdapter(this, getData("init"));
        // mRecyclerView.setAdapter(mAdapter); //测试一

        //测试二
        adapter = new RecycViewBaseAdapter(this);
        adapter.addData(loadMore2());   //添加itemdata

        mRecyclerView.setAdapter(adapter);

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(3000);
                            final List list = getData("refresh");
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mAdapter.reSetData(list);
                                    mSwipeRefreshLayout.setRefreshing(false);
                                }
                            });
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override//滚动状态变化时回调
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                super.onScrollStateChanged(recyclerView, newState);
                // mLastVisibleItemPosition=mLayoutManager.findLastVisibleItemPosition();
                //滑动停止并且底部不可滚动(即滑动到底部) 加载更多
                if (newState == RecyclerView.SCROLL_STATE_IDLE && !(ViewCompat.canScrollVertically(recyclerView, 1))
                        ) {
                    //loadMore(); //测试一

                    adapter.setLoadStatus(LoadStatus.LOADING_MORE);
                    adapter.notifyDataSetChanged();
                    new Thread() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(3000);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        adapter.addData(loadMore2());   //添加itemdata
                                        adapter.notifyDataSetChanged();
                                        adapter.setLoadStatus(LoadStatus.CLICK_LOAD_MORE);
                                    }
                                });
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            ;
                        }
                    }.start();


                }
            }

            @Override//滚动时回调
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

            }
        });
    }

    //测试一的方法
    public void loadMore() {
        mAdapter.setLoadStatus(LoadStatus.LOADING_MORE);
        mAdapter.refresh();
        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    final List list = getData("load more");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.addAll(list);

                            mAdapter.setLoadStatus(LoadStatus.CLICK_LOAD_MORE);
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ;
            }
        }.start();
    }

    public List getData(String flag) {
        int idx = 1;
        if (mAdapter != null) {
            idx = mAdapter.getItemCount();
        }
        List list = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            list.add(flag + ":" + (idx + i));
        }
        return list;
    }
    //测试一的方法完毕

    //测试二的方法

    private List loadMore2() {
        List list = new ArrayList<>(10);
        for (int i = 0; i < 5; i++) {
            ItemData data = new ItemData();
            data.setName("A" + i);
            list.add(data);
        }
        return list;
    }
}


布局文件:

xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
     >

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        android.support.v7.widget.RecyclerView>

    android.support.v4.widget.SwipeRefreshLayout>
RelativeLayout>






你可能感兴趣的:(android)