文档章节

【学习笔记】RecyclerView的使用

2tman
 2tman
发布于 2015/06/26 10:08
字数 915
阅读 305
收藏 4
为什么叫做RecyclerView?
  • 不关心Item是否显示在正确的位置,如何显示。(LayoutManager)
  • 不关心Item间如何分隔。(ItemDecoration)
  • 不关心Item增加与删除的动画效果。(ItemAnimator)
  • 仅仅关注如何回收与复用View。

RecylerView相关的重要类


  • Adapter
  • ViewHolder
  • LayoutManager
  • ItemDecoration
  • ItemAnimator

RecylerView能干什么?


  • Just like ListView
  • Just like GridView
  • 横向 ListView
  • 横向 GridView
  • 瀑布流
  • 定制Item增加与删除动画(ItemAnimator)
  • 需要手动为Adapter提供Item Click/longClick的回调

RecyclerView关于ListView的实现代码:

package cn.jiarong.myrecyclerview;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ActionBarActivity {

    private RecyclerView mRecyclerView;
    private List<String> mDatas;
    private MyAdapter adapter;

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

        initDatas();

        initViews();
        adapter = new MyAdapter(this, mDatas);
        mRecyclerView.setAdapter(adapter);

        //设置RecyclerView的布局管理
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置RecyclerView的item的分割线
        //mRecyclerView.addItemDecoration(new DividerItemDecoration(this,
                //DividerItemDecoration.VERTICAL_LIST));

        adapter.setmOnItemClickListner(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int pos) {

                Toast.makeText(MainActivity.this, pos +" click:",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View v, int pos) {
                Toast.makeText(MainActivity.this, pos +" long click:",Toast.LENGTH_SHORT).show();
            }
        });
    }

    //加载视图
    private void initViews(){
        mRecyclerView = (RecyclerView)findViewById(R.id.mRecyclerView);
    }

    //初始化数据
    private void initDatas(){
        mDatas = new ArrayList<String>();
        for(int i='A';i<='z';i++){
            mDatas.add(""+(char)i);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch (id){
            case R.id.action_add:
                adapter.addData(1, "insert a");
                break;
            case R.id.action_delete:
                adapter.deleteData(1);
                break;
            case R.id.action_listview:
                mRecyclerView.setLayoutManager(new LinearLayoutManager(this,
                        LinearLayoutManager.VERTICAL, false));

                break;
            case R.id.action_gridview:
                mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
                break;
            case R.id.action_hor_gridview:
                mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(5,
                        StaggeredGridLayoutManager.HORIZONTAL));
                break;
            case R.id.action_staggered:
                Intent intent = new Intent(this, StaggedActivity.class);
                startActivity(intent);
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}



package cn.jiarong.myrecyclerview;

import android.content.Context;
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.TextView;

import java.util.List;

/**
 * Created by jiaronggeng on 15/6/25.
 */
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

    private LayoutInflater mInflater;
    private Context mContext;
    protected List<String> mDatas;

    public interface OnItemClickListener{
        public void onItemClick(View v, int pos);
        public void onItemLongClick(View v, int pos);
    }

    private OnItemClickListener mOnItemClickListner;

    public void setmOnItemClickListner(OnItemClickListener mOnItemClickListner) {
        this.mOnItemClickListner = mOnItemClickListner;
    }

    public void addData(int pos, String str){
        this.mDatas.add(pos, str);
        this.notifyItemInserted(1);
    }

    public void deleteData(int pos){
        this.mDatas.remove(pos);
        this.notifyItemRemoved(1);
    }

    public MyAdapter(Context context, List<String> datas){
        this.mContext = context;
        this.mDatas = datas;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder myViewHolder, final int i) {

        myViewHolder.m_tv.setText(mDatas.get(i));
        setUpEvent(myViewHolder);

    }

    protected void setUpEvent(final MyViewHolder myViewHolder){
        if (mOnItemClickListner != null) {
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int pos = myViewHolder.getLayoutPosition();
                    mOnItemClickListner.onItemClick(myViewHolder.itemView, pos);
                }

            });

            myViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    int pos = myViewHolder.getLayoutPosition();
                    mOnItemClickListner.onItemLongClick(myViewHolder.itemView, pos);

                    return false;
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item, viewGroup, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }
}

class MyViewHolder extends ViewHolder{

    TextView m_tv;

    public MyViewHolder(View itemView) {
        super(itemView);
        m_tv = (TextView)itemView.findViewById(R.id.m_tv);

    }
}



瀑布流

package cn.jiarong.myrecyclerview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import java.util.ArrayList;
import java.util.List;


public class StaggedActivity extends Activity {

    private RecyclerView mRecyclerView;
    private List<String> mDatas;
    private StaggedAdapter adapter;

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

        initDatas();

