首页 新闻 会员 周边

GridView在position==0时getView多次调用

0
悬赏园豆:100 [已解决问题] 解决于 2016-01-22 11:14

我请求数据然后在GridView中展示但是position==0是总是重复调用  布局只是LinearLayout嵌套个GridView 所以应该和布局什么android:layout_height无关

下面是适配器的getView

@Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        Product info = products.get(arg0);
        Holder holder = null;
        if (null == arg1) {
            holder = new Holder();

            arg1 = View.inflate(context, R.layout.product_item, null);
            holder.product_cost = (TextView) arg1
                    .findViewById(R.id.product_cost);
            holder.product_title = (TextView) arg1
                    .findViewById(R.id.product_title);
            holder.product_img = (ImageView) arg1
                    .findViewById(R.id.product_img);
            holder.buy_count = (TextView) arg1.findViewById(R.id.buy_count);

            arg1.setTag(holder);
        } else {
            holder = (Holder) arg1.getTag();
        }
        holder.product_cost.setText(products.get(arg0).getSalePrice() + "");
        holder.product_title.setText(products.get(arg0).getTitle());
        holder.buy_count.setText(products.get(arg0).getBuyCount() + "");
        Log.d("asdf", "dddddd___   " + arg0 + "_________" + info.getTitle());
        String getSmallPicUrl = "";
        if (info.getSmallPic() != null) {
            getSmallPicUrl = "http://www.huandaohang.com/UploadImages/ProductImages/"
                    + info.getSmallPic();
            ImageLoader.getInstance().displayImage(getSmallPicUrl,
                    holder.product_img, options);
        } else {
            holder.product_img.setBackgroundColor(R.drawable.ic_launcher);
        }
        return arg1;
    }

 

 

如下是日志

问题补充:

其中的ImageLoader的DisplayImageOptions options

options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.ic_empty)
                // image连接地址为空时
                .showImageOnFail(R.drawable.ic_error)
                // image加载失败
                .resetViewBeforeLoading(true).cacheOnDisc(true)
                .imageScaleType(ImageScaleType.EXACTLY).cacheInMemory(true)
                .cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new FadeInBitmapDisplayer(300))// 设置用户加载图片task(这里是渐现图片显示)
                .build();
王丰蛋哥的主页 王丰蛋哥 | 初学一级 | 园豆:44
提问于:2016-01-18 15:52
< >
分享
最佳答案
0

可以在GridView的外面嵌套个ScrollView再用if(parent.getChildCount == positon){}也可以处理多次调用

Android中的Adapter内,有getView方法. 假设你自己定义的某个ListView 或者GridView,有可能在程序运行中修改它们的Item的高度,那么此时getView将被回调.于是就出现position=0的情况. 因为出现这种情况,并且此时item内的对象的引用与正确的position=0是不一样的,它不过一个临时的对象而已. 所以,要加个条件来判断此时是否不是临时的position=0. if (parent.getChildCount() == position) { //里面就是正常的position } else { //临时的position=0 }

王丰蛋哥 | 初学一级 |园豆:44 | 2016-01-22 11:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册