How to do custom ListView with colorful items' backgrounds?

前端 未结 3 511
没有蜡笔的小新
没有蜡笔的小新 2021-01-05 06:15

I have created an ArrayList> collection to hold my data for ListView. I\'m using SimpleAdapter.

3条回答
  •  执念已碎
    2021-01-05 06:27

    You override getView in your adapter to make changes to the view. Keep in mind that ListView reuses the view implementations, so if you change the color to item 10, make sure you set the color to the opposite for all other views.

    e.g.

    new SimpleAdapter( ... ) {
      @Override
      public View getView (int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        if (position == 10) {
          // set background color = red;
        } else {
          // set background color = green;
        }
        return view;
      }
    }
    

提交回复
热议问题