简介

在谷歌的官方网站上,我们可以看到,它是此演示文稿:RecyclerView is
a more advanced and flexible version of ListView.
This widget is a container for large sets of views that can be recycled and scrolled very efficiently. Use the RecyclerView widget
when you have lists with elements that change dynamically.

RecyclerView比listview更先进更灵活,对于非常多的视图它就是一个容器,能够有效的重用和滚动。当数据动态变化的时候请使用它。

RecyclerView is easy to use, because it provides:

  • A layout manager for positioning items
  • Default animations for common item operations
  • You also have the flexibility to define custom layout managers and animations for this widget.

RecyclerView使用起来非常方便由于它提供:

它为item的定位提供一个layoutmanager

为item的操作提供一个缺省的animations

您还能够灵活地定义这个小部件的自己定义布局管理器和动画

To use the RecyclerView widget,
you have to specify an adapter and a layout manager. To create an adapter, you extend the RecyclerView.Adapter class.
The details of the implementation depend on the specifics of your dataset and the type of views. For more information, see the examples below.

为了使用RecyclerVIew,你必须指定一个adapter和一个layoutmanager。为了创建一个adapter,你必须得继承RecyclerView.Adapter,具体的实现方法取决与你的数据集和你视图的类型。

Demo介绍不同于官网

这里就介绍完了以下我们就要做自己的Demo了。

假设须要看官网的Demo那么请打开这里:官方Demo

这里既然是具体解释那么就要与官方的Demo有不同,好了看看我们要做的效果吧。

实现图片文字button的混排。

首先还是看我的project结构吧。

首先还是贴出我的main_acitivy.xml

<?

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

其它几个xml就不用贴了,非常easy的放了写TextVIew,ImgeView之类。

然后我们就来看看代码,首先是Bean里面的代码。

package com.androidl.bob;

/**
* 实体包
*
* @author edsheng
*
*/
public class Bean {
public static final int Y_TYPE = 0; //view类型0
public static final int X_TYPE = 1; //view类型2
public static final int Z_TYPE = 2;//view 类型3
private int type;
private String text; public Bean(int type, String text) {
super();
this.type = type;
this.text = text;
} public int getType() {
return type;
} public void setType(int type) {
this.type = type;
} public String getText() {
return text;
} public void setText(String text) {
this.text = text;
} }

然后是Adapter里面的代码:

package com.androidl.bob;

import java.util.List;

import com.example.androidl.R;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; /**
* Date : 2014/7/15
*
* @author edsheng
*
*/
public class RecycleAdapter extends RecyclerView.Adapter<ViewHolder> { private List<Bean> beans; public RecycleAdapter(List<Bean> beans) {
super();
this.beans = beans;
} /**
* 内部TextHoler
*
* @author edsheng
*
*/
public class TextHoler extends RecyclerView.ViewHolder {
public TextView textView; public TextHoler(View textview) {
super(textview);
this.textView = (TextView) textview.findViewById(R.id.mytext);
}
} /**
* iamgeHolder
*
* @author edsheng
*
*/
public class ImageHoler extends RecyclerView.ViewHolder {
public ImageView Imageview; public ImageHoler(View textview) {
super(textview);
this.Imageview = (ImageView) textview.findViewById(R.id.myiamge);
}
} /**
* 按钮的holder
*
* @author edsheng
*
*/
public class ButtonHolder extends RecyclerView.ViewHolder {
public Button button; public ButtonHolder(View textview) {
super(textview);
this.button = (Button) textview.findViewById(R.id.mybutton);
}
} @Override
public int getItemCount() {
// TODO Auto-generated method stub
return beans.size();
} /**
* 获取消息的类型
*/
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return beans.get(position).getType();
} /**
* 创建VIewHolder
*/
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewtype) {
// TODO Auto-generated method stub
View v = null;
ViewHolder holer = null;
switch (viewtype) {
case Bean.X_TYPE:
v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.recylce_item_x, null);
holer = new TextHoler(v);
break;
case Bean.Y_TYPE:
v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.recylce_item_y, null);
holer = new ButtonHolder(v);
break;
case Bean.Z_TYPE:
v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.recylce_item_z, null);
holer = new ImageHoler(v);
break;
} return holer;
} /**
* 绑定viewholder
*/
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// TODO Auto-generated method stub
switch (getItemViewType(position)) {
case Bean.X_TYPE:
TextHoler textholer = (TextHoler) holder;
textholer.textView.setText(beans.get(position).getText());
break;
case Bean.Y_TYPE:
ButtonHolder buttonHolder = (ButtonHolder) holder;
buttonHolder.button.setText(beans.get(position).getText());
break;
case Bean.Z_TYPE:
ImageHoler imageHoler = (ImageHoler) holder;
// imageHoler.Imageview.setImageResource(android.R.drawable.checkbox_on_background);
break;
}
}
}

最后是activity的代码。

package com.androidl.bob;

