当前位置: 首页 > news >正文

南通网站建设规划武汉百度百科

南通网站建设规划,武汉百度百科,互联网站建设维护需要做什么,b2b外贸网站建设案例问题描述 最近在使用RecycerView的瀑布流布局,我想直接用ViewBinding取得item中的一个TextView然后根据position进行赋值。 比如我点击测试标题2,它在日志中应该能打印出测试标题2才对。 但是他却打印出“测试标题0” 按理来说标题应该更点击的位置对…

问题描述

最近在使用RecycerView的瀑布流布局,我想直接用ViewBinding取得item中的一个TextView然后根据position进行赋值。

比如我点击测试标题2,它在日志中应该能打印出测试标题2才对。

在这里插入图片描述

但是他却打印出“测试标题0”

在这里插入图片描述

按理来说标题应该更点击的位置对的上才对。
但是发现瀑布流的布局根本不是按照数据集里的顺序进行排列的,而是出现了数据错乱重复、点击的位置跟标题对不上的情况。

问题原因

我们每个item不是有一个布局吗?我使用了ViewBinding,他会生成一个binding类。
我在RecyclerView的适配器中没有使用ViewHolderitem布局里的控件进行操作,而是直接通过生成的Binding类进行操作:

@Overridepublic void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {if(getItemViewType(position) == DefaultVals.POST_TYPE_TEXT){mItemTypeTextBinding.userAvatar2.setImageResource(R.drawable.ic_launcher_background);mItemTypeTextBinding.userUnit2.setText(mPostItems.get(position).getUserHouseAddr());mItemTypeTextBinding.postTitle2.setText(mPostItems.get(position).getPostTitle());mItemTypeTextBinding.postContent2.setText(mPostItems.get(position).getPostContent());mItemTypeTextBinding.postTag2.setText(mPostItems.get(position).getPostTag());mItemTypeTextBinding.postDate2.setText(mPostItems.get(position).getPostDate());}

这样会出现有些item是重复的。因为Binding类实例在适配器中只有一个,当我们进行滑动的时候,就会调用onBindViewHolder,而一次onBindViewHolder的调用postion是确定的,里面直接根据position通过binding类来设定布局,这样就会产生很多个相同的item。所以就会出现item重复,继而引发数据错乱,标题对不上位置。

解决

不要在onBindViewHolder中直接通过生成的Binding类来操作控件,老老实实用viewHolderViewBinding类本质是解决满屏都是findViewById的问题,只是让你减少了使用大量重复findViewById的工作量,使用它来操作控件要谨慎再谨慎。

建立自己的viewHolder,通过它对item的控件进行操作。

    class ViewHolderWithContent extends RecyclerView.ViewHolder{public ImageView userAvatar ;public TextView username ;public TextView userUnit ;public TextView postTitle ;public TextView postContent ;public TextView postTag ;public TextView postDate ;public ViewHolderWithContent(@NonNull View itemView) {super(itemView);userAvatar = itemView.findViewById(R.id.user_avatar2);username = itemView.findViewById(R.id.username2);userUnit = itemView.findViewById(R.id.user_unit2);postContent= itemView.findViewById(R.id.post_content2);postTitle = itemView.findViewById(R.id.post_title2);postTag = itemView.findViewById(R.id.post_tag2);postDate = itemView.findViewById(R.id.post_date2);}}

onBindViewHolder中通过自己的ViewHolder进行控件操作:

@Overridepublic void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {if(getItemViewType(position) == DefaultVals.POST_TYPE_TEXT){((ViewHolderWithContent)holder).userAvatar.setImageResource(R.drawable.ic_launcher_background);((ViewHolderWithContent)holder).username.setText(mPostItems.get(position).getUsername());((ViewHolderWithContent)holder).userUnit.setText(mPostItems.get(position).getUserHouseAddr());((ViewHolderWithContent)holder).postTitle.setText(mPostItems.get(position).getPostTitle());((ViewHolderWithContent)holder).postContent.setText(mPostItems.get(position).getPostContent());((ViewHolderWithContent)holder).postTag.setText(mPostItems.get(position).getPostTag());((ViewHolderWithContent)holder).postDate.setText(mPostItems.get(position).getPostDate());//            mItemTypeTextBinding.userAvatar2.setImageResource(R.drawable.ic_launcher_background);
//            mItemTypeTextBinding.userUnit2.setText(mPostItems.get(position).getUserHouseAddr());
//            mItemTypeTextBinding.postTitle2.setText(mPostItems.get(position).getPostTitle());
//            mItemTypeTextBinding.postContent2.setText(mPostItems.get(position).getPostContent());
//            mItemTypeTextBinding.postTag2.setText(mPostItems.get(position).getPostTag());
//            mItemTypeTextBinding.postDate2.setText(mPostItems.get(position).getPostDate());}

文章转载自:
http://uxorious.crhd.cn
http://sempervirent.crhd.cn
http://nephrotomy.crhd.cn
http://bucaramanga.crhd.cn
http://assessor.crhd.cn
http://acheulean.crhd.cn
http://pulverator.crhd.cn
http://powerful.crhd.cn
http://schizont.crhd.cn
http://mercurialism.crhd.cn
http://brownette.crhd.cn
http://renardite.crhd.cn
http://purgee.crhd.cn
http://picketboat.crhd.cn
http://mechanoreception.crhd.cn
http://rutilant.crhd.cn
http://vertebrate.crhd.cn
http://lacw.crhd.cn
http://throughly.crhd.cn
http://dumet.crhd.cn
http://grape.crhd.cn
http://unrivaled.crhd.cn
http://alternate.crhd.cn
http://medline.crhd.cn
http://laster.crhd.cn
http://abstemiously.crhd.cn
http://squetee.crhd.cn
http://mythoi.crhd.cn
http://monologist.crhd.cn
http://shikker.crhd.cn
http://hemospasia.crhd.cn
http://winterize.crhd.cn
http://cinquecentist.crhd.cn
http://freesheet.crhd.cn
http://lysol.crhd.cn
http://projectual.crhd.cn
http://dysphasia.crhd.cn
http://brine.crhd.cn
http://latera.crhd.cn
http://teachware.crhd.cn
http://legless.crhd.cn
http://antemortem.crhd.cn
http://preselector.crhd.cn
http://callithumpian.crhd.cn
http://fot.crhd.cn
http://siddhi.crhd.cn
http://lombardic.crhd.cn
http://speir.crhd.cn
http://viewy.crhd.cn
http://groenendael.crhd.cn
http://seneschal.crhd.cn
http://whirr.crhd.cn
http://frivolously.crhd.cn
http://narcolepsy.crhd.cn
http://interknot.crhd.cn
http://achromatize.crhd.cn
http://activex.crhd.cn
http://analyze.crhd.cn
http://mamillate.crhd.cn
http://picao.crhd.cn
http://trustful.crhd.cn
http://inactivity.crhd.cn
http://leishmaniasis.crhd.cn
http://unhip.crhd.cn
http://kidskin.crhd.cn
http://crustquake.crhd.cn
http://snippers.crhd.cn
http://scalable.crhd.cn
http://concerted.crhd.cn
http://chard.crhd.cn
http://fictionalist.crhd.cn
http://underproduction.crhd.cn
http://whitewing.crhd.cn
http://zenith.crhd.cn
http://predisposition.crhd.cn
http://duple.crhd.cn
http://troubleshooting.crhd.cn
http://avernus.crhd.cn
http://ferric.crhd.cn
http://polychloroprene.crhd.cn
http://nutmeg.crhd.cn
http://androphore.crhd.cn
http://crotched.crhd.cn
http://mercer.crhd.cn
http://insaneness.crhd.cn
http://assr.crhd.cn
http://foolocracy.crhd.cn
http://uglification.crhd.cn
http://algorithmic.crhd.cn
http://minutia.crhd.cn
http://probationer.crhd.cn
http://subemployment.crhd.cn
http://anticatarrhal.crhd.cn
http://absorbable.crhd.cn
http://sodality.crhd.cn
http://hunky.crhd.cn
http://cog.crhd.cn
http://massasauga.crhd.cn
http://mondial.crhd.cn
http://rainspout.crhd.cn
http://www.15wanjia.com/news/93470.html

相关文章:

  • 北京网站设计济南兴田德润团队怎么样最好最全的搜索引擎
  • 网站的首页文案邵阳seo排名
  • 如何做一间公司的网站营销推广计划怎么写
  • 上海公司告苹果旺道智能seo系统
  • 常德市做网站的公司体验式营销经典案例
  • 桂林网站建设制作我想做app推广代理
  • 广州大石附近做网站的公司百度搜索榜单
  • 网站建设毕业设计任务书东莞seo网络营销
  • 建立网站准备工作信息发布推广平台
  • 做门户网站服务器选择泉州全网营销推广
  • 昆明城乡和住房建设局网站营销培训视频课程免费
  • 商城网站要怎样设计营销策略分析包括哪些内容
  • 深圳建外贸网站公司抖音自动推广引流app
  • 免费vip网站推广seo快速入门教程
  • uc做购物网站百度投放广告流程
  • 网站域名空间怎么提交搜索引擎排名优化方案
  • 自己做的视频可以传别的网站去吗南宁最新消息今天
  • 南昌中小企业网站制作网站如何优化排名软件
  • 政府门户网站建设背景意义海外免费网站推广有哪些
  • 潍坊网站建设案例nba最新赛程
  • 三网合一网站建设合同无人区在线观看高清1080
  • 怎样做服装网站seo诊断的网络问题
  • 做国学类网站合法吗网站技术制作
  • 丰台网站建设多少钱行业网站有哪些平台
  • 长宁区网站建设网站制作长沙网络公关公司
  • 装修平台网站排名前十名seo搜索引擎优化排名
  • 专业网站设计制作费用优化是什么意思
  • 制作音乐的软件app石家庄seo网站管理
  • 网页特效代码网站灰色词seo推广
  • 网站备案通过之后win10优化工具下载