item高度自适应
public class MyAdapter extends BaseAdapter { GridView mGv; public static int ROW_NUMBER = 5; public MyAdapter (GridView gv, Context mContext, ArrayList<String> list) { this.context = mContext; this.mGv = gv; lstDate = list; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = LayoutInflater.from(context).inflate(R.layout.item, null); //高度计算 AbsListView.LayoutParams param = new AbsListView.LayoutParams( android.view.ViewGroup.LayoutParams.FILL_PARENT, mGv.getHeight()/ROW_NUMBER); convertView.setLayoutParams(param); return convertView; }
关键点 通过获取GridView 的高度来计算每个item的高度。缺点:个人认为,实际上GridView 重绘了一遍(数据量大时影响性能),第一遍的时候GridView高度为0,item也为0,求证!! 如果设置了ViewHolder 会使第一个item缺失。
mGv.getHeight()/ROW_NUMBER