Highlight ListView selected row

前端 未结 12 1486
情歌与酒
情歌与酒 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条回答
  •  温柔的废话
    2020-11-27 16:23

    Following solution can be used for highlighting one ore many list items. Starting point: There is a ListFragment to show a List of MyEntity via an ArrayAdapter implementation. The color of an TextView as part of the item layout has to change to grey, if the item is selected. Multiple items should be selectable.

    1. The ListFragment must provide an OnItemSelectListener(YourEntity item) that will be called in onListItemClicked().

      public void onListItemClick(ListView l, View v, int position, long id) {
        MyEntity entity = (MyEntity) getListView().getItemAtPosition(position);
        if (entityListSelectListener != null) 
          itemSelectListener.onItemSelect(entity); }
      
    2. The ArrayAdapter owns a List for the key fields for the selected items and provides the manager methods addSelection(MyEntity) and clearAllSelections(). The overriden method getView() reads the key field list and, if the actual position is in there, changes the particular TextView color to grey, otherwise to transparent.

      public View getView(int position, View convertView, ViewGroup parent) {
        ...
        if (selectedEntityList.contains(entity.key)) //... highlight view field }
      
    3. In onCreate() of the owning activity the listeners has to be implemented to manage the ArrayAdapter: calling addSelection() and notifyDataSetChanged().

      entityListFragment.setEntityListSelectListener(
        new EntityListFragment.OnItemSelectedListener() {
          public void onItemSelect(MyEntity entity) {
          aa.addSelection(entity);
          aa.notifyDataSetChanged(); }});
      

提交回复
热议问题