使用Retrofit+RecyclerView+Swiperefreshlayout实现分页加载数据,包括上拉刷新和下拉加载更多。
demo中的api参考地址
http://gank.io/api/data/福利/5/1
用到的知识点:
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
public interface Api {
//http://gank.io/api/data/福利/5/1
@GET("api/data/福利/{pageCount}/{pageIndex}")
Call getData(@Path("pageCount") int pageCount,
@Path("pageIndex") int pageIndex);
}
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
xmlns:my="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.administrator.retrofit.MainActivity">
<com.example.administrator.retrofit.pull.SwipyRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
my:direction="both">
.support.v7.widget.RecyclerView
android:id="@+id/recyerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
com.example.administrator.retrofit.pull.SwipyRefreshLayout>
{
"error": false,
"results": [
{
"_id": "5817e1fa421aa913769745fe",
"createdAt": "2016-11-01T08:29:46.640Z",
"desc": "11-1",
"publishedAt": "2016-11-01T11:46:01.617Z",
"source": "chrome",
"type": "\u798f\u5229",
"url": "http://ww1.sinaimg.cn/large/610dc034jw1f9cayjaa96j20u011hqbs.jpg",
"used": true,
"who": "daimajia"
},
{
"_id": "5816871a421aa91369f959b6",
"createdAt": "2016-10-31T07:49:46.592Z",
"desc": "10-31",
"publishedAt": "2016-10-31T11:43:44.770Z",
"source": "chrome",
"type": "\u798f\u5229",
"url": "http://ww2.sinaimg.cn/large/610dc034jw1f9b46kpoeoj20ku0kuwhc.jpg",
"used": true,
"who": "daimajia"
},
{
"_id": "581218e9421aa90e799ec222",
"createdAt": "2016-10-27T23:10:33.618Z",
"desc": "10-28",
"publishedAt": "2016-10-28T11:29:36.510Z",
"source": "chrome",
"type": "\u798f\u5229",
"url": "http://ww2.sinaimg.cn/large/610dc034jw1f978bh1cerj20u00u0767.jpg",
"used": true,
"who": "daimajia"
},
{
"_id": "5811596a421aa90e6f21b45e",
"createdAt": "2016-10-27T09:33:30.47Z",
"desc": "10-27",
"publishedAt": "2016-10-27T11:41:45.88Z",
"source": "chrome",
"type": "\u798f\u5229",
"url": "http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg",
"used": true,
"who": "daimajia"
},
{
"_id": "58101f83421aa90e6f21b44b",
"createdAt": "2016-10-26T11:14:11.143Z",
"desc": "10-26",
"publishedAt": "2016-10-26T11:28:10.759Z",
"source": "chrome",
"type": "\u798f\u5229",
"url": "http://ww4.sinaimg.cn/large/610dc034jw1f95hzq3p4rj20u011htbm.jpg",
"used": true,
"who": "daimajia"
}
]
}
Bean类
public class DataInfo {
public String error;
public ArrayList results;
public class Info{
public String _id;
public String url;
}
}
package com.example.administrator.retrofit;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.example.administrator.retrofit.pull.SwipyRefreshLayout;
import java.util.ArrayList;
import api.Api;
import bean.DataInfo;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import static com.example.administrator.retrofit.R.id.recyerview;
public class MainActivity extends AppCompatActivity implements MyAdapter.OnRecyclerViewItemClickListener,SwipyRefreshLayout.OnRefreshListener {
private ArrayList arrayList;
private RecyclerView recyclerView;
private MyAdapter adapter;
private SwipyRefreshLayout refreshLayout;
private LinearLayoutManager linearLayoutManager;
private int pages=1;
private final int TOP_REFRESH = 1;
private final int BOTTOM_REFRESH = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView= (RecyclerView) findViewById(recyerview);
refreshLayout= (SwipyRefreshLayout) findViewById(R.id.refreshLayout);
refreshLayout.setOnRefreshListener(this);
arrayList=new ArrayList();
initData(1);
linearLayoutManager=new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
adapter=new MyAdapter(this,arrayList);
adapter.setOnItemClickListener(this);
recyclerView.setAdapter(adapter);
}
private void initData(int pages) {
//使用retrofit配置api
Retrofit retrofit=new Retrofit.Builder()
.baseUrl("http://gank.io/")
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api =retrofit.create(Api.class);
Call call=api.getData(5,pages);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
arrayList.addAll(response.body().results);
adapter.notifyDataSetChanged();
Log.i("aaaa", arrayList.size() + "");
refreshLayout.setRefreshing(false);
}
@Override
public void onFailure(Call call, Throwable t) {
refreshLayout.setRefreshing(false);
}
});
}
@Override
public void onItemClick(View view, DataInfo.Info data) {
Toast.makeText(MainActivity.this, "click item " + data, Toast.LENGTH_SHORT).show();
}
@Override
public void onRefresh(int index) {
dataOption(TOP_REFRESH);
Toast.makeText(this,"已经是最新数据",Toast.LENGTH_SHORT).show();
}
@Override
public void onLoad(int index) {
dataOption(BOTTOM_REFRESH);
Toast.makeText(this,"加载完成",Toast.LENGTH_SHORT).show();
}
private void dataOption(int option){
switch (option) {
case TOP_REFRESH:
//下拉刷新
arrayList.clear();
initData(1);
break;
case BOTTOM_REFRESH:
//上拉加载更多
pages++;
initData(pages);
break;
}
// adapter.notifyDataSetChanged();
}
}
package com.example.administrator.retrofit;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import bean.DataInfo;
/**
* Created by Administrator on 2016/6/25.
*/
public class MyAdapter extends RecyclerView.Adapter implements View.OnClickListener {
private Context context;
private ArrayList list;
public MyAdapter(Context context, ArrayList list) {
this.context = context;
this.list = list;
}
private OnRecyclerViewItemClickListener mOnItemClickListener = null;
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
//注意这里使用getTag方法获取数据
mOnItemClickListener.onItemClick(v, (DataInfo.Info) v.getTag());
}
}
public interface OnRecyclerViewItemClickListener {
void onItemClick(View view , DataInfo.Info data);
}
public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
this.mOnItemClickListener = listener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item, null);
ViewHodler vh=new ViewHodler(view);
LinearLayout.LayoutParams lp = new LinearLayout.
LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
view.setOnClickListener(this);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ViewHodler mHodler = (ViewHodler) holder;
mHodler.textview.setText("this is item "+position );
Glide.with(context)
.load(list.get(position).url)
.centerCrop()
.placeholder(R.color.app_primary_color)
.crossFade()
.into(mHodler.imageView);
Log.i("aaaa", list.get(position)._id);
mHodler.itemView.setTag(list.get(position));
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHodler extends RecyclerView.ViewHolder{
private TextView textview;
private ImageView imageView;
public ViewHodler(View itemView) {
super(itemView);
textview= (TextView) itemView.findViewById(R.id.textview);
imageView= (ImageView) itemView.findViewById(R.id.image);
// itemView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Toast.makeText(context,"click"+getPosition(),Toast.LENGTH_SHORT).show();
// }
// });
}
}
}
Bean代码
public class DataInfo {
public String error;
public ArrayList results;
public class Info{
public String _id;
public String url;
}
}
代码as下载地址
http://download.csdn.net/detail/androidxiaogang/9688216