Android GridView如何固定间隔,item能自适应大小(笔记)

这个适配需求之前工作也适配过,现在项目又要用到GridView并更好的适配,不如写个笔记以后也方便使用;

适配需求--item间无论是垂直或者列表间隔固定值dp,然后每个item固定宽度随之适应,如下图:

Android GridView如何固定间隔,item能自适应大小(笔记)_第1张图片

util代码:

import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListAdapter;

public class ViewUtils {

	public static int GRIDVIEW_ITEM = 0x20123456;

	/**
	 * 设置GridView中item固定垂直和横行间隔宽度item, 然后自适应一行多少个,每个item会自动改大小适应。
	 * 
	 * @param convertView  item的View
	 * @param parent    getview 中的 parent
	 * @param itemWithDp  希望item的大致宽度
	 * @param isSquare  item是否是正方形
	 */
	public static void setGridViewItemWith(View convertView, ViewGroup parent, int itemWithDp, int hSpacingDp,
			int vSpacingDp, boolean isSquare) {
		if (parent instanceof GridView && null == convertView.getTag(GRIDVIEW_ITEM)) {
			if (0 == parent.getWidth()) {
				return;
			}
			GridView gv = (GridView) parent;
			float density = gv.getContext().getResources().getDisplayMetrics().density;
			if (null == gv.getTag(GRIDVIEW_ITEM)) { // GridView只设置一次
				int parentWith = gv.getWidth() - gv.getPaddingLeft() - gv.getPaddingRight();
				int count = (int) (parentWith / ((itemWithDp + hSpacingDp) * density));

				gv.setVerticalSpacing((int) (density * vSpacingDp));
				gv.setHorizontalSpacing((int) (density * hSpacingDp));
				if (count <= 0) {
					count = 1;
				}
				gv.setNumColumns(count);
				gv.setTag(GRIDVIEW_ITEM, count);

			}

			if (null == convertView.getTag(GRIDVIEW_ITEM)) { // 一个convertView只设置一次
				int parentWith = gv.getWidth() - gv.getPaddingLeft() - gv.getPaddingRight();
				int count = (int) gv.getTag(GRIDVIEW_ITEM);
				int itemWith = (int) ((parentWith - (density * hSpacingDp) * (count - 1)) / count); // item宽度
				ViewGroup.LayoutParams l = convertView.getLayoutParams();
				l.width = itemWith;
				if (isSquare) {
					l.height = itemWith;
				}
				convertView.setLayoutParams(l);
			}
		}
	}
}

使用:

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (null == convertView) {
			convertView = new View(parent.getContext());
		}
		// 希望item大概50dp,垂直或者列间隔都为10,item又为正方形
		ViewUtils.setGridViewItemWith(convertView, parent, 50, 10, 10, true);
		return convertView;
	}
再贴上经常用的一个方法,ListView 在 setOnItemClickListener 方便得到item对象:

    @SuppressWarnings("unchecked")
    public static  O getItem(AdapterView parent, View view, int position, long id) {

        O o;
        if (parent.getAdapter() instanceof HeaderViewListAdapter) {
            int headerViewsCount = 0;
            if (view.getParent() instanceof ListView) {
                ListView listView = (ListView) view.getParent();
                headerViewsCount = listView.getHeaderViewsCount();
            }

            HeaderViewListAdapter headerAdapter = (HeaderViewListAdapter) parent.getAdapter();
            ListAdapter list = headerAdapter.getWrappedAdapter();
            position = position - headerViewsCount;
            o = (O) list.getItem(position);
        } else { // 没有添加HeaderView
            o = (O) parent.getAdapter().getItem(position);
        }
        return o;
    }
使用:

        lvList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                BankCard card = ViewUtil.getitem(parent, view, position, id);
            }
        });





你可能感兴趣的:(android,开发小知识)