RecyclerView最后一行显示不全

RecyclerView高度使用wrap_content时最后一行显示不全,需在RecyclerView包裹一层布局,并确定外层布局的高度;或者分配剩余空间;如果父布局使用ConstraintLayout,则确定顶部和底部定位,并且高度设为0dp。如果外层嵌套ScrollView,改成NestedScrollView。

 

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".me.activity.MyXuedouActivity">

    <include
        android:id="@+id/xuedou_top"
        layout="@layout/activity_top"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list_mingxi"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/common_10dp"
        android:layout_marginEnd="@dimen/common_10dp"
        android:layout_marginTop="@dimen/common_12dp"
        android:background="@drawable/background_bg_white_course"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/xuedou_top" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在RecyclerView中只显示2.5行的数据,您可以通过以下步骤实现: 1. 计算每个项的高度:首先,您需要计算每个项的高度,以便知道2.5行的高度是多。可以使用以下方法来计算每个项的高度: ```java int itemHeight = recyclerView.getHeight() / 2.5; ``` 这将把RecyclerView的高度除以2.5,以得到每个项的高度。 2. 创建自定义LayoutManager:接下来,您可以创建一个自定义的LayoutManager,该LayoutManager将在布局过程中限制项的数量,以使RecyclerView显示2.5行的数据。 ```java public class CustomLayoutManager extends LinearLayoutManager { public CustomLayoutManager(Context context) { super(context); } @Override public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { int itemCount = state.getItemCount(); int height = getHeight(); // RecyclerView的高度 if (itemCount > 0 && height > 0) { int itemHeight = height / 2.5; // 每个项的高度 int visibleItemCount = height / itemHeight; // 可见的项数量 if (visibleItemCount > itemCount) { visibleItemCount = itemCount; } setMeasuredDimension(getWidth(), itemHeight * visibleItemCount); // 设置RecyclerView的高度 super.onLayoutChildren(recycler, state); } } } ``` 在上面的代码中,我们重写了`onLayoutChildren()`方法,并在布局过程中根据2.5行的高度限制了可见项的数量。 3. 使用自定义LayoutManager:最后,将自定义LayoutManager应用于RecyclerView。 ```java CustomLayoutManager layoutManager = new CustomLayoutManager(context); recyclerView.setLayoutManager(layoutManager); ``` 现在,RecyclerView将只显示2.5行的数据,并且可以滚动浏览。 请注意,这种方法仅适用于固定项高度的情况。如果您的项高度不同,您可能需要通过其他方式来计算和调整布局,以确保只显示2.5行的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值