【问题标题】:Change TextView background with ripples用涟漪改变 TextView 背景
【发布时间】:2016-02-12 23:31:28
【问题描述】:

我有一个 RecyclerView(网格布局),其元素是 TextView。当用户单击 TextView 时,我会更改背景颜色。我允许多项选择。所有这一切都很好。现在我有一个新要求,即当用户单击 TextView 时显示波纹。所以基本上我必须完成两件事:

  1. 显示波纹
  2. 更改 TextView 的背景

我该如何做到这一点?我尝试在 RecyclerView (xml) 中使用android:drawSelectorOnTop="true”,但这没有任何区别。

所以想象一下,我必须在蓝色和绿色之间更改 TextView 的背景。但最重要的是,我必须始终显示onLongClick 的涟漪

【问题讨论】:

  • 也许我必须通过 GridLayoutManager 的装饰器添加波纹?怎么样?
  • 也许这就是答案:为什么有些按钮会在 longClick 上产生波纹而有些则不会?例如 CheckBox 在 click 和 longClick 上都会产生涟漪。而普通按钮不会在 longClick 上产生涟漪,但会在短点击时产生涟漪

标签: android android-layout android-recyclerview ripple


【解决方案1】:

假设您使用的是 Android 支持库,请将您的项目布局的 android:background 更改为 ?attr/selectableItemBackground 或 ?attr/selectableItemBackgroundBorderless,这将在 Lollipop+ 设备上应用波纹并回退到较低设备上的完整项目选择。

【讨论】:

  • 我的项目是所述的 TextView
  • 因此,将TextView 的android:background 属性更改为?attr/selectableItemBackground 或?attr/selectableItemBackgroundBorderless,您将得到所述的涟漪:-D
  • 感谢您的帮助,但您可能误读了这个问题。 textView 有一个 onLongClickListener。当长按发生时,我必须在绿色和蓝色之间切换 textView 的背景。你有看到? textView.setBackgroundColor(resources.getColor(R.color.colorPrimary)); 是选项之一。因此,一旦适配器开始绑定,xml 分配将立即丢失。
  • @NouvelTravay 所以更进一步。将TextView 包裹在FrameLayout 中。将TextView 的背景设置为?attr/selectableItemBackground 并在FrameLayout 上调用frameLayout.setBackgroundColor(resources.getColor(R.color.colorPrimary));,因为?attr/selectableItemBackground 和?attr/selectableItemBackgroundBorderless 是透明的...
  • 谢谢。我知道了。 +1。除了一个问题:波纹自然适用于点击而不是长点击。您(或正在观看的任何人)知道如何使用 longClick 制作波纹吗?
猜你喜欢
  • 2019-07-12
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
  • 2014-04-08
  • 2016-11-14
  • 2018-11-13
相关资源
最近更新 更多