Android RecyclerView item选中放大被遮挡问题详解
作者:lbcab 发布时间:2023-04-11 17:58:31
标签:Android,RecyclerView,item
在Android TV上一般选中某个View, 都会有焦点突出放大的效果, 但是当在RecyclerView中(ListView或GridView)实现当item View执行放大动画后会被其他的item View遮挡.
原因是: RecyclerView的机制是越靠后的View z-order越高, 所以bringToFront方法是不管用的.
在实现针对TV端的自定义控件 TvRecyclerView 时遇到此问题, 最后的解决方案是:
自定义RecyclerView, 重写getChildDrawingOrder方法, 让选中的item最后绘制, 这样就不会让其他view遮挡.
public class ScaleRecyclerView extends RecyclerView {
private int mSelectedPosition = 0;
public ScaleRecyclerView(Context context) {
super(context);
init();
}
public ScaleRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public ScaleRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
//启用子视图排序功能
setChildrenDrawingOrderEnabled(true);
}
@Override
public void onDraw(Canvas c) {
mSelectedPosition = getChildAdapterPosition(getFocusedChild());
super.onDraw(c);
}
@Override
protected int getChildDrawingOrder(int childCount, int i) {
int position = mSelectedPosition;
if (position < 0) {
return i;
} else {
if (i == childCount - 1) {
if (position > i) {
position = i;
}
return position;
}
if (i == position) {
return childCount - 1;
}
}
return i;
}
}
最好还需要设置RecyclerView的父类的属性: clipChildren = false, clipToPadding = false, 避免边缘的子view被父类遮挡.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="245dp"
android:layout_centerInParent="true" />
</RelativeLayout>
使用介绍:
(1) 自定具有放大缩小的布局:
public class FocusRelativeLayout extends RelativeLayout {
private Animation scaleSmallAnimation;
private Animation scaleBigAnimation;
public FocusRelativeLayout(Context context) {
super(context);
}
public FocusRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FocusRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
if (gainFocus) {
getRootView().invalidate();
zoomOut();
} else {
zoomIn();
}
}
private void zoomIn() {
if (scaleSmallAnimation == null) {
scaleSmallAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_scale_small);
}
startAnimation(scaleSmallAnimation);
}
private void zoomOut() {
if (scaleBigAnimation == null) {
scaleBigAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_scale_big);
}
startAnimation(scaleBigAnimation);
}
}
(2) 放大动画xml配置如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false">
<scale
android:duration="150"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:repeatCount="0"
android:toXScale="1.08"
android:toYScale="1.08" />
</set>
(3) 主布局xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false">
<com.app.tvviewpager.ScaleRecyclerView
android:id="@+id/main_recyclerView"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
(4) 子视图的xml:
<FocusRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_main_layout"
android:layout_width="300dp"
android:layout_height="210dp"
android:focusable="true">
<TextView
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true" />
</FocusRelativeLayout>
(5) adapter配置:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private OnItemStateListener mListener;
private static int[] mColorIds = {R.color.amber, R.color.brown, R.color.cyan,
R.color.deepPurple, R.color.green, R.color.lightBlue, R.color.lightGreen,
R.color.lime, R.color.orange, R.color.pink, R.color.cyan, R.color.deepPurple};
MyAdapter(Context context) {
mContext = context;
}
public void setOnItemStateListener(OnItemStateListener listener) {
mListener = listener;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new RecyclerViewHolder(View.inflate(mContext, R.layout.item_recyclerview, null));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
final RecyclerViewHolder viewHolder = (RecyclerViewHolder) holder;
viewHolder.mRelativeLayout.setBackgroundColor(ContextCompat.getColor(mContext, mColorIds[position]));
}
@Override
public int getItemCount() {
return mColorIds.length;
}
private class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
FocusRelativeLayout mRelativeLayout;
RecyclerViewHolder(View itemView) {
super(itemView);
mRelativeLayout = (FocusRelativeLayout) itemView.findViewById(R.id.rl_main_layout);
mRelativeLayout.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onItemClick(v, getAdapterPosition());
}
}
}
public interface OnItemStateListener {
void onItemClick(View view, int position);
}
}
(6) Activity中的配置:
public class RecyclerActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);
final ScaleRecyclerView recyclerView = (ScaleRecyclerView) findViewById(R.id.main_recyclerView);
GridLayoutManager manager = new GridLayoutManager(RecyclerActivity.this, 1);
manager.setOrientation(LinearLayoutManager.HORIZONTAL);
manager.supportsPredictiveItemAnimations();
recyclerView.setLayoutManager(manager);
int itemSpace = getResources().
getDimensionPixelSize(R.dimen.recyclerView_item_space);
recyclerView.addItemDecoration(new SpaceItemDecoration(itemSpace));
final MyAdapter mAdapter = new MyAdapter(RecyclerActivity.this);
recyclerView.setAdapter(mAdapter);
}
private class SpaceItemDecoration extends RecyclerView.ItemDecoration {
private int space;
SpaceItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
outRect.left = space;
}
}
}
效果图如下:
来源:https://blog.csdn.net/lbcab/article/details/54576425


