[custom UI series]android 自动排布的标签布局

好吧今天只是充数的。一个自动排列的布局,适合放各种标签。当横向超过Max时会放到下一行。

直接上代码,那个接口可以去掉



package com.lht.pan_android.view;

import java.util.HashMap;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.lht.pan_android.R;
import com.lht.pan_android.view.ShareSelectView.ISSV;

/**
 * @ClassName: AutoArrangeCardLayout
 * @Description: TODO
 * @date 2016年1月24日 下午5:32:19
 * 
 * @author leobert.lan
 * @version 1.0
 */
public class AutoArrangeCardLayout extends ViewGroup implements ISSV {

	private static final int DEFAULT_HORIZONTAL_SPACING = 5;
	private static final int DEFAULT_VERTICAL_SPACING = 5;

	private int mVerticalSpacing;
	private int mHorizontalSpacing;

	private final Context mContext;

	int line_height = 0;

	public AutoArrangeCardLayout(Context context) {
		super(context);
		mContext = context;
		setHorizontalSpacing(DEFAULT_HORIZONTAL_SPACING);
		setVerticalSpacing(DEFAULT_VERTICAL_SPACING);
	}

	public AutoArrangeCardLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.AutoArrangeCardLayout);
		try {
			mHorizontalSpacing = a.getDimensionPixelSize(
					R.styleable.AutoArrangeCardLayout_horizontal_spacing,
					DEFAULT_HORIZONTAL_SPACING);
			mVerticalSpacing = a.getDimensionPixelSize(
					R.styleable.AutoArrangeCardLayout_vertical_spacing,
					DEFAULT_VERTICAL_SPACING);
		} finally {
			a.recycle();
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);

		final int width = MeasureSpec.getSize(widthMeasureSpec)
				- getPaddingLeft() - getPaddingRight();
		int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop()
				- getPaddingBottom();
		final int count = getChildCount();
		int line_height = 0;

		int xpos = getPaddingLeft();
		int ypos = getPaddingTop();

		int childHeightMeasureSpec;
		if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
			childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
					MeasureSpec.AT_MOST);
		} else {
			childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0,
					MeasureSpec.UNSPECIFIED);
		}

		for (int i = 0; i < count; i++) {
			final View child = getChildAt(i);
			if (child.getVisibility() != GONE) {
				child.measure(
						MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST),
						childHeightMeasureSpec);
				final int childw = child.getMeasuredWidth();
				line_height = Math.max(line_height, child.getMeasuredHeight()
						+ mVerticalSpacing);

				if (xpos + childw > width) {
					xpos = getPaddingLeft();
					ypos += line_height;
				}

				xpos += childw + mHorizontalSpacing;
			}
		}
		this.line_height = line_height;

		if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {
			height = ypos + line_height;

		} else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
			if (ypos + line_height < height) {
				height = ypos + line_height;
			}
		}
		setMeasuredDimension(width, height);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		final int count = getChildCount();
		final int width = r - l;
		int xpos = getPaddingLeft();
		int ypos = getPaddingTop();

		for (int i = 0; i < count; i++) {
			final View child = getChildAt(i);
			if (child.getVisibility() != GONE) {
				final int childw = child.getMeasuredWidth();
				final int childh = child.getMeasuredHeight();
				if (xpos + childw > width) {
					xpos = getPaddingLeft();
					ypos += line_height;
				}
				child.layout(xpos, ypos, xpos + childw, ypos + childh);
				xpos += childw + mHorizontalSpacing;
			}
		}
	}

	@Override
	protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
		return p instanceof LayoutParams;
	}

	@Override
	protected LayoutParams generateDefaultLayoutParams() {
		return new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
	}

	@Override
	public LayoutParams generateLayoutParams(AttributeSet attrs) {
		return new LayoutParams(getContext(), attrs);
	}

	@Override
	protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
		return new LayoutParams(p.width, p.height);
	}

	public void setHorizontalSpacing(int pixelSize) {
		mHorizontalSpacing = pixelSize;
	}

	public void setVerticalSpacing(int pixelSize) {
		mVerticalSpacing = pixelSize;
	}

	// ///

	private SSVCardAdapter ssvCardAdapter;

	@Override
	public void setAdapter(SSVAdapter adapter) {
		if (adapter instanceof SSVCardAdapter) {
			ssvCardAdapter = (SSVCardAdapter) adapter;
			ssvCardAdapter.notifyDataSetChanged();
		} else {
			throw new IllegalArgumentException(
					"give me a instance of ssvcardadapter");
		}
	}

	@Override
	public SSVAdapter getSSVAdapter() {
		return ssvCardAdapter;
	}

	// public final static int mode_nil = 0;
	//
	// public final static int mode_add = 1;
	//
	// public final static int mode_update = 2;

	// public void addViewWithAnim(View view, int index, int mode) {
	// super.addView(view, index);
	//
	// Animation showAnimation = AnimationUtils.loadAnimation(mContext,
	// R.anim.scale_show);
	//
	// Animation dismissThenShowAnimation = AnimationUtils.loadAnimation(
	// mContext, R.anim.scale_dismissthenshow);
	//
	// if (mode == mode_add)
	// view.startAnimation(showAnimation);
	// else if (mode == mode_update)
	// view.startAnimation(dismissThenShowAnimation);
	//
	// }

	@Override
	public void addView(View child) {
		super.addView(child);
		Animation showAnimation = AnimationUtils.loadAnimation(mContext,
				R.anim.scale_show);
		child.startAnimation(showAnimation);
	}

	@Override
	public void removeView(View view) {

		Animation dismissAnimation = AnimationUtils.loadAnimation(mContext,
				R.anim.scale_dismiss);
		view.startAnimation(dismissAnimation);
		super.removeView(view);
	}

	@SuppressLint("UseSparseArrays")
	private HashMap<String, View> viewMap = new HashMap<String, View>();

	public void callAddView(View v, String key) {
		viewMap.put(key, v);
		addView(v);
	}
	
//	public boolean check

	public void callRemoveViewAt(String key) {
		if (viewMap.containsKey(key)) {
			View v = viewMap.get(key);
			removeView(v);
			viewMap.remove(v);
		}
	}

	public void callRemoveAll() {
		for (String k : viewMap.keySet()) {
			View v = viewMap.get(k);
			removeView(v);
			viewMap.remove(v);
		}
	}

}



<declare-styleable name="AutoArrangeCardLayout">
        <attr name="horizontal_spacing" format="dimension" />
        <attr name="vertical_spacing" format="dimension" />
    </declare-styleable>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值