gpt4 book ai didi

Android:为什么在NestedScrollView 内部的RecyclerView 底部有一个空白区域?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:31 24 4
gpt4 key购买 nike

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

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

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

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

随着元素的增多,空白区域继续增长并产生越来越多的滚动。

这是我的 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>

是什么导致了这个空间?我怎样才能删除它?

编辑:问题

在这里你可以看到错误: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();
}


}

回收者 View (每行):

<?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">

关于Android:为什么在NestedScrollView 内部的RecyclerView 底部有一个空白区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47751802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com