baseAdapter填充多种布局时,布局错位
来源:互联网 发布:淘宝去哪里投诉卖家 编辑:程序博客网 时间:2024/06/05 20:36
比较菜的问题,大神请绕路走,慢走不送。
想当然的在getView里面做各种判断,然后if(){}放各种布局,然后报各种错误,不一一列举
百度了一下,是机制的问题,viewholer缓冲机制是,当你这个item挪出窗口后不销毁指定的view,而是在里面放新的需要显示的数据。so,填充不同布局肯定不行了,好一点的错位,坏一点的像我这次,所有的格式,ID都不一样,就直接报错了。
处理方法:想了两个方法:
1把填充的不同布局都扔到一个view中,然后设置显示不显示。
优点:简单容易,缺点:尼玛,懒的改
2机智的stackoverflow告诉我用baseAdapter 的几个回调函数:
下面是原文。懒得翻译。看不懂的留言(看的是转载的童鞋,有问题的戳http://blog.csdn.net/amazing_alex/article/details/40656277 留言)
You need to let the adapter's view recycler know that there is more than one layout and how to distinguish between the two for each row. Simply override these methods:
@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}
Incorporate getItemViewType()
inside getView()
, like this:
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, null); else convertView = mInflater.inflate(R.layout.listview_item_product_inprocess, null); // etc, etc...
Watch Android's Romain Guy discuss the view recycler at Google Talks.
下面还有一个大神讲解回收机制,貌似不错,准备今晚翻墙看一下
http://stackoverflow.com/questions/13297299/reusing-views-in-android-listview-with-2-different-layouts/13297339#13297339
- baseAdapter填充多种布局时,布局错位
- BaseAdapter-ListView中的item显示多种布局
- 用布局填充器填充ListView(继承BaseAdapter)
- ListView BaseAdapter 多种布局 和 viewholder的运用
- GridView 完美填充布局
- canvas-图像布局填充
- android BaseAdapter多布局缓存
- 多种布局的ListView
- Adapter实现多种布局
- listView中多种布局
- RecyclerView添加多种布局
- RecyclerView中的多种布局
- RecyclerView中的多种布局
- ListView实现多种布局
- RecyclerView中的多种布局
- adapter加载多种布局
- RecyclerView展示多种布局
- RecyclerView多种item布局
- 设计模式
- Android4.2中应用接收开机广播
- MySQL实现单机多实例安装并配置主从复制
- V4L2驱动程序架构
- iterator
- baseAdapter填充多种布局时,布局错位
- 输入一个单向链表,输出该链表中倒数第k个结点
- Qt Quick调试之显示代码行号
- 人的一生,与爱恨纠缠,与得失相伴
- 一个很有用的辅助类 -- 门槛类实现
- 2014.10.31
- 学会忘记
- 安卓之父从谷歌离职 将创办孵化器公司
- poj 1014 Dividing(多重背包可行性解)