gpt4 book ai didi

android - 如何在 RecycleViewer 中更改 Canvas?

转载 作者:行者123 更新时间:2023-11-30 02:02:52 25 4
gpt4 key购买 nike

我在列表项中有带有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com