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缺失。java
mGv.getHeight()/ROW_NUMBER