在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

  1. package com.jiatu.recyclerviewtest;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.content.res.TypedArray;
  8. import android.graphics.Canvas;
  9. import android.graphics.Rect;
  10. import android.graphics.drawable.Drawable;
  11. import android.os.Bundle;
  12. import android.support.v7.widget.DefaultItemAnimator;
  13. import android.support.v7.widget.LinearLayoutManager;
  14. import android.support.v7.widget.RecyclerView;
  15. import android.view.LayoutInflater;
  16. import android.view.View;
  17. import android.view.ViewGroup;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. public class MainActivity extends Activity {
  22. private RecyclerView recyclerView;
  23. private List<Person> list = new ArrayList<Person>();
  24. private LinearLayoutManager mLayoutManager;
  25. // private GridLayoutManager mGridLayoutManager;
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31. recyclerView = (RecyclerView) findViewById(R.id.recycler_view_test_rv);
  32. recyclerView.setHasFixedSize(true); // 固定item的高或者宽,提高性能
  33. recyclerView.setItemAnimator(new DefaultItemAnimator());
  34. recyclerView.addItemDecoration(new MyDecoration(this, MyDecoration.VERTICAL_LIST));
  35.  
  36. mLayoutManager = new LinearLayoutManager(this);
  37. // 设置list的方向,默认是垂直
  38. // mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
  39.  
  40. // 5,spanCount:列数
  41. // mGridLayoutManager = new GridLayoutManager(this, 5);
  42.  
  43. recyclerView.setLayoutManager(mLayoutManager);
  44.  
  45. initData();
  46. setPersonAdapter();
  47.  
  48. }
  49.  
  50. /**
  51. * 设置适配器
  52. */
  53. private void setPersonAdapter() {
  54. final PersonAdapter adapter = new PersonAdapter(this, list);
  55. adapter.setOnRecyclerViewListener(new OnRecyclerViewListener() {
  56.  
  57. @Override
  58. public boolean onItemLongClick(int position) {
  59. adapter.addData(position);
  60. Toast.makeText(MainActivity.this, "吼吼吼吼吼", Toast.LENGTH_SHORT).show();
  61. return true;
  62. }
  63.  
  64. @Override
  65. public void onItemClick(int position) {
  66. adapter.removeData(position);
  67. Toast.makeText(MainActivity.this, "哈哈哈哈哈", Toast.LENGTH_SHORT).show();
  68. }
  69. });
  70.  
  71. recyclerView.setAdapter(adapter);
  72. }
  73.  
  74. /**
  75. * RecyclerView的单击和长按事件监听
  76. *
  77. * @author fans created on 2016年11月3日
  78. *
  79. */
  80. public interface OnRecyclerViewListener {
  81. void onItemClick(int position);
  82.  
  83. boolean onItemLongClick(int position);
  84. }
  85.  
  86. /**
  87. * 适配器
  88. *
  89. * @author fans created on 2016年11月3日
  90. *
  91. */
  92. class PersonAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  93.  
  94. private OnRecyclerViewListener onRecyclerViewListener;
  95.  
  96. public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
  97. this.onRecyclerViewListener = onRecyclerViewListener;
  98. }
  99.  
  100. private List<Person> list = new ArrayList<Person>();
  101. private Context context;
  102.  
  103. public PersonAdapter(Context context, List<Person> list) {
  104. this.context = context;
  105. this.list = list;
  106. }
  107.  
  108. /**
  109. * 增加一个item
  110. *
  111. * @param position
  112. */
  113. public void addData(int position) {
  114. list.add(position, new Person("fans+", 23));
  115. notifyItemInserted(position);
  116. notifyItemRangeChanged(position, list.size());
  117. }
  118.  
  119. /**
  120. * 删减一个item
  121. *
  122. * @param position
  123. */
  124. public void removeData(int position) {
  125. list.remove(position);
  126. notifyDataSetChanged();
  127. notifyItemRemoved(position);
  128. notifyItemRangeChanged(position, list.size());
  129. }
  130.  
  131. @Override
  132. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
  133. View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
  134. return new PersonViewHolder(view);
  135. }
  136.  
  137. @Override
  138. public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
  139. PersonViewHolder holder = (PersonViewHolder) viewHolder;
  140. holder.position = i;
  141. Person person = list.get(i);
  142. holder.tv.setText(person.name);
  143.  
  144. }
  145.  
  146. @Override
  147. public int getItemCount() {
  148. return list.size();
  149. }
  150.  
  151. /**
  152. * 自定义holder
  153. *
  154. * @author fans created on 2016年11月4日
  155. *
  156. */
  157. class PersonViewHolder extends RecyclerView.ViewHolder
  158. implements View.OnClickListener, View.OnLongClickListener {
  159. public View rootView;
  160. TextView tv;
  161. public int position;
  162.  
  163. public PersonViewHolder(View itemView) {
  164. super(itemView);
  165. tv = (TextView) itemView.findViewById(R.id.list_item);
  166. rootView = itemView.findViewById(R.id.recycler_view_test_item_person_view);
  167. rootView.setOnClickListener(this);
  168. rootView.setOnLongClickListener(this);
  169. }
  170.  
  171. @Override
  172. public void onClick(View v) {
  173. if (null != onRecyclerViewListener) {
  174. onRecyclerViewListener.onItemClick(position);
  175. }
  176. }
  177.  
  178. @Override
  179. public boolean onLongClick(View v) {
  180. if (null != onRecyclerViewListener) {
  181. return onRecyclerViewListener.onItemLongClick(position);
  182. }
  183. return false;
  184. }
  185. }
  186.  
  187. }
  188.  
  189. /**
  190. * 填充数据
  191. */
  192. private void initData() {
  193. for (int i = 0; i < 10; i++) {
  194. list.add(new Person("fans" + i, 10 + i));
  195. }
  196. }
  197.  
  198. class Person {
  199. public String name;
  200. public int age;
  201.  
  202. public Person(String name, int age) {
  203. super();
  204. this.name = name;
  205. this.age = age;
  206. }
  207.  
  208. }
  209.  
  210. /**
  211. * 自定义的分割线
  212. *
  213. * @author fans created on 2016年11月4日
  214. *
  215. */
  216. class MyDecoration extends RecyclerView.ItemDecoration {
  217.  
  218. private Context mContext;
  219. private Drawable mDivider;
  220. private int mOrientation;
  221. public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
  222. public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
  223.  
  224. // 我们通过获取系统属性中的listDivider来添加,在系统中的AppTheme中设置
  225. public final int[] ATRRS = new int[] { android.R.attr.listDivider };
  226.  
  227. public MyDecoration(Context context, int orientation) {
  228. this.mContext = context;
  229. final TypedArray ta = context.obtainStyledAttributes(ATRRS);
  230. this.mDivider = ta.getDrawable(0);
  231. ta.recycle();
  232. setOrientation(orientation);
  233. }
  234.  
  235. // 设置屏幕的方向
  236. public void setOrientation(int orientation) {
  237. if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
  238. throw new IllegalArgumentException("invalid orientation");
  239. }
  240. mOrientation = orientation;
  241. }
  242.  
  243. @Override
  244. public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
  245. if (mOrientation == HORIZONTAL_LIST) {
  246. drawVerticalLine(c, parent, state);
  247. } else {
  248. drawHorizontalLine(c, parent, state);
  249. }
  250. }
  251.  
  252. // 画横线, 这里的parent其实是显示在屏幕显示的这部分
  253. public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
  254. int left = parent.getPaddingLeft();
  255. int right = parent.getWidth() - parent.getPaddingRight();
  256. final int childCount = parent.getChildCount();
  257. for (int i = 0; i < childCount; i++) {
  258. final View child = parent.getChildAt(i);
  259.  
  260. // 获得child的布局信息
  261. final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
  262. final int top = child.getBottom() + params.bottomMargin;
  263. final int bottom = top + mDivider.getIntrinsicHeight();
  264. mDivider.setBounds(left, top, right, bottom);
  265. mDivider.draw(c);
  266. // Log.d("wnw", left + " " + top + " "+right+" "+bottom+" "+i);
  267. }
  268. }
  269.  
  270. // 画竖线
  271. public void drawVerticalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
  272. int top = parent.getPaddingTop();
  273. int bottom = parent.getHeight() - parent.getPaddingBottom();
  274. final int childCount = parent.getChildCount();
  275. for (int i = 0; i < childCount; i++) {
  276. final View child = parent.getChildAt(i);
  277.  
  278. // 获得child的布局信息
  279. final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
  280. final int left = child.getRight() + params.rightMargin;
  281. final int right = left + mDivider.getIntrinsicWidth();
  282. mDivider.setBounds(left, top, right, bottom);
  283. mDivider.draw(c);
  284. }
  285. }
  286.  
  287. // 由于Divider也有长宽高,每一个Item需要向下或者向右偏移
  288. @Override
  289. public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
  290. if (mOrientation == HORIZONTAL_LIST) {
  291. // 画横线,就是往下偏移一个分割线的高度
  292. outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
  293. } else {
  294. // 画竖线,就是往右偏移一个分割线的宽度
  295. outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
  296. }
  297. }
  298. }
  299. }

