【问题标题】:I want to Change the backgroud Colo of Specific Recycler VIew Item我想更改特定 Recyclerview 项目的背景颜色
【发布时间】:2018-02-18 20:10:56
【问题描述】:

我想更改 Recycler 查看状态为 Ture 的数据的背景颜色为绿色,并设置状态为 False 的背景颜色为红色。

第一轮状态为真,其余为假。 那么谁能告诉我我是如何区分的......

我的回收站查看代码写在下面。

@Override
public void onBindViewHolder(DocRecycleAdoptor.MyViewHolder holder, int position) {
    final EmpAttandance empAttandance = mDocs.get(position);

    holder.txtDocID.setText(empAttandance.getDOC_ID());
    holder.txtEmpID.setText(empAttandance.getEMP_ID());
    holder.dateTime.setText(empAttandance.getDATE()+" "+empAttandance.getTIME());
    holder.txtCoID.setText(empAttandance.getCO_ID());
    holder.txtStatus.setText(empAttandance.getSA());
    holder.empName.setText(empAttandance.getEmpName());

    if(empAttandance.getStatus() == "True"){
        holder.itemView.setBackgroundColor(Color.GREEN);
    }else{
        holder.itemView.setBackgroundColor(Color.RED);
    }
}

现在它显示为这样。 但我想当状态为假时颜色会变红。 它确实发生了。 前三个状态为真,其他为假。 以相同的颜色显示所有内容

【问题讨论】:

  • 在 list.getStatus 的 onbind 检查中 == true ..更改 bgcolor.. 否则将 bgcolor 更改为白色
  • 您在 xml 中为布局提供了白色背景。如果你删除它,那么它应该可以工作。
  • 请发布您的项目 xml

标签: android


【解决方案1】:

在你的 onBindViewHolder 添加下面的检查

if(empAttandance.getStatus()){
//set your desired color on textview or a view for status true
}else{
//set your desired color on textview or a view for status false
}

【讨论】:

  • 更改特定视图背景颜色的正确代码是什么
【解决方案2】:
@Override public void onBindViewHolder(DocRecycleAdoptor.MyViewHolder holder, int position) {
  final EmpAttandance empAttandance = mDocs.get(position); 
           holder.txtDocID.setText(empAttandance.getDOC_ID());
holder.txtEmpID.setText(empAttandance.getEMP_ID()); 
holder.dateTime.setText(empAttandance.getDATE()+" "+empAttandance.getTIME()); 
holder.txtCoID.setText(empAttandance.getCO_ID()); 
holder.txtStatus.setText(empAttandance.getSA()); 
holder.empName.setText(empAttandance.getEmpName());

   // this is it
 holder.rootLayout.setBackgroundColor(empAttandance.getStatus() ? GREEN : RED);

 }

【讨论】:

    【解决方案3】:
            inside your activity_docattd_list_items add linear layout.
            add inside linearlayout a drawable @drawarble/selector_gray_view.
    
            #selector_gray_view.xml
            <selector xmlns:android="http://schemas.android.com/apk/res/android">
                <item android:drawable="@drawable/gradient_gray_light"
                    android:state_pressed="true"/>
                <item android:drawable="@color/clicked"
                    android:state_focused="true"/>
            </selector>
    
            and after that add drawble @gradient_gray_light.
    
            #gradient_gray_light.xml
            <?xml version="1.0" encoding="utf-8"?>
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle">    
                <gradient android:startColor="#30888888"
                  android:endColor="#30888888"
                  android:angle="90" />
            </shape>`enter code here`
            and after that add another drawable ,
    
            click inside your selector_gray_view.xml,
            @drawable/color. 
    
         and also intialize linearlayout in myviewholder.
         use it inside onbindviewholder like as: 
        holder.linearlayout.setonclicklistener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
     }
    );
            happy coding.
    

    【讨论】:

      【解决方案4】:

      在 ANdroid == 中不起作用。所以我使用 empAttandance.getStatus().equal("True") 而不是 == 并且它的工作正常。

      感谢你们所有人帮助我。非常感谢。

      if(empAttandance.getStatus().equal("True")){
      //set your desired color on textview or a view for status true
      }else{
      //set your desired color on textview or a view for status false
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 2023-01-17
        • 2012-03-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多