        initViews();
        adapter = new StaggedAdapter(this, mDatas);
        mRecyclerView.setAdapter(adapter);

        //设置RecyclerView的布局管理
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,
                StaggeredGridLayoutManager.VERTICAL));

        adapter.setmOnItemClickListner(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int pos) {

            }

            @Override
            public void onItemLongClick(View v, int pos) {
                //长按删除
                adapter.deleteData(pos);
            }
        });

    }

    //加载视图
    private void initViews(){
        mRecyclerView = (RecyclerView)findViewById(R.id.mRecyclerView);
    }

    //初始化数据
    private void initDatas(){
        mDatas = new ArrayList<String>();
        for(int i='A';i<='z';i++){
            mDatas.add(""+(char)i);
        }
    }


}

package cn.jiarong.myrecyclerview;

import android.content.Context;
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.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiaronggeng on 15/6/25.
 */
public class StaggedAdapter extends MyAdapter {

    private List<Integer> listHeight;

    public StaggedAdapter(Context context, List<String> datas){
        super(context, datas);

        listHeight = new ArrayList<Integer>();
        for(int i=0;i<mDatas.size();i++){
            listHeight.add((int)(100+Math.random()*300));
        }
    }

    @Override
    public void onBindViewHolder(MyViewHolder myViewHolder, int position) {

        ViewGroup.LayoutParams lp = myViewHolder.itemView.getLayoutParams();
        lp.height = listHeight.get(position);
        myViewHolder.itemView.setLayoutParams(lp);
        myViewHolder.m_tv.setText(mDatas.get(position));

        setUpEvent(myViewHolder);
    }


}






© 著作权归作者所有

共有 人打赏支持
2tman
粉丝 12
博文 115
码字总数 31481
作品 0
苏州
Android工程师
android之recyclerview的基本使用

Google在推出Android5.0的时候推出了Material Design ,而RecyclerView是Material Design 重要组件之一。当然还有CardView/Palette。 RecyclerView是用来替换传统的ListView和GridView,瀑布流...

墨宇hz
2016/03/21
0
0
赵彦军/RecyclerView

RecyclerView RecyclerView不再负责显示工作,用法比ListView更解耦。和ListView不一样的是,RecyclerView不再负责Item的摆放等显示方面的功能。所有和布局、绘制等方面的工作Google都其拆分成...

赵彦军
2016/12/02
0
0
Android控件RecyclerView和ListView的异同

在我的一篇介绍Android新控件RecyclerView的博客( Android L新控件RecyclerView简介 )中,一个读者留言说RecyclerView跟ListView之间好像没有什么不同,我觉得这是一个好问题,应该明确地区...

小克898
2016/08/16
27
0
Android进阶——框架打造之RecyclerView框架

前言 RecyclerView已经完全替代了ListView的开发,其强大之处在于性能、内存、使用、灵活、丰富的API、动画、事件处理上都可以完美的体现出来,对于开发者来说RecyclerView是必须精通的一关,...

qq_30379689
01/07
0
0
RecyclerView实现拖动排序和滑动删除功能

RecyclerView 的拖动排序需要借助一下 ItemTouchHelper 这个类,ItemTouchHelper 类是 Google 提供的一个支持 RecyclerView 滑动和拖动的一个使用类,下面使用该类实现 RecyclerView 的滑动删...

jzman
05/12
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

Spark core基础

Spark RDD的五大特性 RDD是由一系列的Partition组成的,如果Spark计算的数据是在HDFS上那么partition个数是与block数一致(大多数情况) RDD是有一系列的依赖关系,有利于Spark计算的容错 RDD中每...

张泽立
36分钟前
0
0
如何搭建Keepalived+Nginx+Tomcat高可用负载均衡架构

一.概述 初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最简单的部署方式,但是随着业务的不断扩大,系统的访问量逐渐的上升,单机部署的模式已...

Java大蜗牛
50分钟前
0
0
ffplay 播放 rtsp地址

D:\ffmpeg\ffmpeg\bin ffplay.exe 播放rtsp地址: ffplay.exe -rtsp_transport tcp "rtsp://ip:port/pag://172.16.0.11:7302:33000000001310001748:1:SUB:TCP?cnid=2&pnid=2&auth=50&streamf......

yizhichao
56分钟前
3
0
[Jekyll] permalink -- 修改文章的链接地址

在我的个人博客中访问这篇文章时,你有注意到地址栏中显示的地址吗?它应该是这样的: https://cometeme.github.io/jekyll/2018/08/Jekyll-为文章设置永久链接.html 。打开其他的文章,可以看...

cometeme
57分钟前
0
0
启动mango数据库批处理

::启动mango数据库set dbdir=c:\mongodb\dataif not exist %dbdir% md %dbdir%c:cd c:\mongodbmongod.exe --dbpath %dbdir%...

江湖令
今天
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部