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
0
投稿
猜你喜欢
- 本文实例讲述了C#实现的文件上传下载工具类。分享给大家供大家参考,具体如下:这里给出的工具类是在VS2013环境下采用C#语言实现文件上传、
- 前言 今天在看Android ContentProvider实现的时候,突然想到了Java类在new的过
- 前言我们常说的字符串为空,其实就是一个没有字符的空数组。比如:String a = "";a 就可以称为是一个空字符串。
- 1.object.toString()方法这种方法要注意的是object不能为null,否则会报NullPointException,一般别
- 合并有序数组的实现java版本:实例代码public class Merge {//合并有序数组 public static v
- 翻译自 Manju lata Yadav 2019年6月2日 的博文 《Difference Between Struct And Clas
- FPS是什么?FPS (每秒传输帧数(Frames Per Second))【摘自百度百科】FPS是图像领域中的定义,是指画面每秒传输帧数,
- 前言emmm… 有个需求,需要根据信息生成svg,因为考虑到样式一致性的问题最终决定有服务端来生成svg。Java提供
- 最近在开发中遇到了这样一个问题,在下拉刷新组件中包含了一个轮播图组件,当左右滑动的图片时很容易触发下拉刷新,如下图所示:如图中红色箭头所示方
- Spring流程Debug1.1 Spring测试环境搭建Spring模块概览,绿色是模块,Spring中八大模块,黑色表示该模块包含的ja
- we can custom min heap or max heap by override the method compare.pack
- 本文实例为大家分享了Android实现蒙板效果的相关代码,供大家参考,具体内容如下1、不保留标题栏蒙板的实现效果:原理:1、弹窗时,设置背景
- 本文实例为大家分享了C语言非递归后序遍历二叉树的具体代码,供大家参考,具体内容如下法一:实现思路:一个栈 先按 根->右子树->
- 我们经常会有用到,当A 用户在北京登录 ,然后A用户在天津再登录 ,要踢出北京登录的状态。如果用户在北京重新登录,那么又要踢出天津的用户,这
- 前言本文主要给大家介绍了关于Java中Arraylist动态扩容的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
- 守护线程在Java中有两类线程User Thread(用户线程)Daemon Thread(守护线程)守护线程的功能非常简单,在其本身是一个
- 大家都知道在C#里面,我们可以使用Thread.Start方法来启动一个线程,当我们想停止执行的线程时可以使用Thread.Abort方法来
- 本文实例为大家分享了java数独游戏的具体代码,供大家参考,具体内容如下自己写的数独游戏,共9关,代码如下:1、DoShudu类用于产生数独
- 线程的两种创建方式及优劣比较1、通过实现Runnable接口线程创建(1).定义一个类实现Runnable接口,重写接口中的run()方法。
- 一、settings.xml文件会在两个目录下存在:1、Maven安装目录(全局):%MAVEN_HOME%\conf\settings.x