在使用RecyclerView之前我们还需要导入依赖库,在build.gradle中的dependencies下添加:
compile 'com.android.support:recyclerview-v7:24.2.1'
通常情况下的RecyclerView的Adapter:
package com.gcy.universal_adapter_recyclerview.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.gcy.universal_adapter_recyclerview.Entity.Bean;
import com.gcy.universal_adapter_recyclerview.R;
import java.util.List;
/**
* Created by gcy71 on 2016/9/28.
*/
public class MyAdapter extends RecyclerView.Adapter.MyViewHolder> {
private Context mContext;
private List mDatas;
private LayoutInflater mInflater;
public MyAdapter(Context context , List datas) {
this.mContext = context;
this.mDatas = datas;
this.mInflater = LayoutInflater.from(mContext);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(mInflater.inflate(R.layout.item_recyclerview, parent, false));
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.mTilte.setText(mDatas.get(position).getTitle());
holder.mInfo.setText(mDatas.get(position).getInfo());
holder.mTime.setText(mDatas.get(position).getTime());
}
@Override
public int getItemCount() {
return mDatas.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView mTilte;
TextView mInfo;
TextView mTime;
public MyViewHolder(View itemView) {
super(itemView);
mTilte = (TextView) itemView.findViewById(R.id.id_title);
mInfo = (TextView) itemView.findViewById(R.id.id_info);
mTime = (TextView) itemView.findViewById(R.id.id_time);
}
}
}
观察发现,RecycleView已经强制我们使用ViewHolder,我们在这里仅仅只发现了getItemCount和onCreateViewHolder两个函数存在重复的代码
BaseViewHolder.java
package com.gcy.universal_adapter_recyclerview.utils;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;
/**
* Created by gcy71 on 2016/9/28.
*/
public class BaseViewHolder extends RecyclerView.ViewHolder {
/*用于存储各类控件的容器*/
private SparseArray mViews;
private View itemView;
public BaseViewHolder(View itemView) {
super(itemView);
mViews = new SparseArray();
this.itemView = itemView;
itemView.setTag(this);
}
//通过ViewID在容器中获取控件,若在容器中没有该控件就通过ID获取,然后存储到容器中
public T getView(int ViewID){
View view = mViews.get(ViewID);
if(view == null){
view = itemView.findViewById(ViewID);
mViews.put(ViewID , view);
}
return (T) view;
}
/**
* 辅助方法,减少用户为控件设置值的代码
*/
//返回值设置为BaseViewHolder类型是为了实现链式编程
public BaseViewHolder setText2TextView(int ViewID , String text){
((TextView)getView(ViewID)).setText(text);
return this;
}
}
分析第一步里的MyAdapter可以看出在Adapter里我们至少需要一个Context和一个数据集还有Item的布局文件ID,和ListView的万能适配器类似,我们也定义一个为控件绑定数据的抽象方法,让用户自己实现。
MyBaseAdapter.java
package com.gcy.universal_adapter_recyclerview.utils;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by gcy71 on 2016/9/28.
*/
public abstract class MyBaseAdapter extends RecyclerView.Adapter {
private int LayoutID;
private Context mContext;
private List mDatas;
public MyBaseAdapter(int layoutID, Context mContext , List datas) {
this.LayoutID = layoutID;
this.mContext = mContext;
this.mDatas = datas;
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
BaseViewHolder holder = new BaseViewHolder(LayoutInflater.from(mContext).inflate(LayoutID, parent , false));
return holder;
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
blindViewHolder(holder , mDatas.get(position));
}
@Override
public int getItemCount() {
return mDatas.size();
}
public abstract void blindViewHolder(BaseViewHolder holder , T t);
}
RecyclerView并未给我们提供Item之间的分割线,所以需要我们自己定义分割线,这里我们直接引用网上的一个工具类DividerItemDecoration
package com.gcy.universal_adapter_recyclerview.utils;/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* limitations under the License.
*/
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* This class is from the v7 samples of the Android SDK. It's not by me!
*
* See the license above for details.
*/
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
protected Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
//RecyclerView v = new RecyclerView(parent.getContext());
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
Bean.java(本例中所使用的实体类)
package com.gcy.universal_adapter_recyclerview.Entity;
/**
* Created by gcy71 on 2016/9/26.
*/
public class Bean {
private String title;
private String info;
private String time;
public Bean(String title, String info, String time) {
this.title = title;
this.info = info;
this.time = time;
}
public Bean() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
item_recyclerview.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/id_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="我是标题"
android:textColor="#444"/>
<TextView
android:id="@+id/id_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/id_title"
android:minLines="1"
android:maxLines="2"
android:text="Android打造万能的ListView和GridView适配器"
android:textColor="#898989"
android:textSize="16sp"/>
<TextView
android:id="@+id/id_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/id_info"
android:minLines="1"
android:maxLines="2"
android:text="2016-09-26"
android:textColor="#898989"
android:textSize="12sp"/>
RelativeLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/id_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android.support.v7.widget.RecyclerView>
LinearLayout>
MainActivity.java
package com.gcy.universal_adapter_recyclerview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.gcy.universal_adapter_recyclerview.Entity.Bean;
import com.gcy.universal_adapter_recyclerview.utils.BaseViewHolder;
import com.gcy.universal_adapter_recyclerview.utils.DividerItemDecoration;
import com.gcy.universal_adapter_recyclerview.utils.MyBaseAdapter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List mDatas;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initRecyclerView();
}
private void initRecyclerView() {
mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
//此处我们使用匿名内部类的方式来为mRecyclerView设置Adapter
mRecyclerView.setAdapter(new MyBaseAdapter(R.layout.item_recyclerview , this , mDatas) {
@Override
public void blindViewHolder(BaseViewHolder holder, Bean bean) {
holder.setText2TextView(R.id.id_title , bean.getTitle())
.setText2TextView(R.id.id_info , bean.getInfo())
.setText2TextView(R.id.id_time,bean.getTime());
}
});
mRecyclerView.addItemDecoration(new DividerItemDecoration(this , DividerItemDecoration.VERTICAL_LIST));
}
private void initData() {
mDatas = new ArrayList();
for(int i=1 ; i<=10 ; i++){
mDatas.add(new Bean("我是标题 " + i, "Android打造ListView和GridView万能适配器" , "2016-09-28"));
}
}
}
测试效果:
项目源码:
https://github.com/Sunrise7878/Universal_adapter/tree/master/universal_adapter_recyclerview