RecyclerView使用技巧(item动画及嵌套高度适配解决方案)
原文地址 · Frank-Zhu http://frank-zhu.github.io/android/2015/02/26/android-recyclerview-part-3/?utm_source=tuicool&utm_medium=referral
在上一篇(RecyclerView使用详解(二))文章中介绍了RecyclerView的多Item布局实现,接下来要来讲讲RecyclerView的Cursor实现,相较于之前的实现,Cursor有更多的使用场景,也更加的常用,特别是配合LoaderManager和CursorLoader进行数据的缓存及加载显示,基于此我们来重点看看RecyclerView的CursorAdapter具体要怎么实现。
一、CursorAdapter实现(配合LoaderManager和CursorLoader)
如果之前你用过ListView实现过此功能(CursorAdapter),那么你一定对下面这两个方法并不陌生
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return null;
} @Override
public void bindView(View view, Context context, Cursor cursor) { }
其中newView方法是用来创建Item布局的,bindView 方法是用来绑定当前View数据的,就相当于之前的getView方法拆成了两个方法实现。
如果你用RecyclerView,你会发现CursorAdapter这个类没有了,既然没有了,那我们就自己仿照着ListView的CursorAdapter类来实现,具体的代码没什么大的出入,无非就是注册两个观察者去监听数据库数据的变化,但是有两个地方需要注意一下,一个就是hasStableIds() 这个方法RecyclerView.Adapter中不能复写父类的方法,需要在初始化的时候调用setHasStableIds(true); 来完成相同功能,第二个就是notifyDataSetInvalidated() 这个方法没有,统一修改成notifyDataSetChanged() 方法即可。
 void init(Context context, Cursor c, int flags) {
         boolean cursorPresent = c != null;
         mCursor = c;
         mDataValid = cursorPresent;
         mContext = context;
         mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;
         if ((flags & FLAG_REGISTER_CONTENT_OBSERVER) == FLAG_REGISTER_CONTENT_OBSERVER) {
             mChangeObserver = new ChangeObserver();
             mDataSetObserver = new MyDataSetObserver();
         } else {
             mChangeObserver = null;
             mDataSetObserver = null;
         }
         if (cursorPresent) {
             if (mChangeObserver != null) c.registerContentObserver(mChangeObserver);
             if (mDataSetObserver != null) c.registerDataSetObserver(mDataSetObserver);
         }
         setHasStableIds(true);//这个地方要注意一下,需要将表关联ID设置为true
     }
     //************//
     public Cursor swapCursor(Cursor newCursor) {
         if (newCursor == mCursor) {
             return null;
         }
         Cursor oldCursor = mCursor;
         if (oldCursor != null) {
             if (mChangeObserver != null) oldCursor.unregisterContentObserver(mChangeObserver);
             if (mDataSetObserver != null) oldCursor.unregisterDataSetObserver(mDataSetObserver);
         }
         mCursor = newCursor;
         if (newCursor != null) {
             if (mChangeObserver != null) newCursor.registerContentObserver(mChangeObserver);
             if (mDataSetObserver != null) newCursor.registerDataSetObserver(mDataSetObserver);
             mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
             mDataValid = true;
             // notify the observers about the new cursor
             notifyDataSetChanged();
         } else {
             mRowIDColumn = -1;
             mDataValid = false;
             // notify the observers about the lack of a data set
             //There is no notifyDataSetInvalidated() method in RecyclerView.Adapter
             notifyDataSetChanged();//注意此处
         }
         return oldCursor;
     }
     //************//
     private class MyDataSetObserver extends DataSetObserver {
         @Override
         public void onChanged() {
             mDataValid = true;
             notifyDataSetChanged();
         }
         @Override
         public void onInvalidated() {
             mDataValid = false;
             //There is no notifyDataSetInvalidated() method in RecyclerView.Adapter
             notifyDataSetChanged();//注意此处
         }
     }
