RecycleView是google为了优化listview,gridview 提供的一个新的控件。

1.android 导入recycleview

在app的gradle里面加入:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
testCompile 'junit:junit:4.12'
compile 'com.android.support:recyclerview-v7:23.1.+'

compile 'com.android.support:appcompat-v7:23.1.1'
}

其中23为sdk版本号。

2.使用RecycleView

RecycleView主要功能分为几个模块:

adapter 的功能与以前类似。

package com.joyfulmath.sample.javanetwork.androidbase.recycleview.view;

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.joyfulmath.sample.javanetwork.R; import java.util.ArrayList; /**
* @author deman.lu
* @version on 2016-03-10 14:19
*/
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.RecycleViewHolder> { private Context context;
private ArrayList<String> mDatas;
public RecycleAdapter(Context context,ArrayList<String> mStrings)
{
this.context = context;
this.mDatas = mStrings;
} @Override
public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecycleViewHolder holder = new RecycleViewHolder(LayoutInflater.from(context)
.inflate(R.layout.item_list,parent,false));
return holder;
} @Override
public void onBindViewHolder(RecycleViewHolder holder, int position) {
holder.tv1.setText(mDatas.get(position));
holder.tv2.setText(mDatas.get(position)+" demo");
} @Override
public int getItemCount() {
return mDatas.size();
} class RecycleViewHolder extends RecyclerView.ViewHolder
{
TextView tv1;
TextView tv2;
public RecycleViewHolder(View itemView) {
super(itemView);
tv1 = (TextView) itemView.findViewById(R.id.tv_1);
tv2 = (TextView) itemView.findViewById(R.id.tv_2);
}
}
}

首先 需要定义一个holder,这个holder是用来优化复用recycleview的item的。此与listview已经非常类似。

其次adapter要实现3个方法:

public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) //viewholder 布局代码
public void onBindViewHolder(RecycleViewHolder holder, int position)  数据处理代码
 public int getItemCount()   得到item size

相当熟悉的代码。

LayoutManager:

RecycleView 默认支持linearlayoutmanager & gridlayoutmanager

其实就是对应listview & gridview

mRcyV.setLayoutManager(new LinearLayoutManager(this));
    void afterView()
{
mPresenter = new RecyclePresenter();
mRcyV.setLayoutManager(new GridLayoutManager(this,4));
mRcyV.setAdapter(new RecycleAdapter(this, mPresenter.getData()));
mRcyV.addItemDecoration(new RecycleGridItemDecoration(this));
}

ItemDectoration:

本质上是每个itemview的子view。很多时候用途就是定义为 分隔线使用。

1.listview.

public class RecycleItemDecoration 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 RecycleItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = new ColorDrawable(Color.argb(0xff,0xff,0x22,0x33));
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);
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+10);
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);
}
}
}

2.gridview 样式:

public class RecycleGridItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[] { android.R.attr.listDivider };
private Drawable mDivider; public RecycleGridItemDecoration(Context context)
{
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
} @Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)
{ drawHorizontal(c, parent);
drawVertical(c, parent); } private int getSpanCount(RecyclerView parent)
{
// 列数
int spanCount = -1;
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager)
{ spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
} else if (layoutManager instanceof StaggeredGridLayoutManager)
{
spanCount = ((StaggeredGridLayoutManager) layoutManager)
.getSpanCount();
}
return spanCount;
} public void drawHorizontal(Canvas c, RecyclerView parent)
{
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.getLeft() - params.leftMargin;
final int right = child.getRight() + params.rightMargin
+ mDivider.getIntrinsicWidth();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
} public void drawVertical(Canvas c, RecyclerView parent)
{
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 top = child.getTop() - params.topMargin;
final int bottom = child.getBottom() + params.bottomMargin;
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicWidth(); mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
} private boolean isLastColum(RecyclerView parent, int pos, int spanCount,
int childCount)
{
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager)
{
if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
{
return true;
}
} else if (layoutManager instanceof StaggeredGridLayoutManager)
{
int orientation = ((StaggeredGridLayoutManager) layoutManager)
.getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL)
{
if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
{
return true;
}
} else
{
childCount = childCount - childCount % spanCount;
if (pos >= childCount)// 如果是最后一列,则不需要绘制右边
return true;
}
}
return false;
} private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
int childCount)
{
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager)
{
childCount = childCount - childCount % spanCount;
if (pos >= childCount)// 如果是最后一行,则不需要绘制底部
return true;
} else if (layoutManager instanceof StaggeredGridLayoutManager)
{
int orientation = ((StaggeredGridLayoutManager) layoutManager)
.getOrientation();
// StaggeredGridLayoutManager 且纵向滚动
if (orientation == StaggeredGridLayoutManager.VERTICAL)
{
childCount = childCount - childCount % spanCount;
// 如果是最后一行,则不需要绘制底部
if (pos >= childCount)
return true;
} else
// StaggeredGridLayoutManager 且横向滚动
{
// 如果是最后一行,则不需要绘制底部
if ((pos + 1) % spanCount == 0)
{
return true;
}
}
}
return false;
} @Override
public void getItemOffsets(Rect outRect, int itemPosition,
RecyclerView parent)
{
int spanCount = getSpanCount(parent);
int childCount = parent.getAdapter().getItemCount();
if (isLastRaw(parent, itemPosition, spanCount, childCount))// 如果是最后一行,则不需要绘制底部
{
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
} else if (isLastColum(parent, itemPosition, spanCount, childCount))// 如果是最后一列,则不需要绘制右边
{
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else
{
outRect.set(0, 0, mDivider.getIntrinsicWidth(),
mDivider.getIntrinsicHeight());
}
}
}

