有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java调用recyclerview中的textview和imageview

您好,我已经做了一个TextView布局(在其中我设置了一个背景图像)和一个简单的TextView视图,并在适配器中充气,并在回收器视图中显示它们,我可以根据自己的意愿在不同的recyclerview列表中更改TextView(带背景)和TextView,但我也希望通过文本中的更改更改TextView的背景

这是arraylist,我在其中输入了两个文本视图,但我不知道在哪里输入背景图像

wordList.add(new Word("1", "What is neuron?"));
wordList.add(new Word("2", "What is brain?"));
wordList.add(new Word("3", "What is gala?"));
wordList.add(new Word("4", "What is accurd?"));

<TextView 安卓:textColor="@安卓:color/white"
        安卓:gravity="center"
        安卓:id="@+id/circle"
        安卓:background="@drawable/teal"
        安卓:layout_width="40dp"
        安卓:layout_height="40dp"
        安卓:layout_marginLeft="8dp"
        安卓:text="C"
        安卓:layout_centerVertical="true"/>
    <TextView
        安卓:textSize="20sp"
        安卓:id="@+id/prod_name"
        安卓:paddingLeft="8dp"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_toRightOf="@+id/circle"
        安卓:layout_centerVertical="true"/>

共 (1) 个答案

  1. # 1 楼答案

    如果你从可绘图资源中获取本地背景图像,那么你必须像这样修改你的Word

    Class Word { 
           private String tv1;
           private String tv2;
           private Drawable backgroundImage;
    
               Word(String a, String b, Drawable c){
                  tv1=a; 
                  tv2=b;
                  backgroundImage=c
               }
           }
    

    现在你可以添加这样的图片

    wordList.add(new Word("1", "What is neuron?", 
    getResources().getDrawable(R.drawable.your_img)));
    wordList.add(new Word("2", "What is brain?", 
    getResources().getDrawable(R.drawable.your_img)));
    wordList.add(new Word("3", "What is 
    gala?", getResources().getDrawable(R.drawable.your_img)));
    wordList.add(new Word("4", "What is 
    accurd?", getResources().getDrawable(R.drawable.your_img)));
    

    现在,在ArrayList中有了背景图像。你可以随心所欲地使用它们