需要重写
GridView和BaseAdapter
public class WrapGridview extends GridView{ public WrapGridview(Context context) { super(context); } public WrapGridview(Context context,AttributeSet attributeSet) { super(context,attributeSet); } public WrapGridview(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//这是主要代码 int expandSpec = MeasureSpec.makeMeasureSpec( Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }class AutAdapter extends BaseAdapter { private List<Map<String, Object>> autlist = AutList(); @Override public int getCount() { return autlist.size(); } @Override public Map<String, Object> getItem(int i) { return autlist.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { AutHolder autHolder = null; if (view == null) { view = View.inflate(SkillInfoActivity.this, R.layout.item_authentication, null); autHolder = new AutHolder(); view.setLayoutParams(new GridView.LayoutParams(sidew, sidew));//这是主要的 控制item的大小 autHolder.iv = (ImageView) view.findViewById(R.id.skill); autHolder.tv = (TextView) view.findViewById(R.id.aut); view.setTag(autHolder); } else { autHolder = (AutHolder) view.getTag(); } Map<String, Object> autmap = getItem(i); autHolder.iv.setImageResource(Integer.parseInt(autmap.get("img").toString())); autHolder.tv.setText(autmap.get("title").toString()); return view; } }
时间: 2024-11-03 21:58:34