MaterialRefreshLayout 实现刷新加载更多

2017-05-09  本文已影响0人  top_coder

github  地址:https://github.com/android-cjj/Android-MaterialRefreshLayout

使用:

1.添加依赖

dependencies {

      compile 'com.cjj.materialrefeshlayout:library:1.3.0'

}

2.在xml 

代码:

布局:

activity:

packagecom.cmcc.recycleviewtest;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.support.v7.app.AppCompatActivity;

importandroid.support.v7.widget.LinearLayoutManager;

importandroid.support.v7.widget.RecyclerView;

importandroid.util.Log;

importandroid.view.View;

importandroid.widget.Toast;

importcom.cjj.MaterialRefreshLayout;

importcom.cjj.MaterialRefreshListener;

importjava.util.ArrayList;

importjava.util.List;

public classMainActivityextendsAppCompatActivityimplementsMyAdapter.OnItemlickListenner {

RecyclerViewmRecyclerView;

Listdata;

MaterialRefreshLayoutmRefreshLayout;

private static final intSTATE_NORMAL=0;

private static final intSTATE_REFRESH=1;

private static final intSTATE_MOREE=3;

private intstate=STATE_NORMAL;

privateMyAdaptermyAdapter;

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

getData(30);

initRecycleView();

initRefresh();

}

private voidinitRefresh() {

mRefreshLayout= (MaterialRefreshLayout) findViewById(R.id.refresh);

mRefreshLayout.setMaterialRefreshListener(newMaterialRefreshListener() {

@Override

public voidonRefresh(MaterialRefreshLayout materialRefreshLayout) {

//下拉刷新

newHandler().postDelayed(newRunnable() {

@Override

public voidrun() {

getData(100);

myAdapter.clearData();

myAdapter.addData(data);

mRecyclerView.scrollToPosition(0);

mRefreshLayout.finishRefresh();

}

},3000);

}

@Override

public voidonRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {

//加载更多

newHandler().postDelayed(newRunnable() {

@Override

public voidrun() {

getData(200);

myAdapter.addData(myAdapter.getData().size(),data);

mRecyclerView.scrollToPosition(myAdapter.getData().size());

mRefreshLayout.finishRefreshLoadMore();

}

},3000);

}

});

}

private voidinitRecycleView() {

mRecyclerView= (RecyclerView) findViewById(R.id.recycle_view);

mRecyclerView.setLayoutManager(newLinearLayoutManager(this));

myAdapter=newMyAdapter(this,data);

Log.i("MainActivity",data.size()+"");

mRecyclerView.setAdapter(myAdapter);

myAdapter.setOnitemClickListenner(this);

}

@Override

public voidonClick(View view,intposition, String data) {

Toast.makeText(MainActivity.this,"点击了第:"+position,Toast.LENGTH_SHORT).show();

}

private voidgetData(inta) {

data=newArrayList<>();

for(inti=0;i

data.add("第"+i+"数据");

}

}

}

adapter:

packagecom.cmcc.recycleviewtest;

importandroid.content.Context;

importandroid.support.v7.widget.RecyclerView;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.widget.TextView;

importjava.util.List;

/**

* Created by PC on 2017/5/5.

*/

public classMyAdapterextendsRecyclerView.Adapter {

privateContextmContext;

privateListmdata;

OnItemlickListennerlistenner;//声明接口

//给接口设置set方法

public voidsetOnitemClickListenner(OnItemlickListenner listenner){

this.listenner=listenner;

}

publicMyAdapter(Context context , List data) {

this.mContext=context;

this.mdata=data;

}

public voidclearData(){

mdata.clear();

notifyItemRangeChanged(0,mdata.size());

}

public voidaddData(List datas){

addData(0,datas);

}

public voidaddData(intposition,List datas){

if(datas!=null&&datas.size()>0){

mdata.addAll(datas);

notifyItemRangeChanged(position,mdata.size());

}

}

@Override

publicMyAdapter.VieHolder onCreateViewHolder(ViewGroup parent,intviewType) {

VieHolder holder =newVieHolder(LayoutInflater.from(mContext).inflate(R.layout.item_view, parent,false));

returnholder;

}

@Override

public voidonBindViewHolder(MyAdapter.VieHolder holder,intposition) {

holder.tv.setText(mdata.get(position));

}

@Override

public intgetItemCount() {

returnmdata.size();

}

publicList getData() {

returnmdata;

}

public classVieHolderextendsRecyclerView.ViewHolder {

TextViewtv;

publicVieHolder(View itemView) {

super(itemView);

tv= (TextView) itemView.findViewById(R.id.tv_item);

//用这个接口,并进行接口回调

itemView.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View view) {

if(listenner!=null){

listenner.onClick(view,getLayoutPosition(),mdata.get(getAdapterPosition()));

}

}

});

}

}

//1.定义一个接口

interfaceOnItemlickListenner{

voidonClick(View view,intposition,String data);

}

}

上一篇下一篇

猜你喜欢

热点阅读