/**
* @author xc
* @version 1.0
* @date 2016/7/3 14:16
* @describe 点击展开/隐藏的文本框
*/
public class MoreTextView extends LinearLayout {
/**
* 默认字体颜色
*/
public int defaultTextColor = Color.BLACK;
/**
* 默认标题字体大小
*/
public int defaultTitleTextSize = 14;
/**
* 默认内容字体大小
*/
public int defaultContentTextSize = 12;
/**
* 标题文本
*/
private TextView tv_title;
/**
* 已选标记
*/
private ImageView iv_hook;
/**
* 箭头
*/
private ImageView iv_arrows;
/**
* 题目选项
*/
private RadioGroup radioGroup;
/**
* 题目数量
*/
private RadioButton[] radioButtons = new RadioButton[4];
/**
* 题目名
*/
private String titleText;
/**
* 问题列表
*/
private String[] contents;
/**
* 标题布局
*/
private LinearLayout layout_title;
/**
* 是否已经全部选择
*/
private boolean isChecked = false;
/**
* 被选id
*/
private int checkedId;
public MoreTextView(Context context) {
this(context, null);
}
public MoreTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initalize();
initWithAttrs(context, attrs);
bindListener();
}
/**
* 初始化并添加View。初始化TextView和ImageView,并添加到MoretextView中去。
*/
private void initalize() {
setOrientation(VERTICAL);
layout_title = new LinearLayout(getContext());
LinearLayout layout_title_left = new LinearLayout(getContext());
layout_title.setPadding(0, dip2px(5), 0, dip2px(5));
radioGroup = new RadioGroup(getContext());
radioGroup.setPadding(dip2px(16), 0, 0, 0);
tv_title = new TextView(getContext());
iv_hook = new ImageView(getContext());
iv_arrows = new ImageView(getContext());
LayoutParams lp_layout_titleleft = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f);
layout_title_left.setGravity(Gravity.CENTER_VERTICAL);
layout_title_left.setLayoutParams(lp_layout_titleleft);
//红钩
LayoutParams lp_hook = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp_hook.setMargins(dip2px(5), 0, 0, 0);
iv_hook.setLayoutParams(lp_hook);
iv_hook.setScaleType(ImageView.ScaleType.FIT_XY);
iv_hook.setImageResource(R.mipmap.ic_red_hook);
//箭头
iv_arrows.setScaleType(ImageView.ScaleType.FIT_XY);
iv_arrows.setImageResource(R.mipmap.arrows_close);
for (int i = 0; i < 4; i++) {
//单选钮
radioButtons[i] = new RadioButton(getContext());
LayoutParams lp_rbtn = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
radioButtons[i].setGravity(Gravity.CENTER);
radioButtons[i].setPadding(dip2px(10), dip2px(6), 0, dip2px(6));
radioButtons[i].setButtonDrawable(R.drawable.ic_rbtn_selector);
radioButtons[i].setLayoutParams(lp_rbtn);
radioButtons[i].setGravity(Gravity.CENTER);
radioGroup.addView(radioButtons[i]);
}
layout_title.setOrientation(HORIZONTAL);
layout_title.setGravity(Gravity.CENTER_VERTICAL);
layout_title_left.addView(tv_title);
layout_title_left.addView(iv_hook);
layout_title.addView(layout_title_left);
layout_title.addView(iv_arrows);
radioGroup.setOrientation(VERTICAL);
addView(layout_title);
addView(radioGroup);
}
/**
* 从定义的styleable中取出属性值,赋给我们定义好的类的属性变量。记得取完之后调用recycle()回收释放
*
* @param context
* @param attrs
*/
private void initWithAttrs(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MoreTextStyle);
//取颜色值,默认defaultTextColor
int textColor = a.getColor(R.styleable.MoreTextStyle_moreTextColor, defaultTextColor);
float titleTextSize = a.getDimensionPixelSize(R.styleable.MoreTextStyle_moreTextSize, defaultTitleTextSize);
float contentTextSize = a.getDimensionPixelSize(R.styleable.MoreTextStyle_moreTextSize, defaultContentTextSize);
String titleText = a.getString(R.styleable.MoreTextStyle_titleText);
contents = new String[4];
contents[0] = a.getString(R.styleable.MoreTextStyle_contentTextOne);
contents[1] = a.getString(R.styleable.MoreTextStyle_contentTextTwo);
contents[2] = a.getString(R.styleable.MoreTextStyle_contentTextThree);
contents[3] = a.getString(R.styleable.MoreTextStyle_contentTextFour);
bindTextView(textColor, titleTextSize, contentTextSize, titleText, contents);
//回收释放
a.recycle();
}
/**
* 绑定到textView
*
* @param color
* @param titleSize
* @param titleText
* @param contents
*/
protected void bindTextView(int color, float titleSize, float contentSize, String titleText, String[] contents) {
tv_title.setTextColor(color);
tv_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, titleSize);
tv_title.setText(titleText);
for (int i = 0; i < radioButtons.length; i++) {
radioButtons[i].setText(contents[i]);
}
iv_arrows.setRotation(180);//默认箭头向上
iv_hook.setVisibility(INVISIBLE);
radioGroup.setVisibility(GONE);
}
private void bindListener() {
layout_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
HiddenAnimUtils.newInstance(getContext(), radioGroup, iv_arrows, UiUtils.dip2px(42)).toggle();
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
iv_hook.setVisibility(VISIBLE);
isChecked = true;
MoreTextView.this.checkedId = checkedId;
}
});
}
public void setData(String title, String[] contents) {
this.titleText = title;
this.contents = contents;
tv_title.setText(this.titleText);
for (int i = 0; i < radioButtons.length; i++) {
radioButtons[i].setText(this.contents[i]);
}
invalidate();
}
public boolean isChecked() {
return isChecked;
}
public int getCheckedId() {
return checkedId;
}
}