自定义视图组内的更改未呈现

Changes inside custom viewgroup not rendering

我创建了一个自定义视图组来处理不同 class 中的视图,但是 none 显示了对子视图所做的更改,例如,如果我更改值内的 textview 值更改但未显示在自定义视图中。

我正在按照 google 指南制作自定义视图,并添加了一个代码来制作圆角。我在日志中测试过,值在后台发生变化问题是视图没有显示它们!

我的代码:


public class ShoppingButton extends FrameLayout {
    private final static float CORNER_RADIUS = 16.0f;

    private TextView plusBtn, minusBtn, valueTv;
    private ImageView overlayIv;

    private Bitmap maskBitmap;
    private Paint paint, maskPaint;
    private float cornerRadius;

    private int minValue = 0, maxValue = Integer.MAX_VALUE;

    public ShoppingButton(Context context) {
        super(context);
        init(context);
    }

    public ShoppingButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ShoppingButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View rootView = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.shopping_button, this);
        plusBtn = rootView.findViewById(R.id.plus_btn);
        minusBtn = rootView.findViewById(R.id.minus_btn);
        overlayIv = rootView.findViewById(R.id.overlay_iv);
        valueTv = rootView.findViewById(R.id.value_tv);
        overlayIv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                overlayIv.setVisibility(GONE);
                setValue(1);
            }
        });
        plusBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("CLICK", "PLUS");
                incrementValue();
            }
        });
        minusBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("CLICK", "MINUS");
                decrementValue();
            }
        });

        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

        setWillNotDraw(false);
    }

    @Override
    public void draw(Canvas canvas) {
        Bitmap offscreenBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas offscreenCanvas = new Canvas(offscreenBitmap);

        super.draw(offscreenCanvas);

        if (maskBitmap == null) {
            maskBitmap = createMask(canvas.getWidth(), canvas.getHeight());
        }

        offscreenCanvas.drawBitmap(maskBitmap, 0f, 0f, maskPaint);
        canvas.drawBitmap(offscreenBitmap, 0f, 0f, paint);
    }

    private Bitmap createMask(int width, int height) {
        Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
        Canvas canvas = new Canvas(mask);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);

        canvas.drawRect(0, 0, width, height, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint);

        return mask;
    }

    public int getMinValue() {
        return minValue;
    }

    public void setMinValue(int minValue) {
        this.minValue = minValue;
    }

    public int getValue() {
        return Integer.valueOf(valueTv.getText().toString());
    }

    public void setValue(int value) {
        Log.e("VALUE", "=" + value);
        if (value > maxValue)
            return;

        if (value == minValue + 1)
            minusBtn.setText("x");
        else
            minusBtn.setText("-");

        if (value == minValue)
            overlayIv.setVisibility(VISIBLE);

        plusBtn.setEnabled(!(value == maxValue));

        valueTv.setText(String.valueOf(value));
    }

    public int getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(int maxValue) {
        this.maxValue = maxValue;
    }

    private void incrementValue() {
        int currentValue = Integer.valueOf(valueTv.getText().toString());
        if (currentValue < maxValue)
            setValue(currentValue + 1);
    }

    private void decrementValue() {
        int currentValue = Integer.valueOf(valueTv.getText().toString());
        if (currentValue > minValue)
            setValue(currentValue - 1);
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="@dimen/_36sdp"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true">

    <ImageView
        android:id="@+id/overlay_iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/secondary"
        android:padding="8dp"
        android:visibility="gone"
        app:srcCompat="@drawable/ic_add_shopping_cart" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/secondary"
        android:orientation="vertical">

        <TextView
            android:id="@+id/plus_btn"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/secondary_variant"
            android:gravity="center"
            android:padding="4dp"
            android:text="+"
            android:textColor="@color/surface_light"
            android:textSize="@dimen/_16ssp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/value_tv"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/secondary"
            android:gravity="center"
            android:text="0"
            android:textColor="@color/surface_light"
            android:textSize="@dimen/_14ssp" />

        <TextView
            android:id="@+id/minus_btn"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/secondary_variant"
            android:gravity="center"
            android:padding="4dp"
            android:text="-"
            android:textColor="@color/surface_light"
            android:textSize="@dimen/_16ssp"
            android:textStyle="bold" />
    </LinearLayout>

</FrameLayout>

更改或评论或删除此行:

setWillNotDraw(false);

看看这个 ->

希望对你有用;)