ListView item background via custom selector

前端 未结 10 1431
深忆病人
深忆病人 2020-11-22 08:19

Is it possible to apply a custom background to each Listview item via the list selector?

The default selector specifies @android:color/transparent for t

相关标签:
10条回答
  • 2020-11-22 09:16

    I always use the same method and it works every time, every where: I simply use a selector like this

    <item android:state_activated="true"  android:color="@color/your_selected_color" />
    <item android:state_pressed="true" android:color="@color/your_pressed_color" />
    <item android:color="@color/your_normal_color"></item>
    

    and set on the ListView (THIS IS VERY IMPORTANT TO MAKE IT WORKING) the attribute

    android:choiceMode="singleChoice"
    

    for the textColor just put in the color folder(IMPORTANT, not drawable folder!) a selector like this

    <item android:state_activated="true"  android:color="@color/your_selected_textColor" />
    <item android:state_pressed="true" android:color="@color/your_pressed_textColor" />
    <item android:color="@color/your_normal_textColor"></item>
    

    this a sample row template

    <ImageView
        android:id="@+skinMenu/lblIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:src="@drawable/menu_catalog" />
    
    <TextView
        android:id="@+skinMenu/lblTitle"
        style="@style/superlabelStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:gravity="center_vertical"
        android:text="test menu testo"
        android:textColor="@color/menu_textcolor_selector"
        android:textSize="20dp"
        android:textStyle="bold" />
    

    everything showld work without tedious workarounds. hope this help

    0 讨论(0)
  • 2020-11-22 09:16

    FrostWire Team over here.

    All the selector crap api doesn't work as expected. After trying all the solutions presented in this thread to no good, we just solved the problem at the moment of inflating the ListView Item.

    1. Make sure your item keeps it's state, we did it as a member variable of the MenuItem (boolean selected)

    2. When you inflate, ask if the underlying item is selected, if so, just set the drawable resource that you want as the background (be it a 9patch or whatever). Make sure your adapter is aware of this and that it calls notifyDataChanged() when something has been selected.

          @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View rowView = convertView;
          if (rowView == null) {
              LayoutInflater inflater = act.getLayoutInflater();
              rowView = inflater.inflate(R.layout.slidemenu_listitem, null);
              MenuItemHolder viewHolder = new MenuItemHolder();
              viewHolder.label = (TextView) rowView.findViewById(R.id.slidemenu_item_label);
              viewHolder.icon = (ImageView) rowView.findViewById(R.id.slidemenu_item_icon);
              rowView.setTag(viewHolder);
          }
      
          MenuItemHolder holder = (MenuItemHolder) rowView.getTag();
          String s = items[position].label;
          holder.label.setText(s);
          holder.icon.setImageDrawable(items[position].icon);
      
          //Here comes the magic
          rowView.setSelected(items[position].selected);
      
          rowView.setBackgroundResource((rowView.isSelected()) ? R.drawable.slidemenu_item_background_selected : R.drawable.slidemenu_item_background);
      
          return rowView;
      }
      

    It'd be really nice if the selectors would actually work, in theory it's a nice and elegant solution, but it seems like it's broken. KISS.

    0 讨论(0)
  • 2020-11-22 09:17

    I'm not sure how to achieve your desired effect through the selector itself -- after all, by definition, there is one selector for the whole list.

    However, you can get control on selection changes and draw whatever you want. In this sample project, I make the selector transparent and draw a bar on the selected item.

    0 讨论(0)
  • 2020-11-22 09:19

    It's enough,if you put in list_row_layout.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:background="@drawable/listitem_background">... </LinearLayout>
    

    listitem_selector.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/dark" android:state_pressed="true" />
        <item android:drawable="@color/dark" android:state_focused="true" />
        <item android:drawable="@android:color/white" />
    </selector>
    
    0 讨论(0)
提交回复
热议问题