ViewGroup点击时修改内部View状态或透明度

编程入门 行业动态 更新时间:2024-10-05 17:21:31

ViewGroup点击时修改内部View状态或<a href=https://www.elefans.com/category/jswz/34/1753609.html style=透明度"/>

ViewGroup点击时修改内部View状态或透明度

使用ViewGroup包裹View作为RecyclerView等列表控件中item的布局是一种常见的场景,点击item的时候如果希望修改子View的透明度则可以给重写ViewGroup,修改setPressed(boolean pressed)方法,在pressed参数为true时,给所有的子View设置透明度或者设置其他按压时需要显示的状态,在pressed参数为false时恢复为之前的状态即可,下面是一个自定义FrameLayout,实现了按压状态时内部所有View透明度的修改,但要记得给FrameLayout设置点击事件OnClickListener,否则看不到效果

/*** 给子view设置点击后透明度变化*/public class PressAlphaFrameLayout extends FrameLayout {public PressAlphaFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public PressAlphaFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overridepublic void setPressed(boolean pressed) {super.setPressed(pressed);if (pressed) {for (int i = 0; i < getChildCount(); i++) {View view = getChildAt(i);view.setAlpha(0.6f);}} else {for (int i = 0; i < getChildCount(); i++) {View view = getChildAt(i);view.setAlpha(1.0f);}}}
}

更多推荐

ViewGroup点击时修改内部View状态或透明度

本文发布于:2024-02-14 00:46:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1761134.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:透明度   状态   ViewGroup   View

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!