问题描述:
在写之前,先把问题的图贴出来,问题如下图,GridView的第一个item的自定义高度显示不正常(再次加载之后又正常),不是预设的高度:
解决方法:
首先需要申明,这个GridView 的适配器用的是BaseAdapter,在定义适配器的getView方法中,主要注意代码中/***********/标记的地方,下面贴出我的getView方法:
public View getView(int position, View convertView, ViewGroup parent) { GirdHolder holder; if(convertView == null){ holder = new GirdHolder(); convertView = LayoutInflater.from(getApplication()).inflate(R.layout.gridview_item, null); //set item's height GridView.LayoutParams param = new GridView.LayoutParams( android.view.ViewGroup.LayoutParams.MATCH_PARENT,(gvList.getHeight()-(1+level)*16)/level); convertView.setLayoutParams(param); holder.imgView = (ImageView) convertView.findViewById(R.id.p_w_picpath); convertView.setTag(holder); }else{ holder = (GirdHolder) convertView.getTag(); //fix the problem of first item's height is not normal /******************一定要把设置高度的代码再写在这个位置******************************/ convertView.setLayoutParams(new GridView.LayoutParams( android.view.ViewGroup.LayoutParams.MATCH_PARENT,(gvList.getHeight()-(1+level)*16)/level)); /************************************************/ } if(status[position] == 0){ holder.imgView.setImageResource(R.drawable.smile_yellow); }else{ holder.imgView.setImageResource(R.drawable.smile_bule); } return convertView; }
再贴上解决后的效果图: