baseAdapter填充多种布局时,布局错位

比较菜的问题,大神请绕路走,慢走不送。


想当然的在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:

@Override
public int getItemViewType(int position) {
    // Define a way to determine which layout to use, here it's just evens and odds.
    return position % 2;
}

@Override
public 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填充多种布局时,布局错位)