新控件RecyclerVIew的介绍和兼容使用的方法

  RecyclerVIew是一个可以替代listview和Gallery的有效空间而且在support-v7中有了低版本支持,具体使用方式还是规规矩矩的适配器加控件模式。我们先来看看官网的介绍:

介绍

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  examplesbelow.

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

Google官方的一个例子:

1.布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <!-- A RecyclerView with some commonly used attributes -->

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="horizontal"
        />

</RelativeLayout>

2.item的布局文件,就是一个textview

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

3.主要代码

3.1 定义控件和布局

  /*
     * recyclerview提供这些内置的布局管理器:
     * linearlayoutmanager              显示垂直滚动列表或水平的项目。
     * gridlayoutmanager                显示在一个网格项目。
     * staggeredgridlayoutmanager       显示在交错网格项目。
     * 自定义的布局管理器,需要继承recyclerview.layoutmanager类。
     *
     * add/remove items时的动画是默认启用的。
     * 自定义这些动画需要继承RecyclerView.ItemAnimator,并实现RecyclerView.setItemAnimator()
     */  
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter<MyAdapter.ViewHolder> mAdapter;
    private LinearLayoutManager mLayoutManager;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String [] myDataset = {"Android","ios","jack","tony","window","mac","1234","hehe","495948"};

        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);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        //设置RecycleView的显示方向:(默认为垂直) 水平
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);

        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
    }

3.2 适配器

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private String[] mDataset;

        // Provide a reference to the type of views that you are using
        // (custom viewholder)
        public class ViewHolder extends RecyclerView.ViewHolder {
            public TextView mTextView;

            public ViewHolder(TextView v) {
                super(v);
                mTextView = v;
            }
        }

        // Provide a suitable constructor (depends on the kind of dataset)
        public MyAdapter(String[] myDataset) {
            mDataset = myDataset;
        }

        // Create new views (invoked by the layout manager)
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                int viewType) {
            // create a new view
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.my_text_view, parent, false);
            // set the view‘s size, margins, paddings and layout parameters

            ViewHolder vh = new ViewHolder((TextView) v);
            return vh;
        }

        // Replace the contents of a view (invoked by the layout manager)
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            holder.mTextView.setText(mDataset[position]);

        }

        // Return the size of your dataset (invoked by the layout manager)
        @Override
        public int getItemCount() {
            return mDataset.length;
        }
    }

全部代码:

package com.kale.recyclerviewtest;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter<MyAdapter.ViewHolder> mAdapter;
    private LinearLayoutManager mLayoutManager;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String [] myDataset = {"Android","ios","jack","tony","window","mac","1234","hehe","495948"};

        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);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        //设置RecycleView的显示方向:(默认为垂直) 水平
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);

        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
    }

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private String[] mDataset;

        // Provide a reference to the type of views that you are using
        // (custom viewholder)
        public class ViewHolder extends RecyclerView.ViewHolder {
            public TextView mTextView;

            public ViewHolder(TextView v) {
                super(v);
                mTextView = v;
            }
        }

        // Provide a suitable constructor (depends on the kind of dataset)
        public MyAdapter(String[] myDataset) {
            mDataset = myDataset;
        }

        // Create new views (invoked by the layout manager)
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                int viewType) {
            // create a new view
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.my_text_view, parent, false);
            // set the view‘s size, margins, paddings and layout parameters

            ViewHolder vh = new ViewHolder((TextView) v);
            return vh;
        }

        // Replace the contents of a view (invoked by the layout manager)
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            holder.mTextView.setText(mDataset[position]);

        }

        // Return the size of your dataset (invoked by the layout manager)
        @Override
        public int getItemCount() {
            return mDataset.length;
        }
    }
}

PS:这个控件好就好在可以自定义动画和布局的方向,在以前还需要我们自己写横向滑动的listview,现在有了它就方便多了。但缺点是没有listview那么完善,没添加头、尾视图的功能。再说下它的动画和布局类,以后用到的话就知道了。

