recyclerview删除item的item高度怎么是固定的

RecyclerView高度随Item自适应
RecyclerView高度随Item自适应
[摘要:转载请说明出处:http://write.blog.csdn.net/postedit/ 编写RecyclerView.ItemDecoration时,正在onDraw方式中,Drawable的下度即是RecyclerView的下度加往RecyclerView的高低padding。 @Override public void onDraw(]
转载请注明出处:http://write.blog.csdn.net/postedit/
编写RecyclerView.ItemDecoration时,在onDraw方法中,Drawable的高度等于RecyclerView的高度减去RecyclerView的上下padding。
public void onDraw(Canvas c, RecyclerView parent, State state) {
int top = parent.getPaddingTop();
int bottom = parent.getHeight() - parent.getPaddingBottom();
int childCount = parent.getChildCount();
for(int i=0;i & childCi++){
View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams)child.getLayoutParams();
int left = child.getRight() + layoutParams.rightM
int right = left + mDivider.getIntrinsicWidth();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}但运行后的显示效果却和我的预期相差很大
可以看到,ItemDecoration高度竟然全屏了,然后检查xml布局文件:
activity_main.xml
&RelativeLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
tools:context=&com.xmy.recylerviewdemo.MainActivity& &
&android.support.v7.widget.RecyclerView
android:id=&@+id/recyclerView&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&/&
&/RelativeLayout&item.xml
&?xml version=&1.0& encoding=&utf-8&?& &LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:gravity=&center&
android:padding=&10.0dip&
android:orientation=&vertical& &
&ImageView
android:id=&@+id/item_iv&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:scaleType=&center&
android:src=&@drawable/img&
android:adjustViewBounds=&true&/&
android:id=&@+id/item_tv&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&/&
&/LinearLayout&xml布局文件中RecyclerView和Item的高度都设定的是wrap_content,那说好的自适应于item高度呢?查看Android文档,没发现有关RecyclerView高度相关说明,看来只能自己动手丰衣足食了。
根据Android-RecylerView初识里提到的,RecyclerView并不负责Item的显示工作,而Adapter负责的是数据仓库和Item的视图,所以最终把目标锁定到RecyclerView.LayoutManager上。于是尝试继承LinearLayoutManager,发现果然有onMeasure方法:
public void onMeasure(Recycler recycler, State state, int widthSpec,int heightSpec)在onMeasure中可以获得RecyclerView.Recycler。Recycler负责管理Item视图的重复利用,所以我们可以通过Recycler获取一个Item视图的实例,然后像复写其他ViewGroup一样,使用measureChild获取子视图的高度后使用setMeasuredDimension设置RecyclerView同样的高度即可。
public class MyLayoutManager extends LinearLayoutManager {
public MyLayoutManager(Context context) {
super(context);
// TODO Auto-generated constructor stub
public void onMeasure(Recycler recycler, State state, int widthSpec,int heightSpec) {
View view = recycler.getViewForPosition(0);
if(view != null){
measureChild(view, widthSpec, heightSpec);
int measuredWidth = MeasureSpec.getSize(widthSpec);
int measuredHeight = view.getMeasuredHeight();
setMeasuredDimension(measuredWidth, measuredHeight);
} }修改完之后的运行效果图:
最后奉上示例程序Github链接。
感谢关注 Ithao123精品文库频道,是专门为互联网人打造的学习交流平台,全面满足互联网人工作与学习需求,更多互联网资讯尽在 IThao123!
Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。
Hadoop是一个由Apache基金会所开发的分布式系统基础架构。
用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力进行高速运算和存储。
Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上;而且它提供高吞吐量(high throughput)来访问应用程序的数据,适合那些有着超大数据集(large data set)的应用程序。HDFS放宽了(relax)POSIX的要求,可以以流的形式访问(streaming access)文件系统中的数据。
Hadoop的框架最核心的设计就是:HDFS和MapReduce。HDFS为海量的数据提供了存储,则MapReduce为海量的数据提供了计算。
产品设计是互联网产品经理的核心能力,一个好的产品经理一定在产品设计方面有扎实的功底,本专题将从互联网产品设计的几个方面谈谈产品设计
随着国内互联网的发展,产品经理岗位需求大幅增加,在国内,从事产品工作的大部分岗位为产品经理,其实现实中,很多从事产品工作的岗位是不能称为产品经理,主要原因是对产品经理的职责不明确,那产品经理的职责有哪些,本专题将详细介绍产品经理的主要职责
IThao123周刊18:29 提问
RecyclerView的item显示出现控件显示不全
item中是一个复杂布局,其中的TextView的显示高度不对。
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/title"
android:weightSum="11"&
&RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:paddingBottom="10dp"&
android:id="@+id/tvQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:text="aslkjdkjhdaksjdhk立刻决定是否莱克斯顿螺丝钉看法上的浪费苏打绿分里是空的将速度;是副食店了解分i未记录河流啊哈我阿拉维uifhlieawh分i芦苇 哇额uifha"
android:textColor="@color/color_text_very_hard"
android:textSize="20sp"
android:id="@+id/tv_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:text="显示问题的检查项两节课第三方乐山大佛蓝色的开始的;史蒂文哦i额;哦恶化;偶发;W 3哦()*&()*&哦额我饿r9p8w4r  "
android:textColor="@color/color_text_very_light"
android:textSize="18sp"
android:layout_below="@+id/tvQuestion"
&/RelativeLayout&
&LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="3"
android:orientation="horizontal"&
&info.hoang8f.android.segmented.SegmentedGroup
android:id="@+id/ans_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
segmentedgroup:sc_border_color="@color/color_answer_border"
segmentedgroup:sc_border_width="1dp"
segmentedgroup:sc_corner_radius="5dp"
segmentedgroup:sc_no_comment_color="@color/color_answer_ignore"
segmentedgroup:sc_text_color="@color/color_answer_text"
segmentedgroup:sc_tint_color="@color/color_answer_pass"
segmentedgroup:sc_unqualified_color="@color/color_answer_fail"&
&RadioButton
android:id="@+id/ans_yes"
style="@style/RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="20sp"/&
&RadioButton
android:id="@+id/ans_no"
style="@style/RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="20sp"/&
&RadioButton
android:id="@+id/ans_never"
style="@style/RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textSize="20sp"/&
&/info.hoang8f.android.segmented.SegmentedGroup&
&/LinearLayout&
&/LinearLayout&
出现显示不全的view就是id为tvQuestion和tv_question
按赞数排序
在adapter中的onCreateViewHolder()中
View inflate = layoutInflater.inflate(R.layout.xxx, parent, false); 注意要依附于parent
123关注|648收录
7762关注|1465收录
428关注|184收录
其他相似问题几个问题,简单搞定高度。
首先,需要自定义一个LinearLayoutManager,这里RecyclerView在onMeasure回调中会调用LinearLayoutManager的onMeasure方法,所以需要在LinearLayoutManager的onMeasure中做一些高度设置的处理,大致内容:
public&void&onMeasure(RecyclerView.Recycler&recycler,&RecyclerView.State&state,&int&widthSpec,int&heightSpec)&{
&&&&View&view&=&recycler.getViewForPosition(0);
&&&&measureChild(view,&widthSpec,&heightSpec);
&&&&int&measuredWidth&=&View.MeasureSpec.getSize(widthSpec);
&&&&int&measuredHeight&=&view.getMeasuredHeight();
&&&&setMeasuredDimension(measuredWidth,&measuredHeight);
两个注意点:这里获取了view的高度,也就是item布局的高度,所以item的布局需要设定固定的高度,否则获取为0。其次,
mLayoutManager.setAutoMeasureEnabled(false)
mList.setHasFixedSize(false)
这两个设置不能少,否则报错,这里和RecyclerView.onMeasure中的调用顺序有关,源码:
protected&void&onMeasure(int&widthSpec,&int&heightSpec)&{
&&&&if&(mLayout&==&null)&{
&&&&&&&&defaultOnMeasure(widthSpec,&heightSpec);
&&&&if&(mLayout.mAutoMeasure)&{&&&&&//这里为true会进入该分支
&&&&&&&&final&int&widthMode&=&MeasureSpec.getMode(widthSpec);
&&&&&&&&final&int&heightMode&=&MeasureSpec.getMode(heightSpec);
&&&&&&&&final&boolean&skipMeasure&=&widthMode&==&MeasureSpec.EXACTLY
&&&&&&&&&&&&&&&&&&&heightMode&==&MeasureSpec.EXACTLY;
&&&&&&&&mLayout.onMeasure(mRecycler,&mState,&widthSpec,&heightSpec);&&&//在调用mLayout的onMeasure方法时(被自定义复写的方法),mState.mItemCount为0,造成越界异常
&&&&&&&&if&(skipMeasure&||&mAdapter&==&null)&{
&&&&&&&&&&&&
&&&&&&&&if&(mState.mLayoutStep&==&State.STEP_START)&{
&&&&&&&&&&&&dispatchLayoutStep1();
&&&&&&&&//&set&dimensions&in&2nd&step.&Pre-layout&should&happen&with&old&dimensions&for
&&&&&&&&//&consistency
&&&&&&&&mLayout.setMeasureSpecs(widthSpec,&heightSpec);
&&&&&&&&mState.mIsMeasuring&=&
&&&&&&&&dispatchLayoutStep2();
&&&&&&&&//&now&we&can&get&the&width&and&height&from&the&children.
&&&&&&&&mLayout.setMeasuredDimensionFromChildren(widthSpec,&heightSpec);
&&&&&&&&//&if&RecyclerView&has&non-exact&width&and&height&and&if&there&is&at&least&one&child
&&&&&&&&//&which&also&has&non-exact&width&&&height,&we&have&to&re-measure.
&&&&&&&&if&(mLayout.shouldMeasureTwice())&{
&&&&&&&&&&&&mLayout.setMeasureSpecs(
&&&&&&&&&&&&&&&&&&&&MeasureSpec.makeMeasureSpec(getMeasuredWidth(),&MeasureSpec.EXACTLY),
&&&&&&&&&&&&&&&&&&&&MeasureSpec.makeMeasureSpec(getMeasuredHeight(),&MeasureSpec.EXACTLY));
&&&&&&&&&&&&mState.mIsMeasuring&=&
&&&&&&&&&&&&dispatchLayoutStep2();
&&&&&&&&&&&&//&now&we&can&get&the&width&and&height&from&the&children.
&&&&&&&&&&&&mLayout.setMeasuredDimensionFromChildren(widthSpec,&heightSpec);
&&&&}&else&{
&&&&&&&&if&(mHasFixedSize)&{&&&//这里不设置为false会造成与上面相同的问题
&&&&&&&&&&&&mLayout.onMeasure(mRecycler,&mState,&widthSpec,&heightSpec);
&&&&&&&&&&&&
&&&&&&&&//&custom&onMeasure
&&&&&&&&if&(mAdapterUpdateDuringMeasure)&{
&&&&&&&&&&&&eatRequestLayout();
&&&&&&&&&&&&processAdapterUpdatesAndSetAnimationFlags();
&&&&&&&&&&&&if&(mState.mRunPredictiveAnimations)&{
&&&&&&&&&&&&&&&&mState.mInPreLayout&=&
&&&&&&&&&&&&}&else&{
&&&&&&&&&&&&&&&&//&consume&remaining&updates&to&provide&a&consistent&state&with&the&layout&pass.
&&&&&&&&&&&&&&&&mAdapterHelper.consumeUpdatesInOnePass();
&&&&&&&&&&&&&&&&mState.mInPreLayout&=&
&&&&&&&&&&&&}
&&&&&&&&&&&&mAdapterUpdateDuringMeasure&=&
&&&&&&&&&&&&resumeRequestLayout(false);
&&&&&&&&if&(mAdapter&!=&null)&{
&&&&&&&&&&&&mState.mItemCount&=&mAdapter.getItemCount();
&&&&&&&&}&else&{
&&&&&&&&&&&&mState.mItemCount&=&0;
&&&&&&&&eatRequestLayout();
&&&&&&&&mLayout.onMeasure(mRecycler,&mState,&widthSpec,&heightSpec);&&&//在这里调用时mState.mItemCount才会有值,这与mLayout中获取当前item布局的方式有关:View&view&=&recycler.getViewForPosition(0);
&&&&&&&&resumeRequestLayout(false);
&&&&&&&&mState.mInPreLayout&=&&//&clear
& 开源中国(OSChina.NET) |
开源中国社区(OSChina.net)是工信部
指定的官方社区优雅的为RecyclerView添加HeaderView和FooterView_鸿洋_【传送门】
优雅的为RecyclerView添加HeaderView和FooterView
RecyclerView通过其高度的可定制性深受大家的青睐,也有非常多的使用者开始对它进行封装或者改造,从而满足越来越多的需求。如果你对RecyclerView不陌生的话,你一定遇到过这样的情况,我想给RecyclerView加个essay-headerView或者footerView,当你敲出`.addHeaderView`,你会发现并没有添加头部或者底部View的相关API。那么本文主要的内容很明显了,完成以下工作:如何为RecyclerView添加HeaderView(支持多个)如何为RecyclerView添加FooterView(支持多个)如何让HeaderView或者FooterView适配各种LayoutManager恩,其实本来我是想偷个懒的,因为Loader写过一篇类似的文章(http://blog.csdn.net/qibin0506/article/details/)。但是我发现被别的公众号推送了~~ 恰好我也有类似的需求,于是考虑换种思路来解决这个问题,并且提供尽可能多的功能了~
2.1 原理对于添加essay-headerView或者footerView的思路其实HeaderView实际上也是Item的一种,只不过显示在顶部的位置,那么我们完全可以通过为其设置ItemType来完成。有了思路以后,我们心里就妥了,最起码我们的内心中想想是可以实现的,接下来考虑一些细节。
2.2 一些细节假设我们现在已经完成了RecyclerView的编写,忽然有个需求,需要在列表上加个HeaderView,此时我们该怎么办呢?打开我们的Adapter,然后按照我们上述的原理,添加特殊的ViewType,然后修改代码完成。这是比较常规的做法了,但是有个问题是,如果需要添加viewType,那么可能我们的Adapter需要修改的幅度就比较大了,比如`getItemType`、`getItemCount`、`onBindViewHolder`、`onCreateViewHolder`等,几乎所有的方法都要进行改变。这样来看,出错率是非常高的。况且一个项目中可能多个RecyclerView都需要在其列表中添加essay-headerView。这么来看,直接改Adapter的代码是非常不划算的,最好能够设计一个类,可以无缝的为原有的Adapter添加essay-headerView和footerView。本文的思路是通过类似装饰者模式,去设计一个类,增强原有Adapter的功能,使其支持`addHeaderView`和`addFooterView`。这样我们就可以不去改动我们之前已经完成的代码,灵活的去扩展功能了。我希望的用法是这样的:mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(mAdapter);
t1.setText("Header 1");
TextView t2 = new TextView(this);
mHeaderAndFooterWrapper.addHeaderView(t2);在不改变原有的Adapter基础上去增强其功能。
3初步的实现
3.1 基本代码首先我们编写一个Adapter的子类,我们叫做HeaderAndFooterWrapper,然后再其内部添加了addHeaderView,addFooterView等一些辅助方法。这里你可以看到,对于多个HeaderView,讲道理我们首先想到的应该是使用List,而这里我们为什么要使用SparseArrayCompat呢?SparseArrayCompat有什么特点呢?它类似于Map,只不过在某些情况下比Map的性能要好,并且只能存储key为int的情况。并且可以看到我们对每个HeaderView,都有一个特定的key与其对应,第一个essay-headerView对应的是BASE_ITEM_TYPE_HEADER,第二个对应的是BASE_ITEM_TYPE_HEADER+1;为什么要这么做呢?这两个问题都需要到复写onCreateViewHolder的时候来说明。
2.2 需要复写的方法getItemViewType由于我们增加了essay-headerView和footerView首先需要复写的就是getItemCount和getItemViewType。getItemCount很好理解;对于getItemType,可以看到我们的返回值是mHeaderViews.keyAt(position),这个值其实就是我们addHeaderView时的key,footerView是一样的处理方式,这里可以看出我们为每一个essay-headerView创建了一个itemType。onCreateViewHolder可以看到,我们分别判断viewType,如果是headview或者是footerview,我们则为其单独创建ViewHolder,这里的ViewHolder是我之前写的一个通用的库里面的类,文末有链接。当然,你也可以自己写一个ViewHolder的实现类,只需要将对应的essay-headerView作为itemView传入ViewHolder的构造即可。这个方法中,我们就可以解答之前的问题了:为什么我要用SparseArrayCompat而不是List?为什么我要让每个essay-headerView对应一个itemType,而不是固定的一个?对于essay-headerView假设我们有多个,那么onCreateViewHolder返回的ViewHolder中的itemView应该对应不同的essay-headerView,如果是List,那么不同的essay-headerView应该对应着:list.get(0),list.get(1)等。但是问题来了,该方法并没有position参数,只有itemType参数,如果itemType还是固定的一个值,那么你是没有办法根据参数得到不同的essay-headerView的。所以,我利用SparseArrayCompat,将其key作为itemType,value为我们的essay-headerView,在onCreateViewHolder中,直接通过itemType,即可获得我们的essay-headerView,然后构造ViewHolder对象。而且我们的itemType取值为100000以上,而正常的itemType是从0开始,所以在使用过程中几乎是不可能冲突的。需要说明的是,并非是一定不能用List,通过一些特殊的处理,List也能达到上述我描述的效果。onBindViewHolderonBindViewHolder比较简单,发现是HeaderView或者FooterView直接return即可,因为对于头部和底部我们仅仅做展示即可,对于事件应该是在addHeaderView等方法前设置。这样就初步完成了我们的装饰类,我们分别添加两个essay-headerView和footerView:我们看看运行效果:感觉还是不错的,再不改动原来的Adapter的基础上,我们完成了初步的实现。大家都知道RecyclerView比较强大,可以设置不同的LayoutManager,那么我们换成GridLayoutMananger再看看效果。好像发现了不对的地方,我们的essay-headerView果真被当成普通的Item处理了,由于我们的编写方式,出现上述情况是可以理解的。那么我们该如何处理呢?让每个essay-headerView独立的占据一行?
4进一步的完善
好在RecyclerView里面为我们提供了一些方法。
4.1 针对GridLayoutManager在我们的HeaderAndFooterWrapper中复写onAttachedToRecyclerView方法,如下:当发现layoutManager为GridLayoutManager时,通过设置SpanSizeLookup,对其getSpanSize方法,返回值设置为layoutManager.getSpanCount();现在看一下运行效果:哈,终于正常了。
4.2 针对StaggeredGridLayoutManager在刚才的代码中我们好像没有发现StaggeredGridLayoutManager的身影,StaggeredGridLayoutManager并没有setSpanSizeLookup这样的方法,那么该如何处理呢?依然不复杂,重写onViewAttachedToWindow方法,如下:这样就完成了对StaggeredGridLayoutManager的处理,效果图就不贴了。到此,我们就完成了整个HeaderAndFooterWrapper的编写,可以在不改变原Adapter代码的情况下,为其添加一个或者多个essay-headerView或者footerView,以及完成了如何让HeaderView或者FooterView适配各种LayoutManager。源码地址:/hongyangAndroid/baseAdapter如果对你有用,欢迎打赏鼓励~
觉得不错,分享给更多人看到
鸿洋 微信二维码
分享这篇文章
7月9日 22:20
鸿洋 最新文章
鸿洋 热门文章recyclerview加载item会自动显示完整,我不想让他完整显示,能怎么办
[问题点数:40分,结帖人qq_]
recyclerview加载item会自动显示完整,我不想让他完整显示,能怎么办
[问题点数:40分,结帖人qq_]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2016年4月 移动开发大版内专家分月排行榜第二
2016年7月 移动开发大版内专家分月排行榜第三2015年12月 移动开发大版内专家分月排行榜第三
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 recyclerview单选item 的文章

 

随机推荐