【问题标题】:Change background colour of current listview item in adapter getView method在适配器 getView 方法中更改当前列表视图项的背景颜色
【发布时间】:2013-09-20 12:48:44
【问题描述】:

我正在为列表视图构建一个自定义适配器 - 我希望这个适配器为列表视图提供交替的背景颜色。

boolean alternate = false;

@Override
public View getView(int position, View convertView, ViewGroup parent) {

        if (alternate)
        {
            // set background colour of this item?
        }

        alternate = !alternate;

        // lots of other stuff here

        return convertView;     }

如何在上下文中设置列表视图项的背景?

【问题讨论】:

    标签: android listview


    【解决方案1】:

    这些是以下步骤来做展示。 Step1.1) 对奇偶位置列表项使用两个选择器

    artists_list_backgroundcolor.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item
     android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@color/grey" />
    <item android:state_pressed="true"
        android:drawable="@color/itemselected" />
    <item android:state_selected="true"
     android:state_pressed="false"
        android:drawable="@color/itemselected" />
    </selector>
    

    步骤 1.2) Artist_list_background_alternate.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item
     android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@color/sign_out_color" />
    <item android:state_pressed="true"
        android:drawable="@color/login_hover" />
    <item android:state_selected="true"
     android:state_pressed="false"
        android:drawable="@color/login_hover" />
    </selector>
    

    步骤 2) 颜色.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <color name="survey_toplist_item">#EFEDEC</color>
        <color name="survey_alternate_color">#EBE7E6</color>
        <color name="grey">#ffffff</color>
        <color name="itemselected">#EDEDED</color>
        <color name="login_hover">#E5F5FA</color>
        <color name="sign_out_color">#e84040</color>
    
    </resources>
    

    步骤 3) 在 Arrayadapter 中:

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                view = lInflater.inflate(R.layout.listitem, parent, false);
            }
    
            if (position % 2 == 0) {
                view.setBackgroundResource(R.drawable.artists_list_backgroundcolor);
            } else {
                view.setBackgroundResource(R.drawable.artists_list_background_alternate);
            }
    
            ((TextView) view.findViewById(R.id.heading)).setText(data.get(position));
    
            return view;
        }
    

    更多详情请访问belog链接

    http://amitandroid.blogspot.in/2013/03/android-listview-with-alternate-list.html

    【讨论】:

    • 是否可以添加备用按钮??
    【解决方案2】:

    您没有朝着正确的方向前进,好像视图被重复使用一样,您可能会得到意想不到的结果,因为一些回收的视图将具有不同的颜色,而另一些则没有。

    根据位置设置背景,而不是上面的。比如:

    if(position % 2 == 0) {
        // set some color
    } else {
        // set the other color
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用此代码:

       if (position % 2 == 0) {
                  holder.image.setBackgroundResource(R.drawable.image1);
              } else {
                 holder.image.setBackgroundResource(R.drawable.image2);
              }
      

      【讨论】:

        【解决方案4】:

        如果我错了,请纠正我,但我是这样做的:

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
                convertView = ((LayoutInflater) this._ctx
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                        .inflate(this._resource, parent, false);
        
            }
        
           switch(position % 3){
              case 0: convertView.setBackgroun....
                  break;
              .... (case 1; case 2;)
        
           }
        
        return convertView;
        
        }
        

        【讨论】:

          【解决方案5】:

          你可以在你的适配器中做

             public View getView(int position, View convertView, ViewGroup parent) {
          
                  if (convertView == null) {
                      inflater = ((Activity) context).getLayoutInflater();
                      convertView = inflater.inflate(resourceId, parent, false);
                  }
                 Person user = getItem(position);
          
                 if(user.isCharged)
                 convertView.setBackgroundColor(Color.BLUE);
              }
          

          这将应用于您的所有项目

          【讨论】:

            【解决方案6】:
             private int[] colors = new int[] { 0xffD3D3D3, 0xffA9A9A9 };
            
                inside getView refer the id
                 holder._linear_text_active_release_date = (LinearLayout) convertView.findViewById(R.id.relative_text_active_release_date);
            
            holder._linear_text_active_release_status = (LinearLayout) convertView.findViewById(R.id.linear_text_active_release_status); 
            add these lines in the getView to set the colour for layout row
            
            holder._linear_text_active_release_status.setBackgroundColor(Color.LTGRA;
                        holder._linear_text_active_release_pass.setBackgroundColor(ContextCompat.getColor(context,R.color.amber));
            

            【讨论】:

              【解决方案7】:

              我想强调解决方案,以免人们感到困惑。

              如果您想要颜色/图像,或在绘制时对列表视图进行的任何更改,您需要在 getView 中设置它,如下所示,但如果您希望它在点击时显示,您需要在 onClick 方法中进行如下所示。

                  @Override
                  public View getView(int position, View view, ViewGroup parent) {
                      if(view == null) {
                          view = inflater.inflate(R.layout.dashboard_item, parent, false);
                      }
              
                      if(selected[position]) {
                          view.setBackgroundColor(Color.LTGRAY);
                      } else {
                          selected[position] = false;
                          view.setBackgroundColor(Color.WHITE);
                      }
                      return view;
                  }
              
                  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                      @Override
                      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                          //this is just my custom object..you could also assign your boolean here to see if it is "highlighted"
                          Item o = (Item)listView.getItemAtPosition(position);
                          if(adapter.selected[position] == true) {
                              adapter.selected[position] = false;
                              view.setBackgroundColor(Color.WHITE);
                          } else {
                              adapter.selected[position] = true;
                              view.setBackgroundColor(Color.LTGRAY);
                          }
                   }
              

              我的布尔检查只是一个布尔数组,比如

              private boolean[] selected;
              

              我在 ArrayAdapater 构造函数的子类中初始化它。

              selected = new boolean[objects.size()];
              Arrays.fill(selected, Boolean.FALSE);
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2019-08-07
                • 1970-01-01
                • 2021-04-14
                • 2016-01-15
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多