简体   繁体   English

在适配器 getView 方法中更改当前列表视图项的背景颜色

[英]Change background colour of current listview item in adapter getView method

I am building a custom adapter for a listview - I would like this adapter to give the listview alternating background colours.我正在为列表视图构建自定义适配器 - 我希望此适配器为列表视图提供交替背景颜色。

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;     }

How would I set the background of the listview item in context?我将如何在上下文中设置列表视图项目的背景?

备用列表项目背景

These are the following steps to do show. 以下是要显示的步骤。 Step1.1) Use two selector for odd and even postion list item 步骤1.1)对奇数和偶数位置列表项使用两个选择器

artists_list_backgroundcolor.xml 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>

Step 1.2) artists_list_background_alternate.xml 步骤1.2)artists_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>

Step2) colors.xml Step2)colors.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>

Step 3) In Arrayadapter: 步骤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;
    }

For more details go through belog link 有关详细信息,请浏览belog链接

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

You're not going in the right direction as if the views are re-used you might get unexpected results in that some recycled views will have a different colors, while others not. 你没有朝着正确的方向前进,就像重新使用这些视图一样,你可能会得到意想不到的结果,因为一些回收的视图会有不同的颜色,而有些则没有。

Instead of above, set the background based on position. 而不是上面,根据位置设置背景。 Something like: 就像是:

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

You can use this code: 您可以使用此代码:

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

Please correct me if I am wrong but I do it this way: 如果我错了请纠正我,但我是这样做的:

@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;

}

You can do in your adapter 你可以在适配器中做

   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);
    }

that will be applied in all your items 这将适用于您的所有项目

 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));

在此输入图像描述 I wanted to highlight the solution so people aren't confused. 我想强调解决方案,这样人们就不会感到困惑。

If you want the colors/image, or whatever alteration done on the listview at draw time you need to set it in the getView like below, but if you want it to show on click you need to do it in an onClick method like below. 如果你想要在绘图时在列表视图上完成颜色/图像或任何更改,你需要在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);
            }
     }

My boolean check is just a boolean array like 我的布尔检查只是一个布尔数组

private boolean[] selected;

I initalize it in the my subclass of the ArrayAdapater constructor. 我在我的ArrayAdapater构造函数的子类中初始化它。

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

at first, you can create a color list in the adapter like this ->首先,您可以像这样在适配器中创建一个颜色列表 ->

you can check this image for better clarity...你可以检查这张图片以获得更好的清晰度......

Photo referencing the location of the lines written below照片参考下面所写线条的位置

var colorsList = arrayListOf<String>("#2ecc71", "#e67e22", "#2c3e50", "#e74c3c")

then add this logic (if condition) in onBindviewholder like this, here itemtxt is the id of card view ->然后像这样在onBindviewholder中添加这个逻辑(if condition),这里的itemtxt是card view的id ->

    if (position % 5 == 0) {
            holder.binding.itemtxt.setBackgroundColor(Color.parseColor(colorsList[0]))
        } else if (position % 5 == 1) {
            holder.binding.itemtxt.setBackgroundColor(Color.parseColor(colorsList[1]))
        } else if (position % 5 == 2) {
            holder.binding.itemtxt.setBackgroundColor(Color.parseColor(colorsList[2]))
        } else if (position % 5 == 3) {
            holder.binding.itemtxt.setBackgroundColor(Color.parseColor(colorsList[3]))
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM