下面是gridview 控件,每行放置四个,因此用宽度除4. 下面展现出来的的界面就显的比较美观。
public class HotSearchAdapter extends BaseAdapter { private LayoutInflater inflater; private List<App> infos; private Context context; public HotSearchAdapter(Context context, List<App> infos) { super(); this.infos = infos; this.context = context; this.inflater = LayoutInflater.from(context); initWin(); } @Override public int getCount() { if (infos != null && infos.size() > 0) { return infos.size(); } return 0; } @Override public Object getItem(int position) { if (infos != null && infos.size() > 0) { return infos.get(position); } return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { DataHolder holder; if (convertView == null) { convertView = inflater.inflate(R.layout.hot_search_gv_item, null); holder = new DataHolder(); holder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image); convertView.setTag(holder); } else { holder = (DataHolder) convertView.getTag(); } App mApp = infos.get(position); int pad = (int) (2*density); convertView.setPadding(pad, pad, pad, pad); convertView.setLayoutParams(new AbsListView.LayoutParams(widthPixels/4,widthPixels/4)); return convertView; } public final class DataHolder { public ImageView iv_image; } private void initWin(){ WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics metrics = new DisplayMetrics(); manager.getDefaultDisplay().getMetrics(metrics); density = metrics.density; widthPixels = metrics.widthPixels; } private float density; private int widthPixels; }