getView()在GridView控件的多个电话 [英] Multiple calling of getView() in GridView

查看:120
本文介绍了getView()在GridView控件的多个电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动由GridView中持有超过40元的。开始活动的用户可以看到最大的15个项目(3行,5项在每一行)之后。我写的getView()的身体传递给越来越查看LogCat中号:

My Activity consists of GridView which holds 40+ elements. After starting activity user could see maximum 15 items (3 rows, 5 items in each row). I wrote in getView() body to pass to the LogCat number of getting View:

Log.i("getView()", "GETTING VIEW_POSITION[" + String.valueOf(position) + "]" + (convertView != null?convertView.toString():"null"));

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

After launching my app I get such log:

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

我也红了这个发布有关这类问题。但是,我的GridView的高度XML描述设置填充母:

I also red this post about such kind of issue. But My GridView's height in XML description set as fill parent:

<?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"
                /> 

和最终消除所有疑虑,这是我的适配器code:

And finally to remove all doubts, here is my Adapter code:

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, 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;
        }   
    }

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

I hope somebody will respond at my problem and will help me to solve it.

等待你的建议。亚历克斯。

Waiting for your suggestions. Alex.

推荐答案

该链接的响应,如果很清楚,我认为

The response on that link if quite clear I think

绝对没有任何保证的   次数)的数目getView(将   对于任何给定的位置调用

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

现在这句话也意味着你不应该使用 WRAP_CONTENT

Now this remark also implies you shouldn't use wrap_content

通常发生在列表和网格   具有高度设置为WRAP_CONTENT

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

但是,这并不意味着它不会发生的其他情形。该错误已关闭,WorkingAsIntended,所以我不认为你可以做的太多。这只是事情都有可能发生。

But that does not mean it would not happen for other situations. The bug was closed as "WorkingAsIntended", so I don't think you can do too much. It's just something that can happen.

这篇关于getView()在GridView控件的多个电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