博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android L新控件RecyclerView详解与DeMo[转]
阅读量:6888 次
发布时间:2019-06-27

本文共 4119 字,大约阅读时间需要 13 分钟。

 

 

在谷歌的官网我们可以看到它是这样介绍的: 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  

 below.

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

Demo介绍不同于官网

这里就介绍完了下面我们就要做自己的Demo了。如果需要看官网的Demo那么请打开这里: 

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

实现图片文字按钮的混排。

首先还是看我的工程结构吧。

首先还是贴出我的main_acitivy.xml

其他几个xml就不用贴了,很简单的放了写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
{ private List
beans; public RecycleAdapter(List
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; }

转载地址:http://uetbl.baihongyu.com/

你可能感兴趣的文章
OA项目笔记
查看>>
引用计数 vs. GC
查看>>
jquery实用的一些方法
查看>>
质数方阵
查看>>
jQuery $.each用法
查看>>
C语言结构体指针成员强制类型转换
查看>>
5.31 dockrer
查看>>
FreeCodeCamp----Intermediate Algorithm Scripting解法
查看>>
软件工程第二章 习题2 第4题
查看>>
《JavaScript设计模式与开发实践》读书笔记之命令模式
查看>>
hdu Problem 1242 Rescue bfs + 优先队列
查看>>
HDU-1507-Uncle Tom's Inherited Land*
查看>>
force里面的射线检测
查看>>
oracle 12.1.0.2中对象锁对系统的较大影响
查看>>
tensorboard的使用
查看>>
java进程占用CPU资源过高分析脚本
查看>>
day17--JQuery实例
查看>>
0312-css样式(选择器、文本text、字体fonts、背景background)
查看>>
【BZOJ】4358: permu 莫队算法
查看>>
powerdesigner 遇到的各种问题总结
查看>>