进击的RecyclerView入门一(简单上手)

虽然RecyclerView面世有一段时间了,但由于它的学习成本相对较高,很多码友只是粗略的认识了一下而没有细致的品味RecyclerView的真谛。

那么从现在开始我将带你装逼带你飞,一起领略Google工程师们赋予RecyclerView独特的魅力。

首先我们看一下官方文档是怎么介绍RecyclerView的简介:

A flexible view for providing a limited window into a large data set.

Google的工程师写文档还真够精辟的,写的什么鬼?这不等于没写吗?OK,别激动,我们慢慢来。先看看怎么样把RecyclerView跑起来?

接下来简单写一个使用RecyclerView加载网络图片的demo。

布局文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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"
tools:context="com.zqlite.android.recyclerviewadvance.MainActivity">


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

</android.support.v7.widget.RecyclerView>
</RelativeLayout>

Activity文件如下:

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.zqlite.android.recyclerviewadvance;

import android.media.Image;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.squareup.picasso.Picasso;

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

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;

private Handler handler = new Handler();

public static final int ADD_DATA_DURATION = 2000;

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

private void initView(){
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

final RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,3);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
recyclerView.setItemAnimator(itemAnimator);

final MyAdapter adapter = new MyAdapter();
recyclerView.setAdapter(adapter);

handler.postDelayed(new Runnable() {
@Override
public void run() {
adapter.addData();
handler.postDelayed(this, ADD_DATA_DURATION);
layoutManager.scrollToPosition(adapter.getSize());
}
},ADD_DATA_DURATION);
}

private class MyAdapter extends RecyclerView.Adapter{

private final String[] imageUrlArray = {
"http://o6p4e1uhv.bkt.clouddn.com/tu24967_22.jpg",
"http://o6p4e1uhv.bkt.clouddn.com/cce4b48f8c5494ee7008eb9528f5e0fe98257eca.jpg",
"http://o6p4e1uhv.bkt.clouddn.com/tu25422_4.jpg",
"http://o6p4e1uhv.bkt.clouddn.com/ad7fd688d43f879424026deed01b0ef41ad53a2e.jpg",
"http://o6p4e1uhv.bkt.clouddn.com/tu25422_6.jpg",
"http://o6p4e1uhv.bkt.clouddn.com/refsdg.jpg",
"http://o6p4e1uhv.bkt.clouddn.com/ggfnvbn.jpg",
"http://o6p4e1uhv.bkt.clouddn.com/sghgfsh.jpg",
"http://o6p4e1uhv.bkt.clouddn.com/asfasdf.jpg",
"http://o6p4e1uhv.bkt.clouddn.com/fsbgbsfg.jpg",
"http://o6p4e1uhv.bkt.clouddn.com/gsfdtrysrtsfdg.jpg",
"http://o6p4e1uhv.bkt.clouddn.com/sdfgsrters.jpg"};

private List<String> datas = new ArrayList<>();

public MyAdapter(){
}

public void addData(){
Random random = new Random();
datas.add(imageUrlArray[random.nextInt(imageUrlArray.length)]);
notifyItemInserted(datas.size());
}

public int getSize(){
return datas.size();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

RecyclerView.LayoutParams reLP = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,RecyclerView.LayoutParams.WRAP_CONTENT);
reLP.setMargins(20, 20, 20, 20);
CardView cardView = new CardView(parent.getContext());
cardView.setLayoutParams(reLP);


CardView.LayoutParams cLP = new CardView.LayoutParams(parent.getMeasuredWidth()/3,(int)(parent.getMeasuredWidth()/3*1.3));
ImageView imageView = new ImageView(parent.getContext());
imageView.setLayoutParams(cLP);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
cardView.addView(imageView);

MyHolder holder = new MyHolder(cardView);

return holder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyHolder myHolder = (MyHolder)holder;
ImageView imageView = myHolder.imageView;
Picasso.with(imageView.getContext()).load(datas.get(position)).into(imageView);
}

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

private class MyHolder extends RecyclerView.ViewHolder{

public ImageView imageView ;
public MyHolder(View itemView) {
super(itemView);
imageView = (ImageView) ((CardView)itemView).getChildAt(0);
}
}
}
}

使用RecyclerView以下几个东西是必备的:

  1. RecyclerView.LayoutManager
  2. RecyclerView.Adapter
    其中LayoutManager负责布局,Adapter负责数据集。
    该demo运行结果如下:

demo完整源码地址:
https://github.com/ZhangQinglian/RecyclerViewAdvance

如文章对您有所帮助,可以请作者喝杯 🍵