android开发分享包含ImageView的RecyclerView项目的涟漪效应

我有一个RecyclerView展开下面的网格项目:

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/children_tile_border" android:layout_marginTop="@dimen/children_tile_border" android:clickable="true" android:focusable="true" android:background="?selectableItemBackground" tools:ignore="RtlHardcoded"> <com.example.app.ui.widget.SquareImageView android:id="@+id/child_picture" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="visible" android:layout_alignParentBottom="true"/> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="68dp" android:background="@color/tile_text_background" android:gravity="center_vertical" android:layout_alignParentBottom="true"> .............. </LinearLayout> </RelativeLayout> 

但是除非我将SquareImageView's可见性设置为不可见,否则不会出现涟漪效应。 似乎涟漪效应绘制在SquareImageView下方。 我怎样才能让它在SquareImageView绘制?

    将以下代码添加到您的父级布局

     android:clickable="true" android:focusable="true" android:background="?attr/selectableItemBackground" 

    如果你想要自定义涟漪效应,请在你的drawable-v21中添加这个ripple_custom.xml

     <?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="https://schemas.android.com/apk/res/android" android:color="@color/colorHighlight"> <item android:id="@android:id/mask" android:drawable="@color/windowBackground" /> </ripple> 

    支持旧版本添加ripple_custom.xml中绘制

     <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="https://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape android:shape="rectangle"> <solid android:color="@color/colorHighlight" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="@android:color/transparent" /> </shape> </item> </selector> 

    更改:

     android:background="?selectableItemBackground" 

    至:

     android:background="?android:attr/selectableItemBackground" 

    并将其添加到您的SquareImageView

     android:clickable="false" 

    我们可以在FrameLayout包装RecyclerView Item并设置android:foreground属性。 详细信息请查看以下链接 。 它适合我很好。

    你的SquareImageView正在填充整个空间( widthmatch_parentheightwrap_content ),所以SquareImageView接收到click事件。

    在我的情况下工作是将您的RelativeLayout的所有对象的可点击的状态设置为false:

     android:clickable="false" 

    只要确保您的RelativeLayout处理您的活动中的点击事件。 在我的代码中,我将RelativeLayout设置为一个view v并在其上设置onClick Listener来处理我的List项目中的CheckBox

     v.setOnClickListener(this); 

    希望这可以帮助读者阅读。

    如果你有一个CardView + ImageView
    只需插入CardView

     android:focusable="true" android:foreground="?android:attr/selectableItemBackground" 

    而对于RelativeLayout + ImageView
    插入RelativeLayout

     android:clickable="true" android:focusable="true" android:background="?attr/selectableItemBackground" 

    要么

     android:focusable="true" android:background="?attr/selectableItemBackground" 

    此代码为我工作

    以上就是android开发分享包含ImageView的RecyclerView项目的涟漪效应相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/512850.html

      (0)
      上一篇 2020年11月27日
      下一篇 2020年11月27日

      精彩推荐