1.在as当中导入RecyclerView的jar包:
compile ‘com.android.support:recyclerview-v7:23.1.0’
2.在布局文件当中生命RecyclerView的控件
package demo.yangzc.com.recycledemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class RecycleDemoActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private List<String> mDatas;
private RecyclerViewAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle_demo);
initViews();
initDatas();
}
private void initViews() {
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
private void initDatas() {
mDatas = new ArrayList<String>();
for (int i = 'A'; i <= 'z'; i++) {
mDatas.add("" + (char) i);
}
mAdapter = new RecyclerViewAdapter(this, mDatas);
mRecyclerView.setAdapter(mAdapter);
//设置RecyclerView的布局管理
// 1.上下文 2.放向 3.是否反向布局
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(linearLayoutManager);
//设置RecycView的Item间的分割线
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
}
}
Adapter
package demo.yangzc.com.recycledemo;
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 java.util.List;
/**
* @author yangzc
* @data 2015/12/1 17:40
* @desc RecyclerViewAdapter
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<MyViewHoder> {
//用于每个item的布局
private LayoutInflater mInflater;
private Context mContext;
protected List<String> mDatas;
@Override
//创建ViewHoler
public MyViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
//item布局
View view = mInflater.inflate(R.layout.item_simple_textview, parent, false);
//传入item布局
MyViewHoder viewHoder = new MyViewHoder(view);
return viewHoder;
}
public void addData(int position) {
mDatas.add(position, "Insert One");
//不会去刷新所有的View 就不会重置position
notifyItemInserted(position);
}
public void delData(int position) {
mDatas.remove(position);
notifyItemRemoved(position);
}
@Override
//绑定ViewHolder
public void onBindViewHolder(final MyViewHoder holder, int position) {
holder.textView.setText(mDatas.get(position));
}
@Override
public int getItemCount() {
return mDatas == null ? 0 : mDatas.size();
}
public RecyclerViewAdapter(Context context, List<String> datas) {
this.mContext = context;
this.mDatas = datas;
mInflater = LayoutInflater.from(context);
}
}
class MyViewHoder extends RecyclerView.ViewHolder {
TextView textView;
public MyViewHoder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.id_tv);
}
}
item的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_margin="3dp"
android:background="@drawable/item_bg"
android:layout_height="72dp">
<TextView
android:id="@+id/id_tv"
android:layout_width="72dp"
android:layout_height="match_parent"
android:gravity="center"/>
</FrameLayout>
实现间隔效果:
package demo.yangzc.com.recycledemo;
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;
/**
* @author yangzc
* @data 2015/12/1 19:51
* @desc 实现了RecyclerView添加分割线(当使用LayoutManager为LinearLayoutManager时)
* 该实现类可以看到通过读取系统主题中的 android.R.attr.listDivider作为Item间的分割线,并且支持横向和纵向。
*/
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* limitations under the License.
*/
/**
* This class is from the v7 samples of the Android SDK. It's not by me!
* <p/>
* 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;
private 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);
android.support.v7.widget.RecyclerView v = new android.support.v7.widget.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);
}
}
}
最终效果
ok 到这用RecyclerView实现listView效果就介绍完了,接下来我们介绍一下GridView效果的实现