【问题标题】:Android - How to apply selectableItemBackground to entire ListView item?Android - 如何将 selectableItemBackground 应用于整个 ListView 项目?
【发布时间】:2021-09-24 04:46:31
【问题描述】:

我是 Android 新手,现在正在编写我的第一个应用程序。我有一个 ListView 呈现一系列项目,每个项目都有多个 TextView。我希望 ListView 中的每个项目都可以点击,并为点击设置动画。动画是我正在努力解决的问题。

我有 selectableItemBackground 属性,它可以在 TextView-by-TextView 的基础上完美运行。但是,这意味着点击动画只出现在特定的 TextView 上,而我希望它出现在整个列表项本身上。

这是我的 Android 视图。我知道这种行为是由于我将 selectableItemBackground 属性放在 TextView 元素上造成的,我只是想不出还有什么地方可以达到我想要的效果。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:gravity="center">
    <TextView
        android:id="@+id/past_game_update_timestamp"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:clickable="true"
        android:padding="@dimen/small_padding"
        android:textSize="@dimen/small_font_size" />
    <TextView
        android:id="@+id/past_game_start_timestamp"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:clickable="true"
        android:padding="@dimen/small_padding"
        android:textSize="@dimen/small_font_size" />
</LinearLayout>

【问题讨论】:

    标签: android


    【解决方案1】:

    作为我从你的问题中理解的一部分。您是否希望整个视图都具有 onClick 动画。 为此,您有两个选择:

    在大多数情况下,您应该在视图 XML 中应用此功能,方法是将视图背景指定为:

    1. ?android:attr/selectableItemBackground 用于有界波纹。
    2. ?android:attr/selectableItemBackgroundBorderless 的涟漪 延伸到视野之外。它将被最近的父级绘制并受其约束 具有非空背景的视图。

    注意:selectableItemBackgroundBorderless 是新引入的属性 在 API 级别 21 中。

    请检查选项 2。

    【讨论】:

    • 我尝试将 #1 放在 LinearLayout 上,但它似乎不起作用。你能告诉我如何在 XML 中应用它吗?
    • NM,我想通了并发布了完整的解决方案。
    【解决方案2】:

    好的,在我对上述答案发表评论后,我想通了其余部分。我需要使 LinearLayout 可点击和可聚焦,并且我需要在 TextViews 上禁用可点击/可聚焦。我还需要将它们设置为 duplicateParentState。所有这些都允许 selectableItemBackground 在 LinearLayout 上工作。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:foreground="?android:attr/selectableItemBackground"
        android:focusable="true"
        android:clickable="true"
        android:gravity="center"
        android:padding="@dimen/small_padding">
        <TextView
            android:id="@+id/past_game_update_timestamp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:duplicateParentState="true"
            android:focusable="false"
            android:clickable="false"
            android:textSize="@dimen/small_font_size" />
        <TextView
            android:id="@+id/past_game_start_timestamp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:duplicateParentState="true"
            android:focusable="false"
            android:clickable="false"
            android:textSize="@dimen/small_font_size" />
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      您可以将?android:attr/selectableItemBackground 移动到项目布局的根视图中。

      像这样:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:layout_gravity="center"
          android:gravity="center"
          android:focusable="true"
          android:clickable="true"
          android:background="?android:attr/selectableItemBackground">
      
          <TextView
              android:id="@+id/past_game_update_timestamp"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:padding="@dimen/small_padding"
              android:textSize="@dimen/small_font_size" />
          <TextView
              android:id="@+id/past_game_start_timestamp"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:padding="@dimen/small_padding"
              android:textSize="@dimen/small_font_size" />
      </LinearLayout>
      

      别忘了添加android:focusable="true"android:clickable="true"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-25
        • 2020-06-27
        • 1970-01-01
        • 2018-12-25
        • 1970-01-01
        相关资源
        最近更新 更多