android – 在GridView中多次调用getView()

前端之家收集整理的这篇文章主要介绍了android – 在GridView中多次调用getView()前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的Activity由GridView组成,包含40个元素.开始活动后,用户可以看到最多15个项目(每行3行,5个项目).我在getView()body中写到传递给获取View的LogCat数:
Log.i("getView()","GETTING VIEW_POSITION[" + String.valueOf(position) + "]" + (convertView != null?convertView.toString():"null"));

启动我的应用程序后,我得到这样的日志:

02-09 14:34:56.900: INFO/getView()(388): GETTING VIEW_POSITION[0]null
02-09 14:34:57.300: INFO/getView()(388): GETTING VIEW_POSITION[0]android.widget.FrameLayout@44c7a9c0  
02-09 14:34:57.300: INFO/getView()(388): GETTING VIEW_POSITION[1]null
02-09 14:34:57.400: INFO/getView()(388): GETTING VIEW_POSITION[2]null
02-09 14:34:57.510: INFO/getView()(388): GETTING VIEW_POSITION[3]null
02-09 14:34:57.620: INFO/getView()(388): GETTING VIEW_POSITION[4]null
02-09 14:34:57.720: INFO/getView()(388): GETTING VIEW_POSITION[5]null
02-09 14:34:57.840: INFO/getView()(388): GETTING VIEW_POSITION[6]null
02-09 14:34:57.930: INFO/getView()(388): GETTING VIEW_POSITION[7]null
02-09 14:34:58.273: DEBUG/dalvikvm(388): GC freed 3530 objects / 322744 bytes in 270ms
02-09 14:34:58.280: INFO/getView()(388): GETTING VIEW_POSITION[8]null
02-09 14:34:58.300: INFO/getView()(388): GETTING VIEW_POSITION[9]null
02-09 14:34:58.320: INFO/getView()(388): GETTING VIEW_POSITION[10]null
02-09 14:34:58.340: INFO/getView()(388): GETTING VIEW_POSITION[11]null
02-09 14:34:58.360: INFO/getView()(388): GETTING VIEW_POSITION[12]null
02-09 14:34:58.380: INFO/getView()(388): GETTING VIEW_POSITION[13]null
02-09 14:34:58.400: INFO/getView()(388): GETTING VIEW_POSITION[14]null
02-09 14:34:59.220: INFO/getView()(388): GETTING VIEW_POSITION[0]null
02-09 14:34:59.490: INFO/getView()(388): GETTING VIEW_POSITION[0]android.widget.FrameLayout@44c69ef0

我也是红色this的帖子关于这样一个问题.但是我的GridView的XML描述中的高度设置为填充父项:

<?xml version="1.0" encoding="utf-8"?>

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/grid"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:numColumns="auto_fit"
                android:horizontalSpacing="10dp"
                android:verticalSpacing="10dp"
                android:columnWidth="140dp"
                android:gravity="center"
                android:scrollbars="none"
                />

最后删除所有疑问,这里是我的适配器代码

public class ImageAdapter extends BaseAdapter
    {
        private Activity activity;
        private LayoutInflater inflater = null;
        private ArrayList<Photo> photoes;

        public ImageAdapter(Activity a,ArrayList<Photo> pictures)
        {
            photoes = pictures;
            activity = a;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public View getView(int position,View convertView,ViewGroup parent)
        {
            Log.i("getView()","GETTING VIEW_POSITION[" + String.valueOf(position) + "]" + (convertView != null?convertView.toString():"null"));
                View mViewSlice = convertView;
                if(convertView == null) {
                    mViewSlice = inflater.inflate(R.layout.photo_preview,null);
                    ((TextView) mViewSlice.findViewById(R.id.name_of_photo)).setText(photoes.get(position).getName());

                    mViewSlice.setPadding(5,5,5);
                    mViewSlice.setLayoutParams(new GridView.LayoutParams(-2,-2));

                }



               return mViewSlice;
        }

        @Override
        public int getCount() {
            return photoes.size();
        }

        @Override
        public Object getItem(int position) {
            return photoes.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }   
    }

我希望有人会回答我的问题,并帮助我解决这个问题.

等待你的建议亚历克斯.

解决方法

如果相当清楚,我认为这个链接的回应

There is absolutely no guarantee on
the number of times getView() will be
invoked for any given position

现在这个意思也暗示你不应该使用wrap_content

usually occurs with lists and grids
that have a height set to wrap_content

但这并不意味着在其他情况下不会发生.这个bug被关闭为“WorkingAsIntended”,所以我不认为你可以做太多.这只是可能发生的事情.

原文链接:https://www.f2er.com/android/311231.html

猜你在找的Android相关文章