0

我的问题是当我CheckableLinearLayout被选中时,我从选择器中绘制的图像不能与未选中的图像重叠。

我的CheckableLinearLayout

public class CheckableLinearLayout extends LinearLayout implements Checkable {
    private boolean mChecked;
    private static final String TAG = CheckableLinearLayout.class
            .getCanonicalName();
    private static final int[] CHECKED_STATE_SET = 
        { android.R.attr.state_checked };

    public CheckableLinearLayout(final Context context) {
        super(context);
        setClickable(true);
        setLongClickable(true);
    }

    public CheckableLinearLayout(final Context context, 
                                 final AttributeSet attrs) {
        super(context, attrs);
        setClickable(true);
        setLongClickable(true);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    public CheckableLinearLayout(final Context context,
                                 final AttributeSet attrs, 
                                 final int defStyle) {
        super(context, attrs, defStyle);
        setClickable(true);
        setLongClickable(true);
    }

    @Override
    public void setChecked(final boolean checked) {
        mChecked = checked;
        refreshDrawableState();
    }

    @Override
    protected int[] onCreateDrawableState(final int extraSpace) {
        final int[] drawableState = 
            super.onCreateDrawableState(extraSpace + 1);
        if (isChecked())
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        return drawableState;
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        final Drawable drawable = getBackground();
        if (drawable != null) {
            final int[] myDrawableState = getDrawableState();
            drawable.setState(myDrawableState);
            invalidate();
        }
    }

    @Override
    public boolean performClick() {
        Toast.makeText(getContext(), "click", Toast.LENGTH_SHORT).show();
        if (isChecked()) {
            toggle();
            return false;
        } else
            return super.performClick();

    }

    @Override
    public boolean performLongClick() {
        toggle();
        Toast.makeText(getContext(), "long click", Toast.LENGTH_SHORT).show();
        return super.performLongClick();
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    @Override
    public Parcelable onSaveInstanceState() {
        // Force our ancestor class to save its state
        final Parcelable superState = super.onSaveInstanceState();
        final SavedState savedState = new SavedState(superState);
        savedState.checked = isChecked();
        return savedState;
    }

    @Override
    public void onRestoreInstanceState(final Parcelable state) {
        final SavedState savedState = (SavedState) state;
        super.onRestoreInstanceState(savedState.getSuperState());
        setChecked(savedState.checked);
        requestLayout();
    }

    // /////////////
    // SavedState //
    // /////////////
    private static class SavedState extends BaseSavedState {
        boolean checked;
        @SuppressWarnings("unused")
        public static final Parcelable.Creator<SavedState> CREATOR;
        static {
            CREATOR = new Parcelable.Creator<SavedState>() {
                @Override
                public SavedState createFromParcel(final Parcel in) {
                    return new SavedState(in);
                }

                @Override
                public SavedState[] newArray(final int size) {
                    return new SavedState[size];
                }
            };
        }

        SavedState(final Parcelable superState) {
            super(superState);
        }

        private SavedState(final Parcel in) {
            super(in);
            checked = (Boolean) in.readValue(null);
        }

        @Override
        public void writeToParcel(final Parcel out, final int flags) {
            super.writeToParcel(out, flags);
            out.writeValue(checked);
        }

        @Override
        public String toString() {
            return TAG + ".SavedState{"
                    + Integer.toHexString(System.identityHashCode(this))
                    + " checked=" + checked + "}";
        }
    }
}

我的layout.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/topics_preview_main_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/favorites_header_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:layout_marginTop="2dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/favorites_header_title"
            style="@style/CardText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="4dp"
            android:background="@drawable/card"
            android:gravity="left"
            android:paddingBottom="10dp"
            android:paddingLeft="5dp"
            android:paddingTop="10dp"
            android:text="@string/favorites_header" />

        <ui.CheckableLinearLayout
            android:id="@+id/favorites_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="4dp"
            android:background="@drawable/card_selector_w_shadow"
            android:clickable="true"
            android:longClickable="true"
            android:orientation="vertical"
            android:paddingBottom="16dp" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:paddingLeft="8dp"
                android:paddingRight="8dp" >

                <TextView
                    android:id="@+id/favorites_title"
                    style="@style/CardTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="title" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="4dp"
                android:background="@color/C_Favorites_Pink" />

            <LinearLayout
                android:id="@+id/favorites_description_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="vertical"
                android:padding="4dp" >

                <TextView
                    android:id="@+id/favorites_description"
                    style="@style/CardText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:ellipsize="end"
                    android:maxLines="4"
                    android:text="Lorem ipsum ..." />
            </LinearLayout>
        </ui.CheckableLinearLayout>
    </LinearLayout>

</FrameLayout>

card_selector_w_shadow -> 这个抽奖将调用选择器

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape
            android:dither="true"
            android:shape="rectangle" >
            <corners android:radius="2dp" />

            <solid android:color="#ccc" />
        </shape>
    </item>
    <item android:bottom="2dp">
        <shape
            android:dither="true"
            android:shape="rectangle" >
            <corners android:radius="2dp" />

            <solid android:color="@android:color/white" />

            <padding
                android:bottom="8dp"
                android:left="8dp"
                android:right="8dp"
                android:top="8dp" />
        </shape>
    </item>
    <item android:drawable="@drawable/drawable_states"/>

</layer-list>

我的drawable_states

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/checked_background" 
          android:state_checked="true"/>
</selector>

这就是正在发生的事情:

在此处输入图像描述

这就是我的意图:

在此处输入图像描述

我认为这是因为我CheckableLinearLayout只改变了孩子的状态,所以绘制只与孩子重叠。有什么建议吗?

4

0 回答 0