我创建了一个自定义视图ButtonView,我想在RecyclerView内的GridLayout中动态显示它。 在RecyclerView Adapter的onBindViewHolder中,我获取gridLayout并动态放置ButtonViews。
for(Appliance appliance : appliances){
ButtonView appliance_button = new ButtonView(context, as);
appliance_button.setCircleText(appliance.getApplianceName());
gridLayout.addView(appliance_button);
}
因此,如果设备列表有4个项目,我应该获得4个ButtonViews,但我只获得第一个渲染。请帮我把所有4个设备都渲染出来。
ButtonView的代码是:
public class ButtonView extends View {
//circle and text colors
private int circleCol =android.R.color.holo_blue_light, labelCol = android.R.color.black;
private boolean isChecked, isAuto;
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
if(isAuto){
setAuto(false);
}
if(isChecked){
setLabelCol(onColor);
}else{
setLabelCol(offColor);
}
}
public boolean isAuto() {
return isAuto;
}
public void setAuto(boolean isAuto) {
this.isAuto = isAuto;
if(isAuto){
setOuterCirclePaint(autoColor);
}else{
setOuterCirclePaint(normalColor);
}
}
private int offColor = android.R.color.darker_gray, onColor = android.R.color.black, autoColor = android.R.color.holo_green_light, normalColor = android.R.color.black;
private void setLabelCol(int labelCol) {
this.labelCol = labelCol;
invalidate();
}
public String getCircleText() {
return circleText;
}
public void setCircleText(String circleText) {
this.circleText = circleText;
invalidate();
}
private void setOuterCirclePaint(int outerCirclePaint) {
this.outerCirclePaint = outerCirclePaint;
invalidate();
}
//label text
private String circleText = "Appliance";
//paint for drawing custom view
private Paint circlePaint;
private int outerCirclePaint;
public ButtonView(Context context)
{
super(context);
init(context);
}
public ButtonView(Context context, AttributeSet attrs)
{
super(context, attrs);
circlePaint = new Paint();
//get the attributes specified in attrs.xml using the name we included
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.ButtonView, 0, 0);
try {
//get the text and colors specified using the names in attrs.xml
circleText = a.getString(R.styleable.ButtonView_circleLabel);
circleCol = a.getInteger(R.styleable.ButtonView_circleColor, android.R.color.holo_blue_dark);//0 is default
onColor = a.getInteger(R.styleable.ButtonView_onColor, android.R.color.white);
offColor = a.getInteger(R.styleable.ButtonView_offColor, android.R.color.darker_gray);
isAuto = a.getBoolean(R.styleable.ButtonView_isAuto, false);
isChecked = a.getBoolean(R.styleable.ButtonView_isChecked, false);
autoColor = a.getInteger(R.styleable.ButtonView_autoColor, android.R.color.holo_green_light);
normalColor = a.getInteger(R.styleable.ButtonView_normalColor, android.R.color.black);
if(isChecked){
labelCol = onColor;
}else{
labelCol = offColor;
}
if(isAuto){
outerCirclePaint = autoColor;
}else{
outerCirclePaint = normalColor;
}
} finally {
a.recycle();
}
}
private void init(Context context) {
circlePaint = new Paint();
//get the attributes specified in attrs.xml using the name we included
TypedArray a = context.getTheme().obtainStyledAttributes(R.styleable.ButtonView);
try {
//get the text and colors specified using the names in attrs.xml
circleText = a.getString(R.styleable.ButtonView_circleLabel);
circleCol = a.getInteger(R.styleable.ButtonView_circleColor, android.R.color.holo_blue_dark);//0 is default
onColor = a.getInteger(R.styleable.ButtonView_onColor, android.R.color.white);
offColor = a.getInteger(R.styleable.ButtonView_offColor, android.R.color.darker_gray);
isAuto = a.getBoolean(R.styleable.ButtonView_isAuto, false);
isChecked = a.getBoolean(R.styleable.ButtonView_isChecked, false);
autoColor = a.getInteger(R.styleable.ButtonView_autoColor, android.R.color.holo_green_light);
normalColor = a.getInteger(R.styleable.ButtonView_normalColor, android.R.color.black);
if(isChecked){
labelCol = onColor;
}else{
labelCol = offColor;
}
if(isAuto){
outerCirclePaint = autoColor;
}else{
outerCirclePaint = normalColor;
}
} finally {
a.recycle();
}
}
@Override
protected void onDraw(Canvas canvas) {
int viewWidthHalf = this.getMeasuredWidth()/10;
int viewHeightHalf = this.getMeasuredHeight()/10;
int radius = 0;
if(viewWidthHalf>viewHeightHalf)
radius=viewHeightHalf-10;
else
radius=viewWidthHalf-10;
circlePaint.setStyle(Style.FILL);
circlePaint.setAntiAlias(true);
//set the paint color using the circle color specified
circlePaint.setColor(circleCol);
canvas.drawCircle(viewWidthHalf, viewHeightHalf, radius, circlePaint);
circlePaint.setStyle(Style.STROKE);
circlePaint.setAntiAlias(true);
circlePaint.setStrokeWidth(10);
//set the paint color using the circle color specified
circlePaint.setColor(outerCirclePaint);
canvas.drawCircle(viewWidthHalf, viewHeightHalf, radius, circlePaint);
Typeface tf = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
circlePaint.setTypeface(tf);
//set the text color using the color specified
circlePaint.setColor(labelCol);
//set text properties
circlePaint.setTextAlign(Paint.Align.CENTER);
circlePaint.setStrokeWidth(0);
circlePaint.setTextSize(20);
if(circleText.length() > 10){
circleText = circleText.substring(0, 10);
circleText = circleText + "..";
}
//draw the text using the string attribute and chosen properties
canvas.drawText(circleText, viewWidthHalf, viewHeightHalf, circlePaint);
}
}