1:效果如下: ?
?2:代码:
public abstract class MyAppStart extends LinearLayout {
private OnItemSelectListener onItemSelectListener;
private int count;
public int colorId;
public int getColorId() {
return colorId;
}
public void setColorId(int colorId) {
this.colorId = colorId;
}
public MyAppStart(Context context, AttributeSet attrs) {
super(context, attrs);
for (int i = 0 ; i < max() ; i ++) {
View view = getView(new View(context));
view.setTag(i);
if (isClick()) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setSelect((Integer) v.getTag());
}
});
}
addView(view);
}
setSelect(count = 0);
}
public void setSelect(int count) {
for (int i = 0 ; i < getChildCount() ; i ++) {
getChildAt(i).setBackgroundResource(i <= (this.count = count) ? selectYes() : selectNo());
BitmapDrawable p = (BitmapDrawable) getChildAt(i).getBackground();
if (i <= (this.count = count) && isReColor()){
p.setColorFilter(new PorterDuffColorFilter(getColorId(), PorterDuff.Mode.SRC_ATOP));
} else {
p.setColorFilter(null);
}
}
if (onItemSelectListener != null) {
onItemSelectListener.onItemSelect(this.count = count);
}
}
public void setOnItemSelectListener(OnItemSelectListener onItemSelectListener) {
this.onItemSelectListener = onItemSelectListener;
onItemSelectListener.onItemSelect(count);
}
protected boolean isReColor(){
return false;
}
protected abstract boolean isClick();
protected abstract int max();
protected abstract int selectYes();
protected abstract int selectNo();
protected abstract View getView(View view);
public interface OnItemSelectListener {
void onItemSelect(int index);
}
}
?
public class EvaluateStartView extends MyAppStart {
private LinearLayout.LayoutParams params;
public EvaluateStartView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected boolean isClick() {
return true;
}
@Override
protected int max() {
return 5;
}
@Override
protected int selectYes() {
return R.mipmap.evaluate_start_on;
}
@Override
protected int selectNo() {
return R.mipmap.evaluate_start_off;
}
@Override
protected View getView(View view) {
params = new LinearLayout.LayoutParams(Utils.dpToPx(23), Utils.dpToPx(23));
params.setMargins(0, 0, Utils.dpToPx(5), 0);
view.setLayoutParams(params);
return view;
}
}
?3:使用说明:
在activity中直接使用
eva.setSelect(2);0-4(分别对应的选中效果是1-5个)。
当要监听选中了第几个的时候调用如下方法:
eva.setOnItemSelectListener(new MyAppStart.OnItemSelectListener() {
@Override
public void onItemSelect(int index) {
Toast.makeText(TagFlowLayoutActivity.this,index+"",Toast.LENGTH_SHORT).show();
}
});
?当不想让图片可点击时把
EvaluateStartView方法中的isClick()方法的返回值改成false就可以了。
|