怎么样,是不是很简单,没错,就是这么简单,这里是完整的BaseAbstractRecycleCursorAdapter代码,用法和ListView的CursorAdapter用法一致,具体的可以看看我的Recyclerview LoaderManager Provider
二、Item的动画实现 RecyclerView本身就已经实现了ITEM的动画,只需要调用以下几个函数来增删Item即可出现默认动画。
notifyItemChanged(int)
notifyItemInserted(int)
notifyItemRemoved(int)
notifyItemRangeChanged(int, int)
notifyItemRangeInserted(int, int)
notifyItemRangeRemoved(int, int)
怎么样,是不是很轻松,如果你不满足系统默认动画,那么你可以自定义实现RecyclerView.ItemAnimator的接口方法,实现代码可以参考DefaultItemAnimator.当然,如果你不想自己实现,那么也没关系,这里有人已经写了开源库,你可以去看看recyclerview-animators,这里给出默认动画实现方式代码AnimFragment
三、嵌套RecycleView
一般是不推荐使用嵌套RecycleView的,和ListView是类似的,遇到这种需要嵌套的View一般都是想别的办法来规避,比如动态AddView,或者通过RecycleView的MultipleItemAdapter来实现,通过设置不同的ItemType布局不同的View,但是有时候会闲麻烦,想直接就用嵌套的方式来做,那么和ListView实现方式不同的是,ListView的实现一般都是继承ListView然后复写onMeasure方法,如下所示:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
但是需要特别注意的一点,RecycleView的实现方式不再是继承RecycleView来做,而是通过修改LayoutManager的方式,即通过继承LinearLayoutManager GridLayoutManagerStaggeredGridLayoutManager来修改子控件的测量,下面给出主要代码:
 private int[] mMeasuredDimension = new int[2];
     @Override
     public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
                           int widthSpec, int heightSpec) {
         final int widthMode = View.MeasureSpec.getMode(widthSpec);
         final int heightMode = View.MeasureSpec.getMode(heightSpec);
         final int widthSize = View.MeasureSpec.getSize(widthSpec);
         final int heightSize = View.MeasureSpec.getSize(heightSpec);
         Log.i(TAG, "onMeasure called. \nwidthMode " + widthMode
                 + " \nheightMode " + heightSpec
                 + " \nwidthSize " + widthSize
                 + " \nheightSize " + heightSize
                 + " \ngetItemCount() " + getItemCount());
         int width = 0;
         int height = 0;
         for (int i = 0; i < getItemCount(); i++) {
             measureScrapChild(recycler, i,
                     View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                     View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                     mMeasuredDimension);
             if (getOrientation() == HORIZONTAL) {
                 width = width + mMeasuredDimension[0];
                 if (i == 0) {
                     height = mMeasuredDimension[1];
                 }
             } else {
                 height = height + mMeasuredDimension[1];
                 if (i == 0) {
                     width = mMeasuredDimension[0];
                 }
             }
         }
         switch (widthMode) {
             case View.MeasureSpec.EXACTLY:
                 width = widthSize;
             case View.MeasureSpec.AT_MOST:
             case View.MeasureSpec.UNSPECIFIED:
         }
         switch (heightMode) {
             case View.MeasureSpec.EXACTLY:
                 height = heightSize;
             case View.MeasureSpec.AT_MOST:
             case View.MeasureSpec.UNSPECIFIED:
         }
         setMeasuredDimension(width, height);
     }
     private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                    int heightSpec, int[] measuredDimension) {
         try {
             View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException
             if (view != null) {
                 RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
                 int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                         getPaddingLeft() + getPaddingRight(), p.width);
                 int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
                         getPaddingTop() + getPaddingBottom(), p.height);
                 view.measure(childWidthSpec, childHeightSpec);
                 measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
                 measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
                 recycler.recycleView(view);
             }
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
         }
     }
 private int[] mMeasuredDimension = new int[2];
     @Override
     public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
         final int widthMode = View.MeasureSpec.getMode(widthSpec);
         final int heightMode = View.MeasureSpec.getMode(heightSpec);
         final int widthSize = View.MeasureSpec.getSize(widthSpec);
         final int heightSize = View.MeasureSpec.getSize(heightSpec);
         int width = 0;
         int height = 0;
         int count = getItemCount();
         int span = getSpanCount();
         for (int i = 0; i < count; i++) {
             measureScrapChild(recycler, i,
                     View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                     View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                     mMeasuredDimension);
             if (getOrientation() == HORIZONTAL) {
                 if (i % span == 0) {
                     width = width + mMeasuredDimension[0];
                 }
                 if (i == 0) {
                     height = mMeasuredDimension[1];
                 }
             } else {
                 if (i % span == 0) {
                     height = height + mMeasuredDimension[1];
                 }
                 if (i == 0) {
                     width = mMeasuredDimension[0];
                 }
             }
         }
         switch (widthMode) {
             case View.MeasureSpec.EXACTLY:
                 width = widthSize;
             case View.MeasureSpec.AT_MOST:
             case View.MeasureSpec.UNSPECIFIED:
         }
         switch (heightMode) {
             case View.MeasureSpec.EXACTLY:
                 height = heightSize;
             case View.MeasureSpec.AT_MOST:
             case View.MeasureSpec.UNSPECIFIED:
         }
         setMeasuredDimension(width, height);
     }
     private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                    int heightSpec, int[] measuredDimension) {
         if (position < getItemCount()) {
             try {
                 View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException
                 if (view != null) {
                     RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
                     int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                             getPaddingLeft() + getPaddingRight(), p.width);
                     int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
                             getPaddingTop() + getPaddingBottom(), p.height);
                     view.measure(childWidthSpec, childHeightSpec);
                     measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
                     measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
                     recycler.recycleView(view);
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
     }
##四、效果图如下:
Item默认动画效果

嵌套ScrollView效果

RecyclerView使用技巧(item动画及嵌套高度适配解决方案)的更多相关文章
- Android在开发中的使用技巧之解决ScrollView嵌套RecyclerView出现的系列问题
		根据已上线的app里总结出来的实用小技巧 相信大家都遇到过ScrollView嵌套RecyclerView或者RecyclerView嵌套RecyclerView来使用, 也会遇到一堆奇奇怪怪的问题, ... 
- Android解决RecyclerView中的item显示不全方案
		最近的项目中实现订单确定页面.需要使用ScrollView嵌套RecyclerView,当RecyclerView中的item数量比较多时,就会出现item只显示一部分数据,并没有将用户勾选的商品数量 ... 
- UIScrollView嵌套的完美解决方案
		UIScrollView嵌套的完美解决方案 做iOS开发,不可避免的会遇到UIScrollView的嵌套问题,之前也曾遇到过,吭哧吭哧做完了,效果不理想,和产品大战好几回合,就那样了.不可避免的,又一 ... 
- Atitit.异常处理 嵌套  冗长的解决方案
		Atitit.异常处理 嵌套 冗长的解决方案 1. 异常处理的需要改进的地方1 2. +异常设计的初衷是, 在程序中出现错误时, 由程序自己处理错误, 尽量不要以exit(0)这种粗暴的方式中止程序 ... 
- iOS6 / iOS7 状态栏高度适配
		问题原因:iOS7的状态栏(status bar)不再占用单独的20px,所以假设你在iOS6上的界面布局是正常的,那么到了iOS7上就会变成以下这个样子: 左边是iOS6界面 ... 
- 为RecyclerView的不同item项实现不同的布局(添加分类Header)
		最近在做一个应用的时候,需要为GridLayoutManager添加头部header,然后自然而然就想到了用不同的itemType去加载不同的布局. 1.实现多item布局,用不同的itemType去 ... 
- Android AbsListView 的item动画类库 —— JazzyListView
		https://github.com/twotoasters/JazzyListView/tree/master/sample github:https://github.com/twotoaster ... 
- !!Python字典增删操作技巧简述+Python字典嵌套字典与排序
		http://developer.51cto.com/art/201003/186006.htm Python编程语言是一款比较容易学习的计算机通用型语言.对于初学者来说,首先需要掌握的就是其中的一些 ... 
- ListView的Item动画
		1.效果图 2.需求就是点击item歌曲时,实现一种飞入到预约按钮处的效果 3.思路:在布局文件中加入了一个条目布局,和listView的item一样,点击listView的item时,使用给条目布局 ... 
随机推荐
- spring@value取不到值的几种情况
			一,spring组件重写构造方法,在构造方法中引用@value为null 由于spring实例化顺序为先执行构造方法,再注入成员变量,所以序为先执行构造方法,再注入成员变量,所以ing实例化顺取值为n ... 
- webstorm取消扫描某文件夹,加快打开速度,例如忽略node_modules目录
			方法1.如下图所示(只对当前项目有效) 或者 如果要使用这种方法,一种是还没安装依赖的时候先创建node_modules目录然后打开webstorm设置好excluded再安装:另一种是已经安装了依赖 ... 
- Extjs4处理后台json数据中日期和时间的方法
			当ASP.NET后台使用JavaScriptSerializer这个组件将对象序列化为json,或者使用ScriptMethod特性的json [ScriptMethod(ResponseFormat ... 
- 浅谈JSONP (vue-jsonp组件 XXXtoken:报错处理)
			由于同源策略的存在,特别是前后端两个项目存在的情况下,客户端访问服务端必然存在跨域的情况,而使用jsonp,则不存在这个问题. 主要是因为jsonp是在页面中插入一段js代码,而请求返回的也是一段js ... 
- 码云创建maven工程
			码云创建maven工程步骤 1.现在码云上创建工程2.拉取到本地3.使用"touch .gitignore"命令创建.gitignore文件4.在文件中添加过滤文件: .setti ... 
- SpringBoot -- 事件(Application Event)
			Spring的事件为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让一个Bean监听当前Bean所发送的事件. ... 
- MVC--初步理解(01)
			一.一般意义上的MVC模式 MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为以下三个基本部分: 模型(Model):模型用于封装与应用程序的业务逻 ... 
- SparseArray类
			继续阅读SparseArray的源码,从构造方法我们可以看出,它和一般的List一样,可以预先设置容器大小,默认的大小是10: public SparseArray() { this(10); } p ... 
- 01-MySql的前戏
			[转]01-MySql的前戏 MySql的前戏 在学习Mysql之前,我们先来想一下一开始做的登录注册案例,当时我们把用户的信息保存到一个文件中: #用户名 |密码root|123321 alex|1 ... 
- 本地服务器搭建服务:mysql
			话不多少,mysql的优劣不再此讨论. 1.官网地址:https://dev.mysql.com/downloads/mysql/ 下载页面直接 No thanks,just start my do ... 
