Highlight ListView selected row

前端 未结 12 1512
情歌与酒
情歌与酒 2020-11-27 16:04

I have a list of albums (several hundred). When I touch the selected album I want to offer the user a choice of playing the whole album, or moving to its track ListView. No

12条回答
  •  猫巷女王i
    2020-11-27 16:22

    In my case, when using simple_list_item_2, the only solution was to override the getView() method of the adapter, and just change the background color manually:

    listview = new SimpleAdapter(ctx, data,
                    android.R.layout.simple_list_item_2, res,
                    new String[] {"field1", "field2" },
                    new int[] {android.R.id.text1, android.R.id.text2 })
    
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);
    
                    if (this_is_the_selected_item)
                    {
                        view.setBackgroundColor(0xff999999);
                    }
                    else
                    {
                        view.setBackgroundColor(Color.BLACK);
                    }
                    return view;
                }
            };
    

    Remember to call listview.invalidateViews() whenever you change the selected item.

提交回复
热议问题