public class VerticalSearchGridView extends LinearLayout implements View.OnClickListener { private int mItemWidth; private int mItemHeight; public int mColumes; public int mRows; private final static int ITEM_MIN_HEIGH = DisplayManager.dipToPixel(75); private final static int ITEM_WIDTH = DisplayManager.dipToPixel(75); private final static int PADDING_LR= DisplayManager.dipToPixel(10); private List<SearchEngineInfo> mSearchEngineInfos; public VerticalSearchGridView(Context context) { super(context); } public VerticalSearchGridView(Context context, AttributeSet attrs) { super(context, attrs); } public VerticalSearchGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setData(List<SearchEngineInfo> searchEngineInfos){ mSearchEngineInfos = searchEngineInfos; for (int i = 0; i < mSearchEngineInfos.size(); i++) { SearchEngineGridItem item = new SearchEngineGridItem(); View childView = item.getView(i, this); childView.setOnClickListener(this); childView.setId(i); addView(childView); } } @Override public void onClick(View v) { int position = v.getId(); mOnitemClickListener.onItemClick(null, v, position, position); } private OnItemClickListener mOnitemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.mOnitemClickListener=onItemClickListener; } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int menuWidth = mItemWidth; int menuHeight = mItemHeight; for (int i = 0; i < mRows; i++) { for (int j = 0; j < mColumes; j++) { int index = i * mColumes + j; if (index < getChildCount()) { View child = getChildAt(index); int x = j * menuWidth; int y = i * menuHeight; child.layout(x + PADDING_LR, y, x + menuWidth + PADDING_LR, y + menuHeight); } } } } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST) { widthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY); } if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) { heightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY); } int width = DisplayManager.screenWidthPixel(getContext()) - getResources().getDimensionPixelSize(R.dimen.search_engine_grid_margin_left) - getResources().getDimensionPixelSize(R.dimen.search_engine_grid_margin_right); int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec); measureChildren(MeasureSpec.makeMeasureSpec(ITEM_WIDTH, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); int maxHeight = ITEM_MIN_HEIGH; int maxWidth = 0; int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); maxWidth = Math.max(maxWidth, child.getMeasuredWidth()); maxHeight = Math.max(maxHeight, child.getMeasuredHeight()); } mItemWidth = maxWidth; mItemHeight = maxHeight; int totleCount = mSearchEngineInfos.size(); width = width - PADDING_LR * 2; int oneLineCount = width / mItemWidth; if(totleCount < oneLineCount){ mColumes = totleCount; mRows = 1; }else {// 一排放不下 mItemWidth = width / oneLineCount; //平均放 int adds = 0; if (totleCount % oneLineCount != 0) { adds = 1; } mColumes = oneLineCount; mRows = totleCount / oneLineCount + adds; // 计算行数 } setMeasuredDimension(mItemWidth * mColumes + PADDING_LR * 2, mItemHeight * mRows); } private final static int ITEM_PADDING_LR = DisplayManager.dipToPixel(5); private class SearchEngineGridItem { public View getView(int position, ViewGroup parent) { View convertView = createView(parent); bindView(position, convertView); return convertView; } private View createView(ViewGroup parent) { View v = LayoutInflater.from(getContext()).inflate(R.layout.search_engine_item, parent, false); return v; } private void bindView(int position, View view) { final ImageView engineImg = (ImageView) view.findViewById(R.id.engine_item_img); final TextView engineName = (TextView) view.findViewById(R.id.engine_item_text); ThemeManager tm = ThemeManager.getInstance(); view.findViewById(R.id.engine_item).setBackgroundDrawable( tm.getDrawable(R.drawable.search_engine_item_bg)); SearchEngineInfo searchEngineInfo = mSearchEngineInfos.get(position); Drawable drawable = searchEngineInfo.getIconDrawable(); tm.applyDrawableAlpha(drawable); engineImg.setBackgroundDrawable(drawable); engineName.setTextColor(tm.getColor(R.color.search_engine_choose_item_textcolor)); engineName.setText(searchEngineInfo.getTitle()); engineName.setPadding(ITEM_PADDING_LR, 0, ITEM_PADDING_LR, 0); } } }
继承LinearLayout实现根据屏幕宽度及内部子View个数自动排布GridView
原文:http://www.cnblogs.com/lchd/p/3543369.html