【问题标题】:Propagate gridview item checkable state to children将 gridview 项目可检查状态传播给子级
【发布时间】:2014-01-08 17:40:06
【问题描述】:

我定义了一个 Gridview 项,如下所示,我扩展了 RelativeLayout 以使其可检查,这工作正常。

现在我还希望 ImageView(在 RelativeLayout 内)接收可检查状态,所以我扩展了它,但永远不会调用 setChecked 方法,并且图像永远不会改变其样式

我尝试将 duplicateParentState 设置为 true 添加到 ImageView,但没有成功。

可能我的做法是完全错误的,在检查父RelativeLayout时如何更改ImageView的样式?

grid_view_item.xml

<?xml version="1.0" encoding="utf-8"?>
<com.my.CheckableRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gridview_selector"
    android:orientation="vertical" >

    <com.my.CheckableImageView
        android:id="@+id/thumbnail_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/place_holder" />

    <TextView
        android:id="@+id/caption"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/thumbnail_image"
        android:layout_centerHorizontal="true" />

</com.my.CheckableRelativeLayout>

CheckableRelativeLayout

public class CheckableRelativeLayout extends RelativeLayout implements Checkable {
    private boolean isChecked;

    /** all ctors removed for simplicity */

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean isChecked) {
        this.isChecked = isChecked;
        setSelected(isChecked);
    }

    public void toggle() {
        setChecked(!isChecked);
    }
}

CheckableImageView

public class CheckableImageView extends ImageView implements Checkable {
    private boolean isChecked;

    /** all ctors removed for simplicity */

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

    @Override
    public void setChecked(boolean checked) {
        isChecked = checked;
        setImageAlpha(isChecked ? 100 : 255);
    }

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

【问题讨论】:

    标签: android android-layout android-imageview android-gridview


    【解决方案1】:

    感谢这个隐藏的(在网络上)gem post,我自己找到了解决方案

    诀窍在于为所有Checkable 孩子调用setChecked,所以我只是在CheckableRelativeLayout 中添加了相关代码

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 2019-03-07
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多