RecyclerView详细了解

关于RecyclerView大家都不陌生了,它的使用也越来越受欢迎,现在总体了解一下RecyclerView的作用,为什么会有RecyclerView呢,我用ListView也能干所有的事情啊,尺有所短,寸有所长,先来看看RecyclerView的优点吧
  1. 可以快速实现gallery 效果。
  2. 可以快速实现瀑布流效果。
  3. 可以方便地为Item添加动画效果。

好吧,看到这些对RecyclerView的强大应该有一些认识了吧,再看看使用RecyclerView会遇到哪些麻烦

1.  没有为Item提供点击事件。
2.  没有为Item提供分割线。

先来实现一个简单的RecyclerView–

在grade里引入support包

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:recyclerview-v7:+'

MainActivity

public class MainActivity extends AppCompatActivity {private RecyclerView mRecyclerView;private List<String> mListData;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initData();mRecyclerView = (RecyclerView) findViewById(R.id.rl_list);mRecyclerView.setLayoutManager(new LinearLayoutManager(this));mRecyclerView.setAdapter(new RecyclerAdapter(this,mListData));}private void initData() {mListData = new ArrayList<String>();for (int i = 0; i < 100; i++) {mListData.add("我是第" + i + "行");}}}

RecyclerAdapter

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.TestViewHolder> {private Context mContext;private List<String> mList;public RecyclerAdapter(Context context, List<String> list) {mContext = context;mList = list;}@Overridepublic TestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {TestViewHolder holder = new TestViewHolder(LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item_layout, parent, false));return holder;}@Overridepublic void onBindViewHolder(TestViewHolder holder, int position) {holder.tv.setText(mList.get(position));}@Overridepublic int getItemCount() {return mList.size();}class TestViewHolder extends RecyclerView.ViewHolder {
;        TextView tv;public TestViewHolder(View view) {super(view);tv = (TextView) view.findViewById(R.id.tv_number);}}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.farmlink.myrecyclerview.MainActivity"><android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/rl_list" />
</RelativeLayout>

看一下效果图
这里写图片描述

没有分割线很糟糕,不过RecycleView 给你提供了绘制分割线的方法

public class DividerItemDecoration extends RecyclerView.ItemDecoration {private static final int[] ATTRS = new int[]{android.R.attr.listDivider};public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;public static  final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;private Drawable mDivider;private int mOrientation;public DividerItemDecoration(Context context, int orientation) {final TypedArray a = context.obtainStyledAttributes(ATTRS);mDivider = a.getDrawable(0);a.recycle();setOrientation(orientation);}public void setOrientation(int orientation) {if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {throw new IllegalArgumentException("invalid orientation");}mOrientation = orientation;}@Overridepublic void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {super.onDrawOver(c, parent, state);if (mOrientation == VERTICAL_LIST) {drawVertical(c, parent);} else {drawHorizontal(c, parent);}}public void drawVertical(Canvas c, RecyclerView parent) {final int left = parent.getPaddingLeft();final int right = parent.getWidth() - parent.getPaddingRight();final int childCount = parent.getChildCount();for (int i = 0; i < childCount; i ++) {final View child = parent.getChildAt(i);final RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams)child.getLayoutParams();final int top = child.getBottom() + layoutParams.bottomMargin;final int bottom = top + mDivider.getIntrinsicHeight();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}public void drawHorizontal(Canvas c, RecyclerView parent) {final int top = parent.getPaddingTop();final int bottom = parent.getHeight() - parent.getPaddingBottom();final int childCount = parent.getChildCount();for (int i = 0; i < childCount; i++) {final View child = parent.getChildAt(i);final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();final int left = child.getRight() + params.rightMargin;final int right = left + mDivider.getIntrinsicHeight();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {super.getItemOffsets(outRect, view, parent, state);if (mOrientation == VERTICAL_LIST) {outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());} else {outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);}}
}

加上这句就ok了

mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

这里写图片描述
关于RecyclerView 先了解到这里。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389157.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

案例与案例之间的非常规排版

In 1929 the Cond Nast publishing group brought Russian-born Mehemed Fehmy Agha—who had been working for the German edition of Vogue magazine—to America as art director for House & Garden, Vanity Fair, and the senior edition of Vogue.1929年&#xff0c…

熊猫分发_熊猫新手:第二部分

熊猫分发This article is a continuation of a previous article which kick-started the journey to learning Python for data analysis. You can check out the previous article here: Pandas for Newbies: An Introduction Part I.本文是上一篇文章的延续&#xff0c;该文…

浅析微信支付:申请退款、退款回调接口、查询退款

本文是【浅析微信支付】系列文章的第八篇&#xff0c;主要讲解商户如何处理微信申请退款、退款回调、查询退款接口&#xff0c;其中有一些坑的地方&#xff0c;会着重强调。 浅析微信支付系列已经更新七篇了哟&#xff5e;&#xff0c;没有看过的朋友们可以看一下哦。 浅析微信…

view工作原理-计算视图大小的过程(onMeasure)

view的视图有两种情况&#xff1a; 内容型视图&#xff1a;由视图的内容决定其大小。图形型视图&#xff1a;父视图为view动态调整大小。 ### measure的本质 把视图布局使用的“相对值”转化成具体值的过程&#xff0c;即把WRAP_CONTENT,MATCH_PARENT转化为具体的值。 measur…

[转载]使用.net 2003中的ngen.exe编译.net程序

