在列表视图中设置不同的颜色分给每个元素视图、元素、颜色、不同

2023-09-07 08:47:08 作者:放我走

我想有一个与列表元素之间的不同分隔的列表。 我已经使用这个code定义一个白色的分频器高度1:

I want to have a list with different dividers between the list elements. I have used this code to define a white divider with height 1:

_listView.setDivider(new ColorDrawable(Color.WHITE));
_listView.setDividerHeight(1);

然而,它设置分隔的所有元素是白色的,我只希望他们中的一些是白色的,而另一个不同的颜色。

However it sets the divider for all the element to be white, and I want only some of them to be white and the other in different color.

我怎样才能做到这一点?

How can i do that?

推荐答案

设置分频器的高度为0,每一次实现您的项目布局视图与1的高度,并改变其颜色根据列表项的看法构建。

Set the divider to height to 0 and implement a View in your item layout with the height of 1 and change its color based on the list item every time the view is built.

下面是一个XML布局示例:

Here's an XML layout sample:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

    <View 
        android:id="@+id/divider"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="#FFFFFFFF" />

</LinearLayout>

这是你如何改变颜色的适配器:

And this is how you change the color in the adapter:

public class MyAdapter extends BaseAdapter {
    /** List of items to show */
    private ArrayList<String> items = new ArrayList<String>();
    private Context context;
    private int color[];

    public OffersAdapter(Context c, ArrayList<String> items, int color[])
    {
        super();
        this.context = c;
        this.items = items;
        this.color = color;
    }

    public int getCount() {
        return items.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;

    if(null == convertView)
    {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.list_item, parent, false);

        viewHolder.text = (TextView) convertView.findViewById(R.id.text);
        viewHolder.divider = (View) convertView.findViewById(R.id.divider);

        convertView.setTag(viewHolder);
    } else {
        //Retrieve the current view
        viewHolder = (ViewHolder) convertView.getTag(); 
    }

    //This is where you chance the divider color based on the item  
    viewHolder.divider.setBackgroundColor(color[position]);

  viewHolder.text.setText(items.get(position));

    return convertView;
}

//Holds the current view
 private static class ViewHolder {
         public TextView text;
         public View divider;
     }   
}

其中, INT颜色[] 是您要使用的颜色列表。

Where int color[] is a list of the colors you want to use.

更多关于ViewHolder这里阅读。