RecycleView在eclipse的初体验
在sdk中找到v7包
\sdk\extras\android\support\v7\recyclerview
导入工程
Import\Android\Existing Android Code Into Workspace
将导入的recycleview作为依赖库Library
工程上右键properties,勾选is Library,Apply,ok
将recycleview\libs\android_support_v7_recycleview.jar包复制粘贴到主工程的libs文件夹下
在layout中使用<android.support.v7.widget.RecyclerView/>标签
MainActivity
- package com.jiatu.recyclerviewtest;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.Canvas;
- import android.graphics.Rect;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.support.v7.widget.DefaultItemAnimator;
- 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;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private RecyclerView recyclerView;
- private List<Person> list = new ArrayList<Person>();
- private LinearLayoutManager mLayoutManager;
- // private GridLayoutManager mGridLayoutManager;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- recyclerView = (RecyclerView) findViewById(R.id.recycler_view_test_rv);
- recyclerView.setHasFixedSize(true); // 固定item的高或者宽,提高性能
- recyclerView.setItemAnimator(new DefaultItemAnimator());
- recyclerView.addItemDecoration(new MyDecoration(this, MyDecoration.VERTICAL_LIST));
- mLayoutManager = new LinearLayoutManager(this);
- // 设置list的方向,默认是垂直
- // mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
- // 5,spanCount:列数
- // mGridLayoutManager = new GridLayoutManager(this, 5);
- recyclerView.setLayoutManager(mLayoutManager);
- initData();
- setPersonAdapter();
- }
- /**
- * 设置适配器
- */
- private void setPersonAdapter() {
- final PersonAdapter adapter = new PersonAdapter(this, list);
- adapter.setOnRecyclerViewListener(new OnRecyclerViewListener() {
- @Override
- public boolean onItemLongClick(int position) {
- adapter.addData(position);
- Toast.makeText(MainActivity.this, "吼吼吼吼吼", Toast.LENGTH_SHORT).show();
- return true;
- }
- @Override
- public void onItemClick(int position) {
- adapter.removeData(position);
- Toast.makeText(MainActivity.this, "哈哈哈哈哈", Toast.LENGTH_SHORT).show();
- }
- });
- recyclerView.setAdapter(adapter);
- }
- /**
- * RecyclerView的单击和长按事件监听
- *
- * @author fans created on 2016年11月3日
- *
- */
- public interface OnRecyclerViewListener {
- void onItemClick(int position);
- boolean onItemLongClick(int position);
- }
- /**
- * 适配器
- *
- * @author fans created on 2016年11月3日
- *
- */
- class PersonAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
- private OnRecyclerViewListener onRecyclerViewListener;
- public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
- this.onRecyclerViewListener = onRecyclerViewListener;
- }
- private List<Person> list = new ArrayList<Person>();
- private Context context;
- public PersonAdapter(Context context, List<Person> list) {
- this.context = context;
- this.list = list;
- }
- /**
- * 增加一个item
- *
- * @param position
- */
- public void addData(int position) {
- list.add(position, new Person("fans+", 23));
- notifyItemInserted(position);
- notifyItemRangeChanged(position, list.size());
- }
- /**
- * 删减一个item
- *
- * @param position
- */
- public void removeData(int position) {
- list.remove(position);
- notifyDataSetChanged();
- notifyItemRemoved(position);
- notifyItemRangeChanged(position, list.size());
- }
- @Override
- public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
- View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
- return new PersonViewHolder(view);
- }
- @Override
- public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
- PersonViewHolder holder = (PersonViewHolder) viewHolder;
- holder.position = i;
- Person person = list.get(i);
- holder.tv.setText(person.name);
- }
- @Override
- public int getItemCount() {
- return list.size();
- }
- /**
- * 自定义holder
- *
- * @author fans created on 2016年11月4日
- *
- */
- class PersonViewHolder extends RecyclerView.ViewHolder
- implements View.OnClickListener, View.OnLongClickListener {
- public View rootView;
- TextView tv;
- public int position;
- public PersonViewHolder(View itemView) {
- super(itemView);
- tv = (TextView) itemView.findViewById(R.id.list_item);
- rootView = itemView.findViewById(R.id.recycler_view_test_item_person_view);
- rootView.setOnClickListener(this);
- rootView.setOnLongClickListener(this);
- }
- @Override
- public void onClick(View v) {
- if (null != onRecyclerViewListener) {
- onRecyclerViewListener.onItemClick(position);
- }
- }
- @Override
- public boolean onLongClick(View v) {
- if (null != onRecyclerViewListener) {
- return onRecyclerViewListener.onItemLongClick(position);
- }
- return false;
- }
- }
- }
- /**
- * 填充数据
- */
- private void initData() {
- for (int i = 0; i < 10; i++) {
- list.add(new Person("fans" + i, 10 + i));
- }
- }
- class Person {
- public String name;
- public int age;
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- }
- /**
- * 自定义的分割线
- *
- * @author fans created on 2016年11月4日
- *
- */
- class MyDecoration extends RecyclerView.ItemDecoration {
- private Context mContext;
- private Drawable mDivider;
- private int mOrientation;
- public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
- public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
- // 我们通过获取系统属性中的listDivider来添加,在系统中的AppTheme中设置
- public final int[] ATRRS = new int[] { android.R.attr.listDivider };
- public MyDecoration(Context context, int orientation) {
- this.mContext = context;
- final TypedArray ta = context.obtainStyledAttributes(ATRRS);
- this.mDivider = ta.getDrawable(0);
- ta.recycle();
- setOrientation(orientation);
- }
- // 设置屏幕的方向
- public void setOrientation(int orientation) {
- if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
- throw new IllegalArgumentException("invalid orientation");
- }
- mOrientation = orientation;
- }
- @Override
- public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
- if (mOrientation == HORIZONTAL_LIST) {
- drawVerticalLine(c, parent, state);
- } else {
- drawHorizontalLine(c, parent, state);
- }
- }
- // 画横线, 这里的parent其实是显示在屏幕显示的这部分
- public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
- int left = parent.getPaddingLeft();
- int right = parent.getWidth() - parent.getPaddingRight();
- final int childCount = parent.getChildCount();
- for (int i = 0; i < childCount; i++) {
- final View child = parent.getChildAt(i);
- // 获得child的布局信息
- final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
- final int top = child.getBottom() + params.bottomMargin;
- final int bottom = top + mDivider.getIntrinsicHeight();
- mDivider.setBounds(left, top, right, bottom);
- mDivider.draw(c);
- // Log.d("wnw", left + " " + top + " "+right+" "+bottom+" "+i);
- }
- }
- // 画竖线
- public void drawVerticalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
- int top = parent.getPaddingTop();
- int bottom = parent.getHeight() - parent.getPaddingBottom();
- final int childCount = parent.getChildCount();
- for (int i = 0; i < childCount; i++) {
- final View child = parent.getChildAt(i);
- // 获得child的布局信息
- final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
- final int left = child.getRight() + params.rightMargin;
- final int right = left + mDivider.getIntrinsicWidth();
- mDivider.setBounds(left, top, right, bottom);
- mDivider.draw(c);
- }
- }
- // 由于Divider也有长宽高,每一个Item需要向下或者向右偏移
- @Override
- public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
- if (mOrientation == HORIZONTAL_LIST) {
- // 画横线,就是往下偏移一个分割线的高度
- outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
- } else {
- // 画竖线,就是往右偏移一个分割线的宽度
- outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
- }
- }
- }
- }
layout
- <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}" >
- <android.support.v7.widget.RecyclerView
- android:id="@+id/recycler_view_test_rv"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#bbccaa"
- android:scrollbars="vertical" />
- </RelativeLayout>
item
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/recycler_view_test_item_person_view"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_margin="16sp"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/list_item"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#08da1d"
- android:gravity="center"
- android:textColor="#f7f4f7"
- android:textSize="20sp" />
- </LinearLayout>
RecycleView在eclipse的初体验的更多相关文章
- Spring之初体验
Spring之初体验 Spring是一个轻量级的Java Web开发框架,以IoC(Inverse of Control 控制反转)和 ...
- 在同一个硬盘上安装多个 Linux 发行版及 Fedora 21 、Fedora 22 初体验
在同一个硬盘上安装多个 Linux 发行版 以前对多个 Linux 发行版的折腾主要是在虚拟机上完成.我的桌面电脑性能比较强大,玩玩虚拟机没啥问题,但是笔记本电脑就不行了.要在我的笔记本电脑上折腾多个 ...
- protobuf初体验
概念介绍 Protocol buffers 是google公司的与语言无关.与平台无关的.可扩张的为序列化话结构数据,就像xml一样,办事更加的小巧.快速.简单.Protocol buffers 目前 ...
- OPhone SDK初体验
OPhone SDK初体验 write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 背景说明 中国伟大的垄断龙头,世界上也是顶尖的中移动最 ...
- ipython及Python初体验
阅读目录: Python环境体验 Python编辑器 ipython安装 Python提示符 Python初体验 print和变量 变量操作 内建函数:方法 数学运算:简单算术.随机数 关于模块 一. ...
- Python基础学习参考(一):python初体验
一.前期准备 对于python的学习,首先的有一个硬件电脑,软件python的运行环境.说了一句废话,对于很多初学者而言,安装运行环境配置环境变量的什么的各种头疼,常常在第一步就被卡死了,对于pyth ...
- 屌丝就爱尝鲜头——java8初体验
Java8已经推出,让我们看看他的魅力.让我们看看他改变较大的部分. 一.java8概述 Java8是由Oracle(甲骨文)公司与2014年3月27日正式推出的.Java8同时推出有3套语言系统,分 ...
- .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验
不知不觉,“.NET平台开源项目速览“系列文章已经15篇了,每一篇都非常受欢迎,可能技术水平不高,但足够入门了.虽然工作很忙,但还是会抽空把自己知道的,已经平时遇到的好的开源项目分享出来.今天就给大家 ...
- Xamarin+Prism开发详解四:简单Mac OS 虚拟机安装方法与Visual Studio for Mac 初体验
Mac OS 虚拟机安装方法 最近把自己的电脑升级了一下SSD固态硬盘,总算是有容量安装Mac 虚拟机了!经过心碎的安装探索,尝试了国内外的各种安装方法,最后在youtube上找到了一个好方法. 简单 ...
随机推荐
- ios隐藏键盘
1.点击页面空白处隐藏键盘 给viewController里面复写-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event方法,在 ...
- EF 自测例子
public ActionResult Test() { using (MvcShoppingContext db = new MvcShoppingContext()) ...
- 虚拟机Ubuntu16,caffe环境搭建
虚拟机下的Ubuntu16.04+caffe+onlycup 官网的step很重要,要跟着官网,的步骤来:http://caffe.berkeleyvision.org/installation.ht ...
- time.h-------日期与时间函数
1.clock函数----返回CPU计时单元.函数返回开启这个程序进程到程序中调用clock函数时之间的CPU时钟计时单元(返回毫秒). (计的是占用cpu的时间) 函数原型:long clock() ...
- [译] Python 3.5 协程究竟是个啥
转自:http://blog.rainy.im/2016/03/10/how-the-heck-does-async-await-work-in-python-3-5/ [译] Python 3.5 ...
- IIS配置文件路径
C:\Windows\System32\inetsrv\config\applicationHost.config
- Codeforces Round #229 (Div. 2) C
C. Inna and Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input stand ...
- POS管理系统之供应商新增
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- SSL协议(HTTPS) 握手、工作流程详解(双向HTTPS流程)
原文地址:http://www.cnblogs.com/jifeng/archive/2010/11/30/1891779.html SSL协议的工作流程: 服务器认证阶段:1)客户端向服务器发送一个 ...
- NHibernate系列文章十:NHibernate对象二级缓存下
摘要 上一节对NHibernate二级缓存做了简单介绍,NHibernate二级缓存是由SessionFactory管理的,所有Session共享.这一节介绍二级缓存其他两个方面:二级缓存查询和二级缓 ...