import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import com.example.androidl.R; public class Mainactivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); // // improve performance if you know that changes in content
// // do not change the size of the RecyclerView
// mRecyclerView.setHasFixedSize(true); //创建布局管理器
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager); //初始化数据
List<Bean> myDataset = new ArrayList<Bean>(); myDataset.add(new Bean(Bean.Z_TYPE, "图片"));
myDataset.add(new Bean(Bean.X_TYPE, "文字"));
myDataset.add(new Bean(Bean.Y_TYPE, "button"));
myDataset.add(new Bean(Bean.Z_TYPE, "图片"));
myDataset.add(new Bean(Bean.X_TYPE, "shit"));
myDataset.add(new Bean(Bean.X_TYPE, "我擦"));
myDataset.add(new Bean(Bean.Z_TYPE, "图片"));
myDataset.add(new Bean(Bean.Y_TYPE, "button"));
myDataset.add(new Bean(Bean.Y_TYPE, "button"));
myDataset.add(new Bean(Bean.X_TYPE, "文字"));
//创建Adapter
RecycleAdapter mAdapter = new RecycleAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter); } }

Demo传送门:开始转移

版权声明:本文博主原创文章,博客,未经同意不得转载。

android L新控件RecyclerView具体解释DeMo的更多相关文章

  1. Android L新控件RecyclerView简介

    Android L是android进化史上的里程碑,尽管还没有正式发布4.5或者5.0,但预览版也同样精彩. 这篇文章只是另外一篇博客的总结性翻译,能够读懂原文的,可以点开这个链接去阅读精彩的原文:h ...

  2. android L新控件RecyclerView详解与DeMo[转]

    http://blog.csdn.net/codebob/article/details/37813801 在谷歌的官网我们可以看到它是这样介绍的: RecyclerView  is a more a ...

  3. android L 新控件侧滑菜单DrawerLayout 使用教程

    介绍 drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产 ...

  4. 【转】Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用

    Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用 分类: Android UI2015-06-15 16: ...

  5. 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用

    Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...

  6. Android其它新控件 (转)

    原文出处:http://blog.csdn.net/lavor_zl/article/details/51312715 Android其它新控件是指非Android大版本更新时提出的新控件,也非谷歌I ...

  7. android design 新控件

    转载请标明出处: http://blog.csdn.net/forezp/article/details/51873137 本文出自方志朋的博客 最近在研究android 开发的新控件,包括drawe ...

  8. Android新控件RecyclerView剖析

    传智·没羽箭(传智播客北京校区Java学院高级讲师) 个人简单介绍:APKBUS专家之中的一个,黑马技术沙龙会长,在移动领域有多年的实际开发和研究经验.精通HTML5.Oracle.J2EE .Jav ...

  9. [转 载] android 谷歌 新控件(约束控件 )ConstraintLayout 扁平化布局

    序 在Google IO大会中不仅仅带来了Android Studio 2.2预览版,同时带给我们一个依赖约束的库. 简单来说,她是相对布局的升级版本,但是区别与相对布局更加强调约束.何为约束,即控件 ...

随机推荐

  1. Java学习之道:空指针错误求解救????????????

    运行下面这个主方法红色代码的地方就报如下错: https://api.weibo.com/2/users/show.json?access_token=2.00szM9cCc4R_EC6ebcf150 ...

  2. Android_模拟时钟内时针、分针触摸转动

    最近实现了android里的一个机能,在activity里面画了一个模拟的时针,然后触摸上面的时针跟分针可以实现调时间的功能. 其实,说起原来来还是挺简单的,但是我花了将近一周的时间才全部实现,有点惭 ...

  3. VIM命令集

    Command Action Notes vim file +54 open file and go to line 54 any : command can be run using + on co ...

  4. 公司需求知识自学-Oracle的Package的作用及用法

    Oracle的Package的作用 简化应用设计.提高应用性能.实现信息隐藏.子程序重载. 1.Oracle的Package除 了把存储过程放到一堆儿以外还有没有其他的作用(好处)? 你不觉得把存储过 ...

  5. 收藏的Android很好用的组件或者框架。

    收藏的Android很好用的组件或者框架. android框架  先说两个站点: http://www.androidviews.net/ 非常好的国外开源码站,就是訪问速度有点慢啊 http://w ...

  6. 矩形旋转碰撞,OBB方向包围盒算法实现

    怎样进行2D旋转矩形的碰撞检測.能够使用一种叫OBB的检測算法(Oriented bounding box)方向包围盒.这个算法是基于SAT(Separating Axis Theorem)分离轴定律 ...

  7. Spring 事情具体详尽的解释

    一.Spring事务管理  1. Spring事务管理机制 三个核心部分  1) PlatformTransactionManager  平台的事务管理器  commit 提交事务.rollback ...

  8. 理解RESTful架构(转)

    越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(high latency).高 ...

  9. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:3.技术简介之MinaFilter——LoggingFilter (转)

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 LoggingFilter 接下来,使我们对Filter介绍的最后一个——LoggingFilter. 与Proto ...

  10. oracle 锁表、解锁的语句

     --1.以下的语句用来查询哪些对象被锁: select object_name,machine,s.sid,s.serial# from v$locked_object l,dba_object ...