Android ListView State List not showing default item background

前端 未结 3 981
忘掉有多难
忘掉有多难 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.

提交回复
热议问题