简体   繁体   English

RecyclerView 中的焦点效果 - 如何将项目置于前面?

[英]Focus effect in a RecyclerView - how to bring to front an item?

I want to create a "focus" effect once the user clicks an item in the recycler view.一旦用户单击回收站视图中的项目,我想创建一个“焦点”效果。 Look at the image below, the second one is the effect I wish to have.看下图,第二张是我想要的效果。

例子

I've tried to get the RecyclerView's clicked item and bring it to the front and show a dark/transparent overlay covering all the others.我试图获得 RecyclerView 的点击项目并将其带到前面并显示覆盖所有其他项目的深色/透明叠加层。

This is my view hierarchy:这是我的视图层次结构:

RelativeLayout相对布局

--- Recycler View --- 回收者视图

--- View (dark overlay) --- 视图(深色叠加)

I've tried with view.bringToFront();我试过view.bringToFront(); , view.setZ(); , view.setZ(); , view.setElevation(); , view.setElevation(); but none of them works.但它们都不起作用。

Now I guess this is about the layout hierarchy.现在我想这是关于布局层次结构的。 How to solve this problem?如何解决这个问题呢?

you can just change the background color of touch item view and text color.您可以更改触摸项目视图的背景颜色和文本颜色。 when user click on it .当用户点击它时。

I achieved this effect by sending the item view to the Window.DecorView.我通过将项目视图发送到 Window.DecorView 来实现此效果。

You can just make a copy of the desired view you want to put in the front (in this case you want the layout used by the ViewHolder) and then您可以复制想要放在前面的所需视图(在这种情况下,您需要 ViewHolder 使用的布局),然后

((ViewGroup)activity.getWindow().getDecorView()).addView(yourView);

after that you can set Translations according to the original view.之后,您可以根据原始视图设置翻译。 The new view will be in front of the dark overlay in the same place the previous view is新视图将位于上一个视图所在的同一位置的深色覆盖前面

EDIT: I have a sample of what you can do: The following code is C# (using Xamarin.Android), it should be easy to turn to Java.编辑:我有一个您可以做什么的示例:以下代码是 C#(使用 Xamarin.Android),转向 Java 应该很容易。

        var productView = productsLayoutManager.FindViewByPosition(position);
        var cardView = productView.FindViewById<CardView>(Resource.Id.product_layout);

        int[] location = new int[2];
        productView.GetLocationOnScreen(location);
        int left = location[0];
        int top = location[1];

        selectedView = LayoutInflater.Inflate(Resource.Layout.ProductRow2, null);
        productsAdapter.CloneItemViewHolder(productView, ref selectedView);

        selectedView.LayoutParameters = new FrameLayout.LayoutParams(cardView.LayoutParameters);
        ((ViewGroup)activity.Window.DecorView).AddView(selectedView, cardView.Width, cardView.Height);

        selectedView.SetX(left);
        selectedView.SetY(top);
        selectedView.RequestLayout();
        activity.DimBackground();

The cardView is the main layout of the RecyclerView item Viewholder. cardView 是 RecyclerView 项 Viewholder 的主要布局。

This is actual code in use, producing this:这是正在使用的实际代码,生成如下: 该图像显示了一个 recyclerView。其中一项已被选中,正在显示焦点效果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM