Android ListView State List not showing default item background

前端 未结 3 968
忘掉有多难
忘掉有多难 2020-12-15 08:54

Have read over a number of related questions here at SO, as well as looked through the Android docs and source to try to figure this out, but I am stumped, although given th

相关标签:
3条回答
  • 2020-12-15 09:07

    The listSelector is only used to specify a View that will be drawn in the same place as the selected item View. I.e., there is only one instance of the listSelector per ListView. You can specify if it can draw on top or not using drawSelectorOnTop.

    If you want all your Views to use a state list then you should specify that where the child Views are defined.

    I'm using the source for AbsListView as reference. Specifically AbsListView#setSelector(Drawable), AbsListView#positionSelector, AbsListView#drawSelector, and AbsListView#dispatchDraw

    AbsListView#drawSelector:

    private void drawSelector(Canvas canvas) {
            if (shouldShowSelector() && mSelectorRect != null && !mSelectorRect.isEmpty()) {
                final Drawable selector = mSelector;
                selector.setBounds(mSelectorRect);
                selector.draw(canvas);
            }
    }
    

    AbsListView#positionSelector:

    void positionSelector(View sel) {
        final Rect selectorRect = mSelectorRect;
        selectorRect.set(sel.getLeft(), sel.getTop(), sel.getRight(), sel.getBottom());
        positionSelector(selectorRect.left, selectorRect.top, selectorRect.right,
                selectorRect.bottom);
    
        final boolean isChildViewEnabled = mIsChildViewEnabled;
        if (sel.isEnabled() != isChildViewEnabled) {
            mIsChildViewEnabled = !isChildViewEnabled;
            refreshDrawableState();
        }
    }
    

    The source indicates that there is only one mSelector created/used and that it is positioned in the same bounding rect as the selected item.

    0 讨论(0)
  • 2020-12-15 09:15

    You can assign default background for root layout and selector_background for child, in this case if item not selected then default background not obscures the selector. Its work for me.

    0 讨论(0)
  • 2020-12-15 09:27

    Figured this out, the issue was that while I was setting the listSelector for the ListView to the State List correctly via android:listSelector="@drawable/list_selector", I also needed to set the background for the list item to another State List via android:background="@drawable/list_background"

    In the list_background.xml state list, I have:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:visible="true">
    
        <!-- the list items are disabled -->
    <item 
            android:state_window_focused="false"
            android:drawable="@drawable/shape_row_disabled" />
    
        <!-- the list items are enabled, in focus but not being touched/pressed (idle) -->
        <item 
            android:state_window_focused="true"
            android:state_pressed="false"
            android:state_focused="false"
            android:state_selected="false"
            android:drawable="@drawable/shape_row" />
    
        <!-- the catch-all -->
        <item 
            android:drawable="@android:color/transparent" />
    </selector>
    

    Found the answer here:

    Changing background color of ListView items on Android

    In conjunction with my list selector above, it works perfectly.

    0 讨论(0)
提交回复
热议问题