Retrofit+RecyclerView+SwipeRefreshlayout实现上拉刷新和分页加载
2017-08-21 11:49
603 查看
使用Retrofit+RecyclerView+Swiperefreshlayout实现分页加载数据,包括上拉刷新和下拉加载更多。
demo中的api参考地址
http://gank.io/api/data/福利/5/1
用到的知识点:
Retrofit实现网络请求
Recyclerview列表
Swiperefreshlayout实现下拉刷新,上拉加载更多
加载图片用Glide
项目目录结构
运行效果图
2
3
1
2
3
2
3
4
5
6
1
2
3
4
5
6
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Bean类
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Bean代码
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
代码as下载地址
http://download.csdn.net/detail/androidxiaogang/9688216
demo中的api参考地址
http://gank.io/api/data/福利/5/1
用到的知识点:
Retrofit实现网络请求
Recyclerview列表
Swiperefreshlayout实现下拉刷新,上拉加载更多
加载图片用Glide
项目目录结构
运行效果图
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'1
2
3
1
2
3
2、retorfit的API配置
public interface Api { //http://gank.io/api/data/福利/5/1 @GET("api/data/福利/{pageCount}/{pageIndex}") Call<DataInfo> getData(@Path("pageCount") int pageCount, @Path("pageIndex") int pageIndex); }1
2
3
4
5
6
1
2
3
4
5
6
3、google自带的Swiperefreshlayout只有下拉刷新,通过对代码配置,加入下拉加载更多
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="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"> <android.support.v7.widget.RecyclerView android:id="@+id/recyerview" android:layout_width="match_parent" android:layout_height="match_parent" /> </com.example.administrator.retrofit.pull.SwipyRefreshLayout> </LinearLayout>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
4、请求返回
{ "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" } ] }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Bean类
public class DataInfo { public String error; public ArrayList<Info> results; public class Info{ public String _id; public String url; } }1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
4、activity中的代码
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<DataInfo.Info> 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<DataInfo> call=api.getData(5,pages); call.enqueue(new Callback<DataInfo>() { @Override public void onResponse(Call<DataInfo> call, Response<DataInfo> response) { arrayList.addAll(response.body().results); adapter.notifyDataSetChanged(); Log.i("aaaa", arrayList.size() + ""); refreshLayout.setRefreshing(false); } @Override public void onFailure(Call<DataInfo> 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(); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
5、在adapter中的代码
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<DataInfo.Info> list; public MyAdapter(Context context, ArrayList<DataInfo.Info> 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(); // } // }); } } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Bean代码
public class DataInfo { public String error; public ArrayList<Info> results; public class Info{ public String _id; public String url; } }1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
代码as下载地址
http://download.csdn.net/detail/androidxiaogang/9688216
相关文章推荐
- Retrofit+RecyclerView+SwipeRefreshlayout实现上拉刷新和分页加载
- Retrofit+SwipeRefreshlayout+RecyclerView实现上拉刷新和分页加载
- Retrofit+RecyclerView+SwipeRefreshlayout实现上拉刷新和分页加载
- SwipeRefreshLoadLayout + RecyclerView 实现下列刷新,上拉加载
- RecyclerView+SwipeRefreshLayout实 4000 现上拉刷新,下拉加载更多的功能
- SwipeRefreshLayout、RecyclerView、BaseRecyclerViewAdapterHelper 三者组合的刷新加载View
- Google自己的下拉刷新组件 SwipeRefreshLayout + RecyclerView 实现 上拉刷新 和 下拉刷新
- SwipeRefreshLayout + RecyclerView 实现 上拉刷新 和 下拉刷新
- SwipeRefreshLayout + RecyclerView 实现 上拉刷新 和 下拉刷新
- SwipeRefreshLayout+RecyclerView实现下拉刷新上拉加载功能
- SwipeRefreshLayout+RecyclerView实现下拉刷新上拉自动加载
- RecyclerView与SwipReferenceLayout实现下拉刷新与上拉分页加载
- SwipeRefreshLayout + RecyclerView 实现 上拉刷新 和 下拉刷新
- RecyclerView+SwipeRefreshLayout实现下拉刷新列表
- SwipeRefreshLayout和RecyclerView实现下拉刷新和上拉加载
- SwipeRefreshLayout + RecyclerView实现上拉加载和下拉刷新
- SwipeRefreshLayout + RecyclerView 实现 上拉刷新 和 下拉刷新
- 4.5.2 SwipeRefreshLayout + RecyclerView 实现 上拉刷新 和 下拉刷新
- 简单实现WaveSwipeRefreshLayout+RecyclerView的上拉加载下拉刷新的功能
- SwipeRefreshLayout + RecyclerView 实现 上拉刷新 和 下拉刷新