最后是activity页面:

@EActivity(R.layout.activity_recycleview_demo)
public class RecycleViewDemo extends Activity{
@ViewById(R.id.act_solution_1_lv)
RecyclerView mRcyV; RecyclePresenter mPresenter; @AfterViews
void afterView()
{
mPresenter = new RecyclePresenter();
mRcyV.setLayoutManager(new GridLayoutManager(this,4));
mRcyV.setAdapter(new RecycleAdapter(this, mPresenter.getData()));
mRcyV.addItemDecoration(new RecycleGridItemDecoration(this));
}
}

ItemAnimator:

recycleview的每一个item的add & delete都是可配置动画的。DefaultItemAnimator是google提供的默认实现方式。

mRcyV.setItemAnimator(new DefaultItemAnimator());
    public void addData(int position) {
mDatas.add(position, "Insert One");
notifyItemInserted(position);
} public void removeData(int position) {
mDatas.remove(position);
notifyItemRemoved(position);
}

在adapter中实现这2个方法,关键在于notifyItem***, 这2个方法可以触发动画。

recycleview比起listview来说,应该是可以定制更多的东西,尤其是瀑布模式,实现起来效果很炫。

参考:

http://blog.csdn.net/lmj623565791/article/details/45059587

android基础开发之RecycleView(1)---基本使用方式的更多相关文章

  1. android基础开发之WebView

    WebView 是android平台沟通 http & H5 页面的桥梁. 但是google对这块的表述不是很清晰,而且SDK里面基本看不到源码,只有一个接口而已. 传送:http://dev ...

  2. android基础开发之scrollview

    scrollView 是android系统提供的一种 特殊的展示view. 其实我们很早就遇到过scrollview的东东,比如listview. 而google官方文档也提出,不要混合使用scrol ...

  3. Android安全开发之WebView中的地雷

    Android安全开发之WebView中的地雷 0X01 About WebView 在Android开发中,经常会使用WebView来实现WEB页面的展示,在Activiry中启动自己的浏览器,或者 ...

  4. android软件开发之webView.addJavascriptInterface循环渐进【一】

    本篇文章由:http://www.sollyu.com/android-software-development-webview-addjavascriptinterface-cycle-of-gra ...

  5. Android NDK开发之C调用Java及原生代码断点调试(二)

    上一篇中,我们主要学习了Java调用本地方法,并列举了两大特殊实例来例证我们的论据,还没学习的伙伴必须先去阅读下,本次的学习是直接在上一篇的基础上进行了.点击:Android NDK开发之从Java与 ...

  6. Android混合开发之WebViewJavascriptBridge实现JS与java安全交互

    前言: 为了加快开发效率,目前公司一些功能使用H5开发,这里难免会用到Js与Java函数互相调用的问题,这个Android是提供了原生支持的,不过存在安全隐患,今天我们来学习一种安全方式来满足Js与j ...

  7. Android混合开发之WebView与Javascript交互

    前言: 最近公司的App为了加快开发效率选择了一部分功能采用H5开发,从目前市面的大部分App来讲,大致分成Native App.Web App.Hybrid App三种方式,个人觉得目前以Hybri ...

  8. Android混合开发之WebView使用总结

    前言: 今天修改项目中一个有关WebView使用的bug,激起了我总结WebView的动机,今天抽空做个总结. 混合开发相关博客: Android混合开发之WebView使用总结 Android混合开 ...

  9. Android安全开发之ZIP文件目录遍历

    1.ZIP文件目录遍历简介 因为ZIP压缩包文件中允许存在“../”的字符串,攻击者可以利用多个“../”在解压时改变ZIP包中某个文件的存放位置,覆盖掉应用原有的文件.如果被覆盖掉的文件是动态链接s ...

随机推荐

  1. JavaScript的学习--正则表达式

    今天用正则表达式的时候遇到了不少问题,就研究了一下,参考了不少博客,特此记录. 正则表达式的参数    参考 /i (忽略大小写)/g (全文查找出现的所有匹配字符)/m (多行查找)/gi(全文查找 ...

  2. Configuring Service Broker for Asynchronous Processing

    Configuring Service Broker for Asynchronous Processing --create a database and enable the database f ...

  3. C# 文字转声音

    添加COM组件引用:Microsoft Speech object library private SpVoice voice; private void button1_Click(object s ...

  4. Gridview的RowDataBound事件(添加删除提示,改变背景颜色)

    protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e) { //如果是绑定数据行 if (e.Row.Row ...

  5. treap树---营业额统计

    台州学院  2924 描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况.Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额 ...

  6. HDU 2256 Problem of Precision 数论矩阵快速幂

    题目要求求出(√2+√3)2n的整数部分再mod 1024. (√2+√3)2n=(5+2√6)n 如果直接计算,用double存值,当n很大的时候,精度损失会变大,无法得到想要的结果. 我们发现(5 ...

  7. [moka同学笔记]yii2.0数据库操作以及分页

    1.model中models/article.php 1 <?php 2 3 namespace app\models; 4 5 use Yii; 6 7 /** 8 * This is the ...

  8. Hibernate框架之注解的配置

    在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式 ...

  9. php生成静态文件

    1,通用生成方法 //获取文件内容 $content=file_get_contents("http://www.google.com/" ); $id=110; $filenam ...

  10. Oracle 查询并删除重复记录的SQL语句

    查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select  ...