被这个问题坑了好久,之前搜索也没找准关键,各种尝试第一个项目就是不能响应一些事件,其它项目却毫无影响。
后来干脆在convertView == null时直接通过屏幕高度减去各种布局的高度,勉强达到了效果。
今天来尝试搜索了下“GridView第一个项目”发现竟然也有相应问题并附有解决方式,然后尝试了下,最后终于是解决了这个问题。
解决方法主要是在new convertView时首判断getLayoutParams()获取是否为null,为null则使用new一个布局,否则的话不能使用new来做,只能通过getLayoutParams()获取到了对象后再赋值高度。
但还是不太明白这个为什么只是第一个才会有这种问题,其它却不会。
以下是我解决的一些代码:
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder(context, parent, layoutRes, position, itemHeight);
// 动态计算行高
if(DeviceUtil.IsPortrait()) {
if(holder.mItem.getLayoutParams() == null) {
AbsListView.LayoutParams param = new AbsListView.LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT, itemHeight);
holder.mItem.setLayoutParams(param);
} else {
AbsListView.LayoutParams lp = (AbsListView.LayoutParams)holder.mItem.getLayoutParams();
lp.height = itemHeight;
}
}
} else {
holder = (ViewHolder) convertView.getTag();
if(DeviceUtil.IsPortrait()) {
AbsListView.LayoutParams lp = (AbsListView.LayoutParams)holder.mItem.getLayoutParams();
lp.height = itemHeight;
}
}
return holder;