1. 程式人生 > >gridview 自適應平均android介面

gridview 自適應平均android介面

需要重寫
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;
        }

    }