android - 如何在 RecycleViewer 中更改 Canvas?

标签 android android-canvas android-recyclerview

我在列表项中有带有 Canvas View 的 RecycleViewer。如何在我的 RecycleViewerAdapter 中更改 Canvas View 的背景颜色?

下面是我的 RecycleViewerAdapter 中的 onBindViewHolder 方法

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.openprice.setText(mopenPrice.get(position));

//How can I change backgrouncolor canvas view ????

}

我的 ViewHolder

public ViewHolder(View itemView) {
        super(itemView);

        openprice = (TextView) itemView.findViewById(R.id.openprice);            
        viewx = (View) itemView.findViewById(R.id.viewx);



    }

项目列表.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="@drawable/border_listfavorite"
>

<view class="com.eusecom.exforu.MyView"
      android:layout_width="fill_parent"
      android:layout_height="20dp"
      android:layout_alignParentLeft="true"
      android:id="@+id/viewx"/>

<TextView
    android:id="@+id/opentxt"
    android:text="@string/opentxt"
    android:singleLine="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/viewx"
    />

<TextView
    android:id="@+id/openprice"
    android:singleLine="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/opentxt"
    android:layout_below="@+id/viewx"
    />


</RelativeLayout>

我的 View .java

public class MyView extends View {
public MyView(Context cxt, AttributeSet attrs) {
    super(cxt, attrs);
    setMinimumHeight(100);
    setMinimumWidth(100);
}

@Override
protected void onDraw(Canvas cv) {
    cv.drawColor(Color.WHITE);
    Paint p = new Paint();
    p.setColor(Color.GREEN);
    p.setStrokeWidth(5);
    cv.drawLine(20, 0, 20, cv.getHeight(), p);
}


}

最佳答案

使用invalidate()方法调用onDraw方法。

   @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
    holder.viewx.setText(mopenPrice.get(position));

    if(condition){
          holder.viewx.setCustomColor(Color.GREEN);
          holder.viewx.invalidate();
    }

    }

公共(public)类 MyView 扩展 View {

private int color = Color.WHITE;

public MyView(Context cxt, AttributeSet attrs) {
    super(cxt, attrs);
    setMinimumHeight(100);
    setMinimumWidth(100);
}

@Override
protected void onDraw(Canvas cv) {
    cv.drawColor(color);
    Paint p = new Paint();
    p.setColor(Color.GREEN);
    p.setStrokeWidth(5);
    cv.drawLine(20, 0, 20, cv.getHeight(), p);
}

public void setCustomColor(int color){
   this.color = color;
}


public ViewHolder(View itemView) {
    private MyView viewx;
    super(itemView);
    openprice = (TextView) itemView.findViewById(R.id.openprice);            
    viewx = (MyView) itemView.findViewById(R.id.viewx);

}

关于android - 如何在 RecycleViewer 中更改 Canvas?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31210826/

相关文章:

android - 从 Activity 中的重叠布局中点击先前的布局

java - 单击 RecyclerView 项目时出现 NullPointerException

android - Jetpack Compose 中的 Canvas 重构

android - RecyclerView onItemClick 监听器

java - 如果触摸屏幕时有电话进来会崩溃

android - 签名 Apk 未被识别

android - 圆角ItemDecoration

Android - 带有以下文字的搜索栏

android - Phonegap 中已弃用插件类

java - 我可以将 Activity 转换为 float 窗口吗?