Skip to content

Instantly share code, notes, and snippets.

@BlinkoWang
Created April 19, 2016 08:33
Show Gist options
  • Save BlinkoWang/136f8ced9cab97c8389e11523fc44257 to your computer and use it in GitHub Desktop.
Save BlinkoWang/136f8ced9cab97c8389e11523fc44257 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.support.annotation.UiThread;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Wang Xuanyu on 16/4/15.
*/
@UiThread
public class KeywordsTrayLayout extends ViewGroup {
private ArrayList<String> mWords;
public KeywordsTrayLayout(Context context) {
this(context, null);
}
public KeywordsTrayLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public KeywordsTrayLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
ArrayList<String> words = new ArrayList<String>();
words.add("今天天气好晴朗");
words.add("处处闻啼鸟");
words.add("让我们荡起双桨");
words.add("歌唱我们亲爱的祖国");
words.add("唱出我们的歌");
words.add("东方红");
words.add("实在编不下去了");
words.add("谁来帮我唱两句");
words.add("没办法了");
words.add("赶紧调试");
words.add("我要出去玩");
words.add("不要捣乱了");
setWords(words);
}
/**
* Any layout manager that doesn't scroll will want this.
*/
@Override
public boolean shouldDelayChildPressedState() {
return false;
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new MarginLayoutParams(p);
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof MarginLayoutParams;
}
@Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(event);
event.setClassName(getClass().getSimpleName());
}
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(getClass().getSimpleName());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Measurement will ultimately be computing these values.
int maxHeight = 0;
int maxWidth = 0;
int childState = 0;
int lineWidth = 0;
int lineHeight = 0;
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
lineWidth += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
lineHeight = Math.max(lineHeight,
child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
childState = combineMeasuredStates(childState, child.getMeasuredState());
if (lineWidth >= MeasureSpec.getSize(widthMeasureSpec)) {
maxHeight += lineHeight;
lineWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
lineHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
}
}
}
maxHeight += lineHeight;
// Check against our minimum height and width
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
// Report our final dimensions.
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
resolveSizeAndState(maxHeight, heightMeasureSpec,
childState << MEASURED_HEIGHT_STATE_SHIFT));
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final int count = getChildCount();
int leftPos = getPaddingLeft();
int topPos = getPaddingTop();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int width = child.getMeasuredWidth();
final int height = child.getMeasuredHeight();
if (leftPos + width >= right - left - getPaddingRight()) {
leftPos = getPaddingLeft();
topPos += height;
}
child.layout(leftPos, topPos, leftPos + width, topPos + height);
leftPos += width;
}
}
}
public void setWords(ArrayList<String> words) {
this.mWords = words;
for (String word : words) {
addViewInLayout(obtainView(word), -1, new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT), true);
}
invalidate();
requestLayout();
}
private TextView obtainView(String word) {
TextView view = new TextView(getContext());
view.setText(word);
view.setBackgroundResource(R.drawable.text_bg);
return view;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment