Android 在RecyclerView中放大项目以重叠2个相邻项目
我正在使用RecyclerView和GridLayoutManager。我想要达到的目标是,当我点击一个项目时,它会放大并重叠相邻的项目。如下图所示(在Android TV中) 当onClick事件被触发时,我调用Android 在RecyclerView中放大项目以重叠2个相邻项目,android,android-recyclerview,Android,Android Recyclerview,我正在使用RecyclerView和GridLayoutManager。我想要达到的目标是,当我点击一个项目时,它会放大并重叠相邻的项目。如下图所示(在Android TV中) 当onClick事件被触发时,我调用 v.animate().scaleX(1.2f).scaleY(1.2f).setDuration(500).start(); 但结果如下: 它只能与位置低于自身的项目重叠。 我应该做什么来重叠所有相邻的项目。提前感谢。 编辑 我已经试过了: v.bringToFront();
v.animate().scaleX(1.2f).scaleY(1.2f).setDuration(500).start();
但结果如下:
它只能与位置低于自身的项目重叠。我应该做什么来重叠所有相邻的项目。提前感谢。
编辑
我已经试过了:
v.bringToFront();
或
但是这两种方法都不起作用。在运行动画之前,尝试调用
v.bringToFront()
。如果不起作用,另一种变体是使用
View#setElevation(float val)
where val>0。缺点是,取消选择项目时,需要将“标高”设置回0。您需要使用与普通视图组相同的方法,替代子视图的绘制顺序。此示例强制第一个视图始终最后绘制-您需要针对您的用例进行自定义
public TopForcingRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setChildrenDrawingOrderEnabled(true);
}
@Override protected int getChildDrawingOrder(int childCount, int i) {
if (i >= childCount - 1) return 0;
//when asked for the last view to draw, answer ZERO
else return i + 1;
//when asked for the i-th view to draw, answer "the next one"
}
注意一个一个的错误。根据aga的回答,我使用ViewCompat.setElevation(View-View,float-elevation)来支持21之前的API,它的工作方式很有魅力
static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View root) {
// bind views
// ...
// bind focus listener
root.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// run scale animation and make it bigger
ViewCompat.setElevation(root, 1);
} else {
// run scale animation and make it smaller
ViewCompat.setElevation(root, 0);
}
}
});
}
}
项目布局文件非常简单,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/sl_live_item" />
1.重写getChildDrawingOrder方法
@Override
protected int getChildDrawingOrder(int childCount, int i) {
View view = getLayoutManager().getFocusedChild();
if (null == view) {
return super.getChildDrawingOrder(childCount, i);
}
int position = indexOfChild(view);
if (position < 0) {
return super.getChildDrawingOrder(childCount, i);
}
if (i == childCount - 1) {
return position;
}
if (i == position) {
return childCount - 1;
}
return super.getChildDrawingOrder(childCount, i);
}
3.当项目视图有焦点时,使其父项无效
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.setScaleX(1.5f);
v.setScaleY(1.5f);
mRecyclerView.invalidate();
} else {
v.setScaleX(1.0f);
v.setScaleY(1.0f);
}
}
它起作用了。但是视图#设置高程(浮点值)需要API 21。我的应用程序也支持API 15。还有其他的解决方法吗。Thanks@HuyDuongTu你可以查看我的答案,它使用ViewCompat.setElevation(View View,float elevation)来支持21世纪之前的API,它工作得很好。我认为ViewCompat.setElevation(View,View,float elevation)在Android Kitkat中实际上没有任何作用,只是为了防止在构建时出错。你解决了这个问题吗?我也面临同样的问题,thx。使用
Carousel
。它最适合这个要求。。!!。。参考:您找到解决方案了吗?能否共享您的布局文件?我已经使用了ViewCompat.setElevation(),但它没有任何帮助…不,无用。ViewCompat不能执行低于21的任何操作。public static void setElevation(@NonNull View View,float elevation){if(Build.VERSION.SDK_INT>=21){View.setElevation(elevation);}}您也可以使用recyclerview.setChildDrawingOrderCallback
而不使用继承和重写recyclerview
方法。@ramineftekhari!此方法仍与当前焦点项目上的下一个视图重叠。目标是使当前项与其他项重叠。
setChildrenDrawingOrderEnabled(true);
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.setScaleX(1.5f);
v.setScaleY(1.5f);
mRecyclerView.invalidate();
} else {
v.setScaleX(1.0f);
v.setScaleY(1.0f);
}
}