Android: 为什么在NestedScrollView 里面的RecyclerView 底部有一个空白space?

Android: Why there are a blank space at the bottom of RecyclerView which is inside of NestedScrollView?

NestedScrollView 中使用 RecyclerView 会在 activity 中创建一个奇怪的空白 space(如底部填充),导致不必要的滚动,因为没有更多要显示的元素。

这个填充随 RecyclerView 增长,我的意思是:

如果在RecyclerView中有0到2个元素,这在屏幕上是完美的,并且没有滚动。这里效果很好。

如果 RecyclerView 中有 3 到 5 个元素,这些元素继续适合屏幕,但在这种情况下,有更多空白 space 导致不必要的滚动。

随着元素的增加,空白 space 继续增长并创建越来越多的滚动条。

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ibosca.thub.ChannelProfileActivity">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/channelPicture"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:civ_border_color="#FF000000"
            app:civ_border_width="2dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <TextView
            android:id="@+id/channelName"
            android:layout_width="256dp"
            android:layout_height="90dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:textAlignment="center"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/channelPicture"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <TextView
            android:id="@+id/description"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:textAlignment="center"
            android:textSize="15sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/channelPicture"
            app:layout_constraintVertical_bias="0.0" />

        <View
            android:id="@+id/line1"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/black"
            app:layout_constraintTop_toBottomOf="@+id/description" />

        <TextView
            android:id="@+id/contentsText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:text="@string/contents"
            android:textColor="@android:color/black"
            android:textAlignment="center"
            android:textSize="17sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/line1"
            app:layout_constraintVertical_bias="0.0" />

        <View
            android:id="@+id/line2"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/black"
            app:layout_constraintTop_toBottomOf="@+id/contentsText" />


        <android.support.v7.widget.RecyclerView
            android:id="@+id/contentList"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintTop_toBottomOf="@+id/line2"/>

    </android.support.constraint.ConstraintLayout>

</android.support.v4.widget.NestedScrollView>

</android.support.constraint.ConstraintLayout>

这是什么原因造成的 space? 我怎样才能删除它?

编辑:问题

这里可以看到错误:https://www.youtube.com/watch?v=6nSc4l2c2jg

编辑 2:回收器

适配器:

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.ibosca.thub.R;
import com.ibosca.thub.models.Channel;
import com.ibosca.thub.models.Content;
import com.ibosca.thub.models.ContentList;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;

import de.hdodenhof.circleimageview.CircleImageView;

public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.MyViewHolder> {

private ContentList contentList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title;
    public CircleImageView picture;
    public TextView channel;
    public TextView lastEdit;
    public ImageView badge;

    public MyViewHolder(View view) {
        super(view);
        title    = (TextView) view.findViewById(R.id.title);
        picture  = (CircleImageView) view.findViewById(R.id.picture);
        channel  = (TextView) view.findViewById(R.id.channel);
        lastEdit = (TextView) view.findViewById(R.id.lastEdit);
        badge    = (ImageView) view.findViewById(R.id.badge);
    }
}


public ContentAdapter(ContentList contentList) {
    this.contentList = contentList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.content_row, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    //Get Content
    Content content = contentList.getContents().get(position);

    //Set title
    holder.title.setText(content.getTitle());

    //Set last edit, channel and picture
    holder.lastEdit.setText(new SimpleDateFormat("dd/MM/yy").format(content.getLastEdit()));
    Channel channel = contentList.findChannelById(content.getChannelId());
    String channelPicture = channel.getPicture();

    holder.channel.setText(channel.getName());
    Picasso.with(holder.picture.getContext()).load(channelPicture).into(holder.picture);

    holder.badge.setVisibility(View.VISIBLE);
    if(content.isRead()){
        holder.badge.setVisibility(View.GONE);
    }

}

@Override
public int getItemCount() {
    return contentList.getContents().size();
}


}

回收站视图(每行):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">

<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/picture"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:src="@drawable/ic_launcher_background"
    app:civ_border_color="#FF000000"
    app:civ_border_width="2dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

<TextView
    android:id="@+id/title"
    android:layout_height="27dp"
    android:layout_alignParentTop="true"
    android:layout_marginStart="8dp"
    android:layout_weight=".90"
    android:layout_width="0dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:singleLine="true"
    android:textColor="@android:color/black"
    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="@+id/picture"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/picture"
    app:layout_constraintTop_toTopOf="@+id/picture"
    app:layout_constraintVertical_bias="0.0" />

<TextView
    android:id="@+id/channel"
    android:layout_width="0dp"
    android:layout_height="23dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_weight=".90"
    android:ellipsize="end"
    android:maxLines="1"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:singleLine="true"
    android:textSize="15sp"
    app:layout_constraintBottom_toBottomOf="@+id/picture"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/picture"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.51" />

<TextView
    android:id="@+id/lastEdit"
    android:layout_width="78dp"
    android:layout_height="23dp"
    android:gravity="bottom|end"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

<ImageView
    android:id="@+id/badge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:contentDescription="notificationBadge"
    android:src="@drawable/orange_badge"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.09"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

</android.support.constraint.ConstraintLayout>

在您的 xml 文件中,更改:

 <android.support.v4.widget.NestedScrollView
     android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="false">

或者

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

我通过在 NestedScrollView 中添加线性布局解决了这个问题。

<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bck_primary_gradient"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center"
    android:orientation="vertical">
  

      <androidx.core.widget.NestedScrollView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:fillViewport="true"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/guideline_15">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <androidx.recyclerview.widget.RecyclerView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginStart="@dimen/dimen_10dp"
                    android:layout_marginEnd="@dimen/dimen_10dp"
                    android:clipToPadding="false"
                    android:fadeScrollbars="false"
                    android:scrollbars="none"
                      />
            </LinearLayout>
    
    
        </androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

我通过在 recyclerview

上调用 setHasFixedSize(false) 解决了这个问题