在Android Listview中以2种不同的布局重用视图

在Android Listview中以2种不同的布局重用视图,第1张

在Android Listview中以2种不同的布局重用视图

您需要让适配器的视图回收器知道不止一种布局,以及如何区分每一行的两种布局。只需覆盖以下方法:

@Overridepublic int getItemViewType(int position) {    // Define a way to determine which layout to use, here it's just evens and odds.    return position % 2;}@Overridepublic int getViewTypeCount() {    return 2; // Count of different layouts}

合并

getItemViewType()
进来
getView()
,像这样:

if (convertView == null) {    // You can move this line into your constructor, the inflater service won't change.    mInflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);    if(getItemViewType(position) == 0)        convertView = mInflater.inflate(R.layout.listview_item_product_complete, parent, false);    else        convertView = mInflater.inflate(R.layout.listview_item_product_inprocess, parent, false);    // etc, etc...

观看Android的Romain Guy 在Google
Talks上讨论视图回收器。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5478477.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存