gridview首位置重复加载情况下,解决首位置被Last位置信息覆盖问题

使用gridview时,打印结果有时会出现数据重复加载问题,布局问题可能会导致该问题的出现,这个好解决,只要把布局参数固定就可以了,比如说是具体的数值100dip,或者将其设置为fill_parent;还有另外一种数据重复加载问题,就是只加载首位置数据(打印结果前后都显示首位置,也就是position=0),该怎么处理呢?对应代码如下:

if (position == 0 && ModelContent.hasFirstLoaded)
        {
                    return convertVIew;
        }

        if (position == 0)
        {
            ModelContent.hasFirstLoaded = true;
        }

}

给定标志做出判断,进行过滤;该问题解决了,随之而来的可能还会有首位置被Last位置信息覆盖问题,又该怎么办?参见如下代码:

private View firstView = null;

public View getView(int position, View convertView, ViewGroup parent)
    {
        int id = position;
        ProductViewCache productViewCache = null;
        ProductVO productVO = null;
        productVO = (ProductVO)dataList.get(id); //实例化一个商品信息类的对象
        if (convertView == null)
        {
            convertView = mInflater.inflate(R.layout.suggest_gridview_adapter, null);
            productViewCache = new ProductViewCache(convertView, itemH);
            convertView.setTag(productViewCache);
        }
        else
        {
            productViewCache = (ProductViewCache)convertView.getTag();
        }

       // 解决首位置重复加载
        if (position == 0 && ModelContent.hasFirstLoaded)
        {
                    return firstView;
        }

        if (position == 0)
        {
            ModelContent.hasFirstLoaded = true;
        }

               ......      

               ......

               ......

       // 解决首位置被Last位置信息覆盖问题

        if(position == 0){
              firstView = convertView;
        }
       
        return convertView;

}

原因分析,首位置做过滤处理后,当converView保存last位置数据信息时,再次运行首位置(首位置重复),则进入到

if (position == 0 && ModelContent.hasFirstLoaded)
        {
                    return firstView;
        }

判断语句,原先直接返回了convertView,也就是Last位置信息,因此首位置显示Last位置数据信息,而该处返回firstView,保存了首位置信息,这样就解决了!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值