layout

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context="${relativePackage}.${activityClass}" >
  6.  
  7. <android.support.v7.widget.RecyclerView
  8. android:id="@+id/recycler_view_test_rv"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:background="#bbccaa"
  12. android:scrollbars="vertical" />
  13.  
  14. </RelativeLayout>

item

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/recycler_view_test_item_person_view"
  4. android:layout_width="match_parent"
  5. android:layout_height="50dp"
  6. android:layout_margin="16sp"
  7. android:orientation="vertical" >
  8.  
  9. <TextView
  10. android:id="@+id/list_item"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:background="#08da1d"
  14. android:gravity="center"
  15. android:textColor="#f7f4f7"
  16. android:textSize="20sp" />
  17.  
  18. </LinearLayout>

RecycleView在eclipse的初体验的更多相关文章

  1. Spring之初体验

                                     Spring之初体验 Spring是一个轻量级的Java Web开发框架,以IoC(Inverse of Control 控制反转)和 ...

  2. 在同一个硬盘上安装多个 Linux 发行版及 Fedora 21 、Fedora 22 初体验

    在同一个硬盘上安装多个 Linux 发行版 以前对多个 Linux 发行版的折腾主要是在虚拟机上完成.我的桌面电脑性能比较强大,玩玩虚拟机没啥问题,但是笔记本电脑就不行了.要在我的笔记本电脑上折腾多个 ...

  3. protobuf初体验

    概念介绍 Protocol buffers 是google公司的与语言无关.与平台无关的.可扩张的为序列化话结构数据,就像xml一样,办事更加的小巧.快速.简单.Protocol buffers 目前 ...

  4. OPhone SDK初体验

    OPhone SDK初体验 write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 背景说明 中国伟大的垄断龙头,世界上也是顶尖的中移动最 ...

  5. ipython及Python初体验

    阅读目录: Python环境体验 Python编辑器 ipython安装 Python提示符 Python初体验 print和变量 变量操作 内建函数:方法 数学运算:简单算术.随机数 关于模块 一. ...

  6. Python基础学习参考(一):python初体验

    一.前期准备 对于python的学习,首先的有一个硬件电脑,软件python的运行环境.说了一句废话,对于很多初学者而言,安装运行环境配置环境变量的什么的各种头疼,常常在第一步就被卡死了,对于pyth ...

  7. 屌丝就爱尝鲜头——java8初体验

    Java8已经推出,让我们看看他的魅力.让我们看看他改变较大的部分. 一.java8概述 Java8是由Oracle(甲骨文)公司与2014年3月27日正式推出的.Java8同时推出有3套语言系统,分 ...

  8. .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验

    不知不觉,“.NET平台开源项目速览“系列文章已经15篇了,每一篇都非常受欢迎,可能技术水平不高,但足够入门了.虽然工作很忙,但还是会抽空把自己知道的,已经平时遇到的好的开源项目分享出来.今天就给大家 ...

  9. Xamarin+Prism开发详解四:简单Mac OS 虚拟机安装方法与Visual Studio for Mac 初体验

    Mac OS 虚拟机安装方法 最近把自己的电脑升级了一下SSD固态硬盘,总算是有容量安装Mac 虚拟机了!经过心碎的安装探索,尝试了国内外的各种安装方法,最后在youtube上找到了一个好方法. 简单 ...

