【问题标题】:How can I have an ImageView selector when using Picasso (Square) on Android?在 Android 上使用 Picasso (Square) 时如何拥有 ImageView 选择器?
【发布时间】:2014-06-07 03:29:22
【问题描述】:

我知道 Picasso 设置了“src”,因此让“image.background”保持平静,但我似乎无法让 ImageView 响应选择器(如触摸它以显示标准全息选择器或任何自定义一)。

有人有工作样本吗?我肯定错过了什么。我尝试了不同的选择器,但没有看到触摸状态。

我已经尝试在 onClick() 中执行 setselected(true),但我知道如果存在具有以下状态的可绘制对象,则视图会自动处理:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_window_focused="false" android:drawable="@android:color/transparent" />

  <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
  <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/abc_list_selector_disabled_holo_light" />
  <item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@drawable/abc_list_selector_disabled_holo_light" />
  <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/abc_list_selector_background_transition_holo_light" />
  <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/abc_list_selector_background_transition_holo_light" />
  <item android:state_focused="true"                                                             android:drawable="@drawable/abc_list_focused_holo" />

</selector>

加载图片的Java代码是:

Picasso.with(mContext).load(url).error(R.drawable.error).fit().into(holder.someIV);

someIV 的 XML 看起来像:

<ImageView
    android:id="@+id/list_item_image"
    android:layout_width="match_parent"
    android:layout_height="@dimen/mixset_list_image_size"
    android:layout_marginTop="1dp"
    android:layout_marginLeft="1dp"
    android:layout_marginRight="1dp"
    android:contentDescription="@string/mixes"
    android:scaleType="centerCrop"
    android:background="@drawable/mix_list_art_selector"
    android:cropToPadding="false" />

背景设置为上述选择器。

有什么想法吗?谢谢。

【问题讨论】:

  • 也许您的图像覆盖了背景高光?试试this answer
  • 成功了。谢谢!

标签: android android-imageview picasso


【解决方案1】:

这就是我所做的:

我想强调的是一个 ImageView,它是 list_item 布局的一部分(从技术上讲,是一行)。

但我不想突出显示整行,只是其中的一个图像。

我必须将 ImageView 包装在 FrameLayout 中。

  <FrameLayout
    android:id="@+id/list_item_image_wrapper"
    android:layout_width="match_parent"
    android:layout_height="@dimen/mixset_list_image_size"
    android:clickable="true"
    android:foreground="@drawable/mix_list_art_selector">
  <ImageView
      android:id="@+id/list_item_image"
      android:layout_width="match_parent"
      android:layout_height="@dimen/mixset_list_image_size"
      android:layout_marginTop="1dp"
      android:layout_marginLeft="1dp"
      android:layout_marginRight="1dp"
      android:contentDescription="@string/mixes"
      android:scaleType="centerCrop"
      android:clickable="false"
      android:focusable="false"
      android:cropToPadding="false" />
</FrameLayout>

... 并将 Click Listeners 移动到 FrameLayout。

感谢@jason_t 提供解决方案的指针。

请注意,对于 TextView 等其他小部件,可能不需要上述内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    相关资源
    最近更新 更多