学习android也有一段时间了,开始试着模仿一些app,但实际开始做才发现有很多细节问题是自己不知道的,比如就遇到了标题所述的问题。参考了http://blog.csdn.net/liuwan1992/article/details/52659135?locationNum=5&fps=1。问题解决了,就有一点,不太明白。
“
第一个 item 去哪里了,就这样被吃掉了嘛,查了查资料,说是 GridView 在绘画时是根据第一个 item 定位其余 item 的,动态设置
item 高度,会导致第一个 item 丢失。解决方法呢,其实也不难,如果凑巧你可能还不会发现这个问题。方法是不要用ViewHolder 写
法,删除 item 布局,改成每次 getView 时创建新的 View 。”
这里我理解的做法是每次新建view,而不管convertView==null。我的代码最后这样的,现在还只是知其然,欢迎探讨。
if(convertView == null){ convertView = LayoutInflater.from(context).inflate(R.layout.selectedcity_list_item,null); convertView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,gridView.getHeight()/3)); item.txRemindCity = (TextView)convertView.findViewById(R.id.selectedcity_list_item_tx_remindcity); item.imgWeatherIcon = (ImageView)convertView.findViewById(R.id.selectedcity_item_img_weather); item.txHighest = (TextView)convertView.findViewById(R.id.selectedcity_item_tx_highest); item.txLowest = (TextView)convertView.findViewById(R.id.selectedcity_item_tx_lowest); item.txWeather = (TextView)convertView.findViewById(R.id.selectedcity_item_tx_weather); item.imgLocateIcon = (ImageView)convertView.findViewById(R.id.selectedcity_item_img_mylocation); item.txCity = (TextView)convertView.findViewById(R.id.selectedcity_item_tx_city); }else { convertView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,gridView.getHeight()/3)); item.txRemindCity = (TextView)convertView.findViewById(R.id.selectedcity_list_item_tx_remindcity); item.imgWeatherIcon = (ImageView)convertView.findViewById(R.id.selectedcity_item_img_weather); item.txHighest = (TextView)convertView.findViewById(R.id.selectedcity_item_tx_highest); item.txLowest = (TextView)convertView.findViewById(R.id.selectedcity_item_tx_lowest); item.txWeather = (TextView)convertView.findViewById(R.id.selectedcity_item_tx_weather); item.imgLocateIcon = (ImageView)convertView.findViewById(R.id.selectedcity_item_img_mylocation); item.txCity = (TextView)convertView.findViewById(R.id.selectedcity_item_tx_city); }