【问题标题】:No ripple effect on recycleview touch eventrecycleview 触摸事件没有涟漪效应
【发布时间】:2015-06-21 11:06:52
【问题描述】:

我已经使用RecycleView 实现了抽屉式导航,但是当我单击项目时,没有显示任何涟漪效果。我的设备正在运行 API 22。

fragment_navigation.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white">


<RelativeLayout
        android:id="@+id/nav_header_container"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:layout_alignParentTop="true"
        android:background="@color/colorPrimary">

    <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/ic_profile"
            android:scaleType="fitCenter"
            android:layout_centerInParent="true" />

</RelativeLayout>


<android.support.v7.widget.RecyclerView
        android:id="@+id/drawerList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/nav_header_container"
        android:layout_marginTop="15dp" />


        </RelativeLayout>

我的行是:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true">
<ImageView android:layout_width="wrap_content"    android:layout_height="wrap_content"
           android:scaleType="fitCenter"
        android:src="@android:drawable/ic_input_get" android:id="@+id/imageView"/>
<TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="30dp"
        android:paddingTop="10dp"
        android:text="asa"
        android:paddingBottom="10dp"
        android:textSize="15dp"
        android:textStyle="bold"
        android:layout_alignParentTop="true" android:layout_toRightOf="@+id/imageView"
        android:layout_toEndOf="@+id/imageView"/>

    </RelativeLayout>

有什么问题,我该如何解决?

【问题讨论】:

  • 您设备的 API 版本是多少?
  • 谢谢@PhanDinhThai,API 22
  • 那么您是否为布局/项目元素应用了材质主题? developer.android.com/training/material/theme.html
  • 如果您确定您使用的是 Material 主题或 appCompat 主题,但仍然无法正常工作,请尝试将此行添加到行布局的根目录 android:background="?android:selectableItemBackground"
  • 当我点击项目时没有选择效果,所以主题不是答案

标签: android android-recyclerview ripple


【解决方案1】:

试试这个作为你的布局

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:focusable="true"
                android:clickable="true">

 <ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    android:src="@android:drawable/ic_input_get"
    android:id="@+id/imageView"/>

 <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="30dp"
            android:paddingTop="10dp"
            android:text="asa"
            android:paddingBottom="10dp"
            android:textSize="15dp"
            android:textStyle="bold"
            android:layout_toRightOf="@+id/imageView"
            android:layout_alignParentTop="true"  
            android:layout_toEndOf="@+id/imageView"/>

        </RelativeLayout>

【讨论】:

  • 不知道为什么更多的人没有发现这个有用,它确实节省了几个小时。
  • 很多人都在发布 selectableItemBackground 的其他方式,但这是唯一对我有用的方式!
猜你喜欢
  • 2015-11-07
  • 2018-02-23
  • 2018-01-07
  • 1970-01-01
  • 2016-04-25
  • 1970-01-01
  • 2014-09-11
  • 2018-11-13
  • 2020-10-02
相关资源
最近更新 更多