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

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

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

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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <GridView xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@+id/grid"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. android:numColumns="auto_fit"
  8. android:horizontalSpacing="10dp"
  9. android:verticalSpacing="10dp"
  10. android:columnWidth="140dp"
  11. android:gravity="center"
  12. android:scrollbars="none"
  13. />

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

  1. public class ImageAdapter extends BaseAdapter
  2. {
  3. private Activity activity;
  4. private LayoutInflater inflater = null;
  5. private ArrayList<Photo> photoes;
  6.  
  7. public ImageAdapter(Activity a,ArrayList<Photo> pictures)
  8. {
  9. photoes = pictures;
  10. activity = a;
  11. inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  12. }
  13.  
  14. @Override
  15. public View getView(int position,View convertView,ViewGroup parent)
  16. {
  17. Log.i("getView()","GETTING VIEW_POSITION[" + String.valueOf(position) + "]" + (convertView != null?convertView.toString():"null"));
  18. View mViewSlice = convertView;
  19. if(convertView == null) {
  20. mViewSlice = inflater.inflate(R.layout.photo_preview,null);
  21. ((TextView) mViewSlice.findViewById(R.id.name_of_photo)).setText(photoes.get(position).getName());
  22.  
  23. mViewSlice.setPadding(5,5,5);
  24. mViewSlice.setLayoutParams(new GridView.LayoutParams(-2,-2));
  25.  
  26. }
  27.  
  28.  
  29.  
  30. return mViewSlice;
  31. }
  32.  
  33. @Override
  34. public int getCount() {
  35. return photoes.size();
  36. }
  37.  
  38. @Override
  39. public Object getItem(int position) {
  40. return photoes.get(position);
  41. }
  42.  
  43. @Override
  44. public long getItemId(int position) {
  45. return position;
  46. }
  47. }

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

等待你的建议亚历克斯.

解决方法

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

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”,所以我不认为你可以做太多.这只是可能发生的事情.

猜你在找的Android相关文章