(1)为每个条目位置提供了layout管理器(RecyclerView.setLayoutManager

(2)为每个条目设置了操作动画(RecyclerView.setItemAnimator

参考自:

http://blog.csdn.net/jjwwmlp456/article/details/40540233

http://blog.csdn.net/a396901990/article/details/40187769

时间: 2024-10-12 14:34:29

新控件RecyclerVIew的介绍和兼容使用的方法的相关文章

android L新控件RecyclerView详解与DeMo

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

Android5.0新控件CardView的介绍和使用

   CardView也是5.0的新控件,这控件其实就是一个卡片啦,当然我们自己也完全可以定义这样一个卡片,从现在的微博等社App中可以看到各式各样的自定义卡片,所以这个控件意义不是很大.比较是support中的view所以使用在布局里面的时候一下子看不到效果的,比较不好.CardView继承的是FrameLayout,所以摆放内部控件的时候需要注意一下啦. 建议:个人不是很建议用这个控件,因为我们完全可以自定义这样一个控件,这样既能在布局时直接看到效果又比较方便,关键问题是CardView中的

Android新控件RecyclerView浅析及上拉和下拉刷新

概述: RecyclerView是android-support-v7-21版本中新增的一个Widgets,RecyclerView是ListView的升级版本,更加先进和灵活.在以后的开发中我们就可以直接使用RecyclerView来替换ListView. 特点介绍: 1. 可横向展示 2. 消除错位问题 3. 标准化了ViewHolder 横向: private void initHorizaontal(List<ItemModel> models) { RecyclerView recy

Android新控件RecyclerView剖析

传智·没羽箭(传智播客北京校区Java学院高级讲师) 个人简介:APKBUS专家之一,黑马技术沙龙会长,在移动领域有多年的实际开发和研究经验,精通HTML5.Oracle.J2EE .Java Web编程.对Android应用开发与平台开发有较深入研究.从基础到高级的课程中,授课风格深受学员的喜爱. Android L版本中新增了RecyclerView,用于显示复杂视图的新增Widget. 一.RecyclerView 替代ListView的RecyclerView使ViewHolder标准化

Android 新控件RecyclerView

RecyclerView 是 android-support-v7-21 版本中新增的一个 Widgets.我们可以使用RecyclerView非常简单的实现横向竖向的ListView.GridView.以及瀑布流的效果.先看一下项目的实现的效果 是不是很炫?使用RecyclerView可以非常简单的实现. RecyclerView 特性 1.不关心Item是否显示在正确的位置以及如何显示 2.不关心item之间如何分割 3.不关注Item增加与删除的动画效果 4.它仅仅只是关注如何回收和复用i

Android5.x 新控件之RecyclerView,CardView,Palette的使用

『转载注明出处:http://blog.csdn.net/feiduclear_up/article/details/46439005 CSDN废墟的树』 自Android5.0发布以来,谷歌推出全新的Material Desigen设计风格,时过一年多了,在国内也看到很多应用在慢 慢适应MD设计风格.其中比较好的app就是网易新闻客户端了,其设计风格基本符合MD要求.鉴于越来多App采 用MD设计风格,作为吊丝程序员的我们怎能落后呢?那就让我们来学习一些Android5.x新推出的一些控件吧.

Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用

[转载请注明出处:http://blog.csdn.net/feiduclear_up/article/details/46514791 CSDN 废墟的树] 上一篇博客我们学习了Android Design Support Library库中的 是个简单的组件,不了解的童鞋可以参考之前的博客 Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用. 这篇博客我们继续学习Design库中的其他四个组件,分别是

一个Activity掌握Design新控件

一个Activity掌握Design新控件 欢迎转载,转载请注明原文地址:http://blog.csdn.net/lavor_zl/article/details/51295364谢谢. 谷歌在推出Android5.0的同时推出了全新的设计Material Design,谷歌为了给我们提供更加规范的MD设计风格的控件,在2015年IO大会上推出了Design支持包,Design常用的新控件有下面8种. 1. TextInputLayout(文本输入布局) TextInputLayout的作用是

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

Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用 分类: Android UI2015-06-15 16:44 1145人阅读 评论(5) 收藏 举报 MaterialDesingsupportlibrary 目录(?)[-] 前提 FloatingActionButton TextInputLayout Snackbar的使用 TabLayout [转载请注明出处:http://blog.csdn.n