标签:
RecyclerView是Android 5.0 materials design中的组件之一,相应的还有CardView、Palette等。我们知道,Listview中的Adapter中可以实现ViewHolder的复用。RecyclerView提供了一个耦合度更低的方式来复用ViewHolder,并且可以轻松的实现ListView、GridView以及瀑布流的效果。RecyclerView支持水平滚动和垂直滚动两种列表,而且还支持Grid格子布局和乱序布局,很爽吧~
1、如果想要使用RecyclerView该控件,我们首先要导入RecyclerView这个控件,直接在build.gradle文件下添加如下配置:
dependencies {
compile fileTree(include: [‘*.jar‘], dir: ‘libs‘)
testCompile ‘junit:junit:4.12‘
compile ‘com.android.support:appcompat-v7:23.2.0‘
compile ‘com.android.support:recyclerview-v7:23.2.0‘
compile ‘com.android.support:cardview-v7:23.2.0‘
compile ‘com.android.support:palette-v7:23.2.0‘
}
compile ‘com.android.support:cardview-v7:23.2.0’
cardview在Item布局里使用到
compile ‘com.android.support:palette-v7:23.2.0’
在RecyclerAdapter里对图片的颜色提取使用到Palette
2、导入了RecyclerView之后,我们可以在xml文件上添加一个RecyclerView,如下:
<android.support.v7.widget.RecyclerView
android:id="@+id/demo_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3、通过id获取得RecyclerView的对象,并且设置显示方式和添加删除Item时的动画效果:
mRecyclerView = (RecyclerView) findViewById(R.id.demo_recyclerView);
// 设置布局显示方式,这里我使用都是垂直方式——LinearLayoutManager.VERTICAL
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
// 设置添加删除item的时候的动画效果
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
setLayoutManager()方法接受一个 LayoutManager 布局管理参数。参数类型可以有以下几种:
LinearLayoutManager:线性布局
GridLayoutManager:网格布局
StaggeredGridLayoutManager:流式布局
那么怎么new一个LayoutManager出来呢?举个例子:
new LinearLayoutManager(this, LinearLayout.VERTICAL, true)
4、RecyclerView是一个容器控件,那么数据内容用什么来封装和操作呢?
1)先准备一个bean类,代表一个item的内容,在这里以一张图(图的id表示)和文字为例:
public class Item {
private String title;
private int resId;
public Item(String title, int resId) {
this.title = title;
this.resId = resId;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setResId(int resId) {
this.resId = resId;
}
public int getResId() {
return resId;
}
}
2)Item也需要显示的布局xml,对应的:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:layout_width="160dp"
android:layout_height="200dp"
android:layout_gravity="center"
card:cardBackgroundColor="@android:color/white"
card:cardCornerRadius="5dp"
card:cardElevation="2dp"
card:cardMaxElevation="@dimen/cardview_default_elevation">
<!--cardMaxElevation:最大卡片阴影的宽度-->
<!--cardElevation:卡片阴影的宽度-->
<!--cardBackgroundColor:卡片的背景颜色-->
<!--cardCornerRadius :卡片的圆角半径-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_weight="3"
android:scaleType="fitXY" />
<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:padding="5dp"
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
3)用RecyclerAdapter来操作数据内容,给RecyclerView使用:
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by Administrator on 2016/5/6 0006.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private Context context;
private List<Item> itemList;
private Resources resources;
private LayoutInflater inflater;
public RecyclerAdapter(Context context, List<Item> itemList) {
this.context = context;
this.itemList = itemList;
this.resources = context.getResources();
inflater = LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_recyclerview, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final Item item = itemList.get(position);
holder.title.setText(item.getTitle());
holder.image.setImageResource(item.getResId());
Bitmap bitmap = BitmapFactory.decodeResource(resources, item.getResId());
//异步获得bitmap图片颜色值
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
// Palette提取颜色
Palette.Swatch vibrant = palette.getVibrantSwatch();//有活力
Palette.Swatch c = palette.getDarkVibrantSwatch();//有活力 暗色
Palette.Swatch d = palette.getLightVibrantSwatch();//有活力 亮色
Palette.Swatch f = palette.getMutedSwatch();//柔和
Palette.Swatch a = palette.getDarkMutedSwatch();//柔和 暗色
Palette.Swatch b = palette.getLightMutedSwatch();//柔和 亮色
if (vibrant != null) {
int color1 = vibrant.getBodyTextColor();//内容颜色
int color2 = vibrant.getTitleTextColor();//标题颜色
int color3 = vibrant.getRgb();//rgb颜色
if(position<4) {
holder.title.setBackgroundColor(f.getRgb());
holder.title.setTextColor(f.getTitleTextColor());
}else{
holder.title.setBackgroundColor(vibrant.getRgb());
holder.title.setTextColor(vibrant.getTitleTextColor());
}
}
}
});
}
@Override
public int getItemCount() {
return null == itemList ? 0 : itemList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.item_title);
image = (ImageView) itemView.findViewById(R.id.item_image);
}
}
}
5、MainActivity的源码:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerAdapter mRecyclerAdapter;
private List<Item> viewData = new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadDatas();
initViews();
}
private void loadDatas() {
viewData.add(new Item("第1张图", R.mipmap.item1));
viewData.add(new Item("第2张图", R.mipmap.item2));
viewData.add(new Item("第3张图", R.mipmap.item3));
viewData.add(new Item("第4张图", R.mipmap.item4));
viewData.add(new Item("第5张图", R.mipmap.item5));
viewData.add(new Item("第6张图", R.mipmap.item6));
viewData.add(new Item("第7张图", R.mipmap.item7));
viewData.add(new Item("第8张图", R.mipmap.item8));
}
private void initViews() {
mRecyclerView = (RecyclerView) findViewById(R.id.demo_recyclerView);
// 设置布局显示方式,这里我使用都是垂直方式——LinearLayoutManager.VERTICAL
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
// 设置添加删除item的时候的动画效果
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
// 初始化適配器
mRecyclerAdapter = new RecyclerAdapter(this, viewData);
// 设置适配器
mRecyclerView.setAdapter(mRecyclerAdapter);
}
}
补充,RecyclerView还有很多可用常用的方法:
RecyclerView添加,删除,更新数据:
notifyDataSetChanged():更新所有数据
notifyItemInserted(int position):在position位置插入数据的时候更新
notifyItemRemoved(int position):移除postion位置的数据的时候更新
notifyItemChanged(int position):当postion位置数据有改变时候更新
notifyItemMoved(int fromPosition, int toPosition):移除从位置formPosition到toPosition位置数据更新
notifyItemRangeChanged(int positionStart, int itemCount)
notifyItemRangeInserted(int positionStart, int itemCount)
notifyItemRangeRemoved(int positionStart, int itemCount)
标签:
原文地址:http://blog.csdn.net/u013091087/article/details/51331133