猜你喜欢
- C#与C++ dll之间传递字符串string wchar_t* char* IntPtr1、由C#向C++ dll 传入字符串时,参数直接
- java io操作中通常采用BufferedReader,BufferedInputStream等带缓冲的IO类处理大文件,不过java n
- 这篇文章主要介绍了SpringMVC Mybatis配置多个数据源并切换代码详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一
- 简介本文我们将会讨怎么在Spring Boot中使用Properties。使用Properties有两种方式,一种是java代码的注解,一种
- 本文实例为大家分享了java实现文件上传下载的具体代码,供大家参考,具体内容如下1.上传单个文件Controller控制层import ja
- 有很多应用场景,用到了接口动态实现,下面举几个典型的应用:1、mybatis / jpa 等orm框架,可以在接口上加注解进行开发,不需要编
- 干java 开发这么多年, 之前一直没留意java 进程还区分守护进程和用户进程。守护进程这个概念最早还是在linux系统中接触的,直到近期
- 上一篇JavaMail入门第三篇 发送邮件中,我们学会了如何用JavaMail API提供的Transport类发送邮件,同样,JavaMa
- 下面的示例提供对某个已存档类型的基本概述。示例// If compiling from the command line, compile
- 前言先说结论,tauri是一个非常优秀的前端桌面开发框架,但是,rust门槛太高了。一开始我是用electron来开发的,但是打包后发现软件
- 前言在之前的Spring Boot基础教程系列中,已经通过《Spring Boot中使用@Async实现异步调用》一文介绍过如何使用@Asy
- 介绍在分布式系统、微服务架构大行其道的今天,服务间互相调用出现失败已经成为常态。如何处理异常,如何保证数据一致性,成为微服务设计过程中,绕不
- 1.概述最近一直都在带实习生做项目,发现自己好久没有写博客了,这几天更新会比较频繁,今天玩QQ的时候发现QQ主页菜单滑动效果早就变了,实在忍
- SelectMany操作符提供了将多个from子句组合起来的功能,相当于数据库中的多表连接查询,它将每个对象的结果合并成单个序列。示例:st
- 在java项目开发中。最开始换行符大家一般是在idea中设置新文件为LF,并且对旧文件通过IDEA下方的点击来更换换行符。很显然,对于几千文
- 一、引入maven依赖Spring Boot默认使用LogBack,但是我们没有看到显示依赖的jar包,其实是因为所在的jar包spring
- mybatis自带对枚举的处理类org.apache.ibatis.type.EnumOrdinalTypeHandler<E>
- Android性能优化-布局优化今天,继续Android性能优化 一 编码细节优化。编码细节,对于程序的运行效率也是有很多的影响的
- 本文基于jdk1.8进行分析。LinkedList和ArrayList都是常用的java集合。ArrayList是数组,Linkedlist
- 本文实例讲述了Android开发之开关按钮控件ToggleButton简单用法。分享给大家供大家参考,具体如下:先来看看运行效果:具体代码如