Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 滚动时,Recyclerview标高会逐渐更改_Android_Android Recyclerview_Android Elevation - Fatal编程技术网

Android 滚动时,Recyclerview标高会逐渐更改

Android 滚动时,Recyclerview标高会逐渐更改,android,android-recyclerview,android-elevation,Android,Android Recyclerview,Android Elevation,我有一个recyclerview,我在其中添加了一个立面图。看起来,根据项目在屏幕上的垂直位置,标高会变得更高,阴影会随着项目向下滚动而增大。我尝试添加背景色,但没有效果。我找到了另一篇文章,其中有一段视频显示了这个问题 这是我的相对位置,我在这里添加高程。 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

我有一个
recyclerview
,我在其中添加了一个
立面图。看起来,根据项目在屏幕上的垂直位置,标高会变得更高,阴影会随着项目向下滚动而增大。我尝试添加背景色,但没有效果。我找到了另一篇文章,其中有一段视频显示了这个问题

这是我的相对位置,我在这里添加高程。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#EDEDED"
    android:focusable="?attr/selectableItemBackground"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:id="@+id/newsRowRelativeLayout"
    android:elevation="4dp">

    <ImageView
        android:id="@+id/newsImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:maxHeight="300dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="15dp"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:background="@color/mainTextBGColor" />

    <TextView
        android:id="@+id/newsTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:layout_below="@id/newsType"
        android:textStyle="bold"
        android:textColor="@color/news_title"
        android:paddingLeft="@dimen/activity_margin"
        android:paddingRight="@dimen/activity_margin"
        android:textSize="@dimen/news_title_main" />

    <TextView
        android:id="@+id/newsType"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:layout_below="@id/newsImage"
        android:textStyle="bold"
        android:textAllCaps="true"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="10dp"
        android:textColor="@color/mainBGColor"
        android:paddingLeft="@dimen/activity_margin"
        android:paddingRight="@dimen/activity_margin"
        android:textSize="@dimen/news_tag_detailed" />

    <TextView
        android:id="@+id/newsText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/newsTitle"
        android:background="@android:color/transparent"
        android:ellipsize="end"
        android:maxLines="3"
        android:textColor="@color/news_text"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp"
        android:paddingLeft="@dimen/activity_margin"
        android:paddingRight="@dimen/activity_margin"
        android:textSize="@dimen/news_text_main"/>

</RelativeLayout>

它没有什么问题。高程效果使用所谓的关键光环境光。一个位于屏幕正上方(环境光),另一个(关键光),与屏幕成45°角。这就是为什么在滚动时阴影会移动

tl;dr 阴影不是静态的,而是动态计算的。另请参见下图,解释该概念:


我不是发布最终解决方案,而是发布我正在起草的草案

我理解Android的提升理论,但这对我来说没有任何意义,所以我的想法是听屏幕上内容的滚动,并基于此调整提升

public class VerticalScrollListener extends RecyclerView.OnScrollListener {

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    int[] visible = new int[1];
    mLayoutManager.findFirstVisibleItemPositions(visible);
    int[] lastvisible = new int[1];
    mLayoutManager.findLastVisibleItemPositions(lastvisible); 
    for (int i = visible[0]; i<lastvisible[0]; i++){
      RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i);
     int[] location = new int[2];
     ((MessageViewHolder)viewHolder).contentView.getLocationOnScreen(location);
     float y = location[1];
     float ratio = y/scheight;
     ((MessageViewHolder)viewHolder).contentView.setElevation(16-14*ratio);
    }

}

@Override
public void onScrollStateChanged(final RecyclerView recyclerView, final int newState) {
    super.onScrollStateChanged(recyclerView, newState);
}}
公共类VerticalScrollListener扩展了RecyclerView.OnScrollListener{
@凌驾
已填空的公共空间(RecyclerView RecyclerView、int dx、int dy){
super.onScrolled(recyclerView、dx、dy);
int[]可见=新的int[1];
mLayoutManager.findFirstVisibleItemPositions(可见);
int[]lastvisible=新int[1];
mLayoutManager.findLastVisibleItemPositions(lastvisible);

对于(int i=可见[0];这很有意义。很有趣。所以我没有办法为每个recyclerview项目创建相同的阴影效果?我认为标高的工作方式有点不同,所以我没有考虑这样的事情。谢谢!至少没有使用标高属性。在引入
材质设计
之前,人们对使用带有背景的“假”
cardwiews
图层列表。这自然会生成带有静态阴影的静态背景图像,但我不建议使用这种方法。Dang。谢谢!