android l 新控件,android L新控件RecyclerView具体解释DeMo

简介

在谷歌的官方网站上,我们可以看到,它是此演示文稿:RecyclerViewis

a more advanced and flexible version ofListView.

This widget is a container for large sets of views that can be recycled and scrolled very efficiently. Use theRecyclerViewwidget

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 theRecyclerViewwidget,

you have to specify an adapter and a layout manager. To create an adapter, you extend theRecyclerView.Adapterclass.

The details of the implementation depend on the specifics of your dataset and the type of views. For more information, see theexamplesbelow.

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

Demo介绍不同于官网

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

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

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

cc2318d543c255429a38f050d085de8c.png

实现图片文字button的混排。

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

878b1174f46053887db4fc0035d5cf6c.png

首先还是贴出我的main_acitivy.xml

xml version="1.0" encoding="utf-8"?>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/my_recycler_view"

android:scrollbars="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

其它几个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 {

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;

}

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 myDataset = new ArrayList();

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传送门:开始转移

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值