ngen.exe程序为.net 2003自带&#xff0c; 在&#xff1a;/windows/microsoft.net/framework/v1.1.4322目录下ngen.exe ngen能把.net框架的东西编译成机器码.... 网友&#xff1a;Visual Studio .NET 2003程序的运行速度怎么样&#xff0c;我有一个感觉&#xff0c;V…

基于Redis实现分布式锁实战

背景在很多互联网产品应用中&#xff0c;有些场景需要加锁处理&#xff0c;比如&#xff1a;秒杀&#xff0c;全局递增ID&#xff0c;楼层生成等等。大部分的解决方案是基于DB实现的&#xff0c;Redis为单进程单线程模式&#xff0c;采用队列模式将并发访问变成串行访问&#x…

数据分析 绩效_如何在绩效改善中使用数据分析

数据分析 绩效Imagine you need to do a bank transaction, but the website is so slow. The page takes so much time to load, all you can see is a blue circle.想象您需要进行银行交易&#xff0c;但是网站是如此缓慢。 该页面需要花费很多时间来加载&#xff0c;您只能看…

隐私策略_隐私图标

隐私策略During its 2020 Worldwide Developers Conference, Apple spent time on one of today’s hottest topics — privacy. During the past couple of years, Apple has been rolling out various public campaigns aiming to position itself as a company that respect…

Java中的数组

一、数组的定义type[] arrayName;type arrayName[]; 推荐第一种 二、数组的初始化 含义&#xff1a;所谓的初始化&#xff0c;就是为数组的数组元素分配内存空间&#xff0c;并为每个数组元素赋初始值 &#xff08;1&#xff09;静态初始化&#xff1a;arrayName new type[…

您一直在寻找5+个简单的一线工具来提升Python可视化效果

Insightful and aesthetic visualizations don’t have to be a pain to create. This article will prevent 5 simple one-liners you can add to your code to increase its style and informational value.富有洞察力和美学的可视化不必费心创建。 本文将防止您添加到代码中…

用C#编写的代码经C#编译器后,并非生成本地代码而是生成托管代码

用C#编写的代码经C#编译器后&#xff0c;并非生成本地代码而是生成托管代码。也就是说&#xff0c;程序集在打包时是连同CLR一起打包的。在客户端的机器上&#xff0c;CLR一行行的读取IL&#xff0c;在读取每行IL时&#xff0c;CLR利用JIT编译器将IL编译成本地的CPU指令。若要节…

figma 安装插件_彩色滤光片Figma插件,用于色盲

figma 安装插件So as a UX Designer, it is important to design with disabilities in mind. One of these is color blindness. It is important to make sure important information on your product is legible to everyone. This is why I like using this tool:因此&…

服务器运维

1.服务器和网站漏洞检测&#xff0c;对Web漏洞、弱口令、潜在的恶意行为、违法信息等进行定期扫描&#xff1b;代码的定期检查&#xff0c;漏洞检查及服务器安全加固 2.服务器数据备份&#xff0c;包括网站程序文件备份&#xff0c;数据库文件备份、配置文件备份&#xff0c;如…

产品观念:更好的捕鼠器_故事很重要:为什么您需要成为更好的讲故事的人

产品观念&#xff1a;更好的捕鼠器重点 (Top highlight)Telling a compelling story helps you get your point across effectively else you get lost in translation.讲一个引人入胜的故事可以帮助您有效地传达观点&#xff0c;否则您会迷失在翻译中。 Great stories happen…

7月15号day7总结

今天复习了springMVC的框架搭建。 思维导图&#xff1a; 转载于:https://www.cnblogs.com/kangy123/p/9315919.html

关于注意力的问题

问题&#xff1a;一旦持续的注意力分散和精力无法集中成为习惯性动作&#xff0c;这将成为一个严重的问题。 实质&#xff1a;加强有意识的集中程度和持续时间&#xff0c;尽量避免无意识注意对大脑的干扰。 不要浪费注意力。大脑以天为周期&#xff0c;每天注意力是有限的。T…

设计师的10种范式转变

For $250, a business can pay a graphic designer to create a logo for their business. Or, for $10,000 a business can hire a graphic designer to form a design strategy that contextually places the business’s branding in a stronghold against the market it’s…

面向Tableau开发人员的Python简要介绍(第2部分)

用PYTHON探索数据 (EXPLORING DATA WITH PYTHON) And we’re back! Let’s pick up where we left off in the first article of this series and use the visual we built there as a starting point.我们回来了&#xff01; 让我们从在本系列的第一篇文章中停下来的地方开始&…

GAC中的所有的Assembly都会存放在系统目录%winroot%/assembly下面

是的&#xff0c;GAC中的所有的Assembly都会存放在系统目录"%winroot%/assembly下面。放在系统目录下的好处之一是可以让系统管理员通过用户权限来控制Assembly的访问。 关于GAC本身&#xff0c;上面redcaff_l所引述的一段话正是MSDN中对GAC的定义。GAC全称是Global A…

Mysql(三) Mysq慢查询日志

Mysql Slow Query Log MYSQL慢查询日志是用来记录执行时间超过指定时间的查询语句。通过慢查询日志&#xff0c;可以查找出哪些查询语句的执行效率很低&#xff0c;以便进行优化。一般建议开启&#xff0c;它对服务器性能的影响微乎其微&#xff0c;但是可以记录mysql服务器上执…