随机推荐

  1. ios隐藏键盘

    1.点击页面空白处隐藏键盘 给viewController里面复写-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event方法,在 ...

  2. EF 自测例子

    public ActionResult Test()        { using (MvcShoppingContext db = new MvcShoppingContext())         ...

  3. 虚拟机Ubuntu16,caffe环境搭建

    虚拟机下的Ubuntu16.04+caffe+onlycup 官网的step很重要,要跟着官网,的步骤来:http://caffe.berkeleyvision.org/installation.ht ...

  4. time.h-------日期与时间函数

    1.clock函数----返回CPU计时单元.函数返回开启这个程序进程到程序中调用clock函数时之间的CPU时钟计时单元(返回毫秒). (计的是占用cpu的时间) 函数原型:long clock() ...

  5. [译] Python 3.5 协程究竟是个啥

    转自:http://blog.rainy.im/2016/03/10/how-the-heck-does-async-await-work-in-python-3-5/ [译] Python 3.5 ...

  6. IIS配置文件路径

    C:\Windows\System32\inetsrv\config\applicationHost.config

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

  8. POS管理系统之供应商新增

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  9. SSL协议(HTTPS) 握手、工作流程详解(双向HTTPS流程)

    原文地址:http://www.cnblogs.com/jifeng/archive/2010/11/30/1891779.html SSL协议的工作流程: 服务器认证阶段:1)客户端向服务器发送一个 ...

  10. NHibernate系列文章十:NHibernate对象二级缓存下

    摘要 上一节对NHibernate二级缓存做了简单介绍,NHibernate二级缓存是由SessionFactory管理的,所有Session共享.这一节介绍二级缓存其他两个方面:二级缓存查询和二级缓 ...