-
仅作为自己在使用recyclerview过程中一点点记录,
- 使用Recyclerview已经有一定时间,项目中也用了很久,总觉得该亲自整理一下,也算是自己回顾一下Recyclerview的知识,然后尝试去深层次的了解Recyclerview,只会用,不了解,也不算是会用;
- 如果你想尝试Recyclerview,正好又看到这个文章,嗯哼0.0,那你就好好看看吧。
- 废话不多说,直接开整
首先当然是添加依赖,根据当前sdk版本,选择合适的依赖,Recyclerview是V7包提供的,所以呢?
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
第一个不必说,第二个就是我们需要的导入的包了,rebuild一下,看看是否导入成功,只要根据自己使用的sdk版本,一般是没有意外的,如果有了
很明显是你导入错了。
-
导入成功,接下来就是布局里使用了
-
布局很简单,SwipeRefreshLayout包裹Recyclerview,实现下拉刷新
回到代码中
public class MainActivity extends AppCompatActivity {
@InjectView(R.id.recyclerview)
RecyclerView recyclerview;
@InjectView(R.id.swipeRefresh)
SwipeRefreshLayout swipeRefresh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
initData();
}
}
Recyclerview和SwipeRefreshLayout初始化完成,开始加载数据实现Recyclerview的多种实现布局;
![先请求到数据.png](http://upload-images.jianshu.io/upload_images/2789715-d9ac349d9a7c5c9e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
有了数据之后,和ListView一样,需要设置适配器,但是Recyclerview得适配器和ListView适配器,不完全一样;假设Recyclerview适配器定义为Adapter
public class Adapter extends RecyclerView.Adapter {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
}
我们真正要实现的是重写一个ViewHolder,继承 Recyclerview.ViewHolder;
public class ItemHolder extends RecyclerView.ViewHolder {
public ItemHolder(View itemView) {
super(itemView);
}
}
设置Adapter的布局
android:layout_height="60dp"
android:paddingLeft="10dp"
android:paddingTop="10dp">
回到Adapter
public class Adapter extends RecyclerView.Adapter
private Context context;
private ArrayList datas;
public Adapter(Context context, ArrayList datas) {
this.context = context;
this.datas = datas;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ItemHolder(LayoutInflater.from(context).inflate(R.layout.item_adapter, null));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return datas.size() == 0 ? 0 : datas.size();
}
public class ItemHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ItemHolder(View itemView) {
super(itemView);
this.imageView = (ImageView) itemView.findViewById(R.id.imageView);
this.textView = (TextView) itemView.findViewById(R.id.textView);
}
}
}
最后数据绑定
/**
- Creation time : 2016/11/22 21:01
- Fix time : 2016/11/22 21:01
*/
public class Adapter extends RecyclerView.Adapter
private Context context;
private List datas;
public Adapter(Context context, List datas) {
this.context = context;
this.datas = datas;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ItemHolder(LayoutInflater.from(context).inflate(R.layout.item_adapter, null));
}
//绑定数据
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
bindViewHolder((ItemHolder) holder);
}
private void bindViewHolder(ItemHolder holder) {
for (int i = 0; i < this.datas.size() - 1; i++) {
holder.textView.setText(datas.get(i));
holder.imageView.setBackgroundResource(R.drawable.musiclogo144);
}
}
@Override
public int getItemCount() {
return datas.size() == 0 ? 0 : datas.size();
}
public class ItemHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ItemHolder(View itemView) {
super(itemView);
this.imageView = (ImageView) itemView.findViewById(R.id.imageView);
this.textView = (TextView) itemView.findViewById(R.id.textView);
}
}
}
回到加载数据部分
/**
* 请求数据
*/
private void initData() {
//假设联网请求数据N条,这里我就用本地数据
data = null;
for (int i = 0; i < 40; i++) {
data.add(i, i + ". 成功请求数据");
}
adapter = new Adapter(MainActivity.this, data);
recyclerview.setAdapter(adapter);
}
编译运行
但是什么都没有,因为我们没有设置LayoutManager,Recyclerview需要设置LayoutManager,才能显示;
public class MainActivity extends AppCompatActivity {
@InjectView(R.id.recyclerview)
RecyclerView recyclerview;
@InjectView(R.id.swipeRefresh)
SwipeRefreshLayout swipeRefresh;
private static final int GET = 1;
private LinearLayoutManager linearLayoutManager;
private GridLayoutManager gridLayoutManager;
private StaggeredGridLayoutManager staggeredGridLayoutManager;
private List data;
private RecyclerView.LayoutManager layoutmanager;
private Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
linearLayoutManager = new LinearLayoutManager(MainActivity.this); //常见纵向布局
// linearLayoutManager = new LinearLayoutManager(MainActivity.this,LinearLayoutManager.HORIZONTAL,true); //水平方向,最后一个参数是否逆向
gridLayoutManager = new GridLayoutManager(MainActivity.this, 2); //类似GridView的格式,第二个参数为行的item数量,同理,也有水平方向的GridLayoutManager;
staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); //瀑布流,第一个为每行item数量
layoutmanager = linearLayoutManager; //默认为纵向的Recyclerview;
initData();
}
@Override
public boolean onCreateOptionsMenu(Menu item) {
getMenuInflater().inflate(R.menu.main_menu, item);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.linearlayout:
layoutmanager = linearLayoutManager;
initData();
break;
case R.id.gridlayout:
layoutmanager = gridLayoutManager;
break;
case R.id.staggeredgridlayout:
layoutmanager = staggeredGridLayoutManager;
initData();
break;
default:
layoutmanager = linearLayoutManager;
initData();
break;
}
return super.onOptionsItemSelected(item);
}
/**
* 请求数据
*/
private void initData() {
//假设联网请求数据N条,这里我就用本地数据
data = new ArrayList<>();
for (int i = 0; i < 40; i++) {
data.add(i + ". 成功请求数据");
}
recyclerview.setLayoutManager(layoutmanager);
adapter = new Adapter(MainActivity.this, data);
recyclerview.setHasFixedSize(true);
recyclerview.setAdapter(adapter);
}
}
默认的LayoutManager
![LayoutManager==LinearLayoutManager.png](http://upload-images.jianshu.io/upload_images/2789715-3701f5ba45cdff7d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
那么切换LayoutManager为GridLayoutManager,效果会是什么样呢
![LayoutManager为GridLayoutManager.png](http://upload-images.jianshu.io/upload_images/2789715-140c3fc0ad08fe70.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后,切换到瀑布流
![LayoutManager为StaggeredGridLayoutManager.png](http://upload-images.jianshu.io/upload_images/2789715-0cfd753ab973a980.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
什么效果都没有,这是为什么呢,因为,设置瀑布流,我们需要对item的间隙进行设置,不然没有效果
首先自定义ItemDecoration
public class ItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public ItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
if (parent.getChildAdapterPosition(view) == 0) {
outRect.top = space;
}
}
}
设置间隙
* 请求数据
*/
private void initData() {
//假设联网请求数据N条,这里我就用本地数据
data = new ArrayList<>();
for (int i = 0; i < 40; i++) {
data.add(i + ". 成功请求数据");
}
recyclerview.setLayoutManager(layoutmanager);
if(layoutmanager.equals(staggeredGridLayoutManager)) { //针对瀑布流,设置间隙
recyclerview.addItemDecoration(new ItemDecoration(15));
}
adapter = new Adapter(MainActivity.this, data);
recyclerview.setHasFixedSize(true);
recyclerview.setAdapter(adapter);
}
重新编译运行,搞定
![瀑布流效果.png](http://upload-images.jianshu.io/upload_images/2789715-18bd62a8639f13fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
这算是Recyclerview的简单使用,实现三种模式,已经搞定,还有很多细节,留到下一篇,仔细讲解。