我需要确保水平 recyclerView 高度与最大项目的高度相同。
项目可以有不同的高度(项目 = 总是相同的图像 + 标题 + 副标题,标题和副标题可以有无限长度)。当我为我的 recyclerView 设置 wrap_content 时,它会根据可见项目的高度调整大小,这使得 recyclerView 下方的内容跳跃,这是我想要避免的。
我想要达到的目标:
灰色区域是可见视口。
所以基本上我想以某种方式获得最大项目的高度,然后将 recyclerView 高度设置为该数字。
我已经尝试过的是基于标题 + 副标题长度的近似值,但它非常不准确,因为例如,即使两个标题具有相同的文本长度,它们也可能具有不同的宽度,因为我使用的字体不是等宽字体。
我打算实现的目标
项目视图应占据项目的整个高度
可能是项目高度小于recyclerview中最高项目的高度,在这种情况下,它应该像上面的屏幕截图中那样粘在顶部.
我遇到的错误
如上面的屏幕截图所示,视图被截断.
到目前为止我尝试过的
最初我在recyclerview上使用wrap_content,现在它已得到支持.当当时在屏幕上看不到的任何视图都是最高的时候,它不起作用.这对视图层次结构的布局有意义.如果高度取决于该数据,如何计算甚至没有绑定到任何数据的东西的高度?
解决时间:S
我没有尝试自定义布局管理器,而是首先选择了我认为需要完成的工作 - 在开始时列出所有项目视图以确定它们的高度.
在屏幕的上半部分有一个进度条和一个动画,以吸引用户的注意力,而这一切都发生在recyclelerview可见性被设置为不可见的情况下.我使用了两件事,一件是不够的 - 我在适配器的onViewAttached()调用中附加了一个观察者,我也使用了滚动更改监听器.有一个LinearSnapHelper附加到回收器视图,以捕捉到滚动的相邻(下一个或上一个,取决于滚动方向)位置.
在此设置中,
layoutManager.smoothScrollToPosition()使用获取子视图高度
View currentChildView = binding.nextRv.getChildAt(layoutManager.findFirstCompletelyVisibleItemPosition());
if (currentChildView != null) {
currentChildHeight = currentChildView.getHeight();
}
Run Code Online (Sandbox Code Playgroud)在滚动更改侦听器中RecyclerView.SCROLL_STATE_IDLE或通过将高度传递给适配器的onViewAttachedToWindow()中上面提到的视图附加观察器
@Override
public void onViewAttachedToWindow(BindingViewHolder holder) {
if (mObserver != null) {
mObserver.onViewAttached(holder.binding.getRoot().getHeight());
}
}
Run Code Online (Sandbox Code Playgroud)
maxHeight更改为maxHeight和新子高度的最大值.很明显,这很难看.另外它并没有给我当前视图的高度 - onAttached意味着它只是附加,而不是测量和布局.它是循环视图,而不是绑定到当前数据项的视图.这会出现如上所示的截断视图等问题.
我还在回收器视图上尝试了wrap_content高度,并从回收器的父进程中失效,直到回收器和滚动子进入SCROLL_STATE_IDLE.不行.
我不确定自定义布局管理器如何在这里提供帮助.
有人能引导我朝正确的方向发展吗?
我正在尝试实现RecyclerView水平滚动的 a,所以我使用的是LinearLayoutManager水平方向的 a。问题是我正在使用 2 种不同类型的项目填充 RecyclerView,具有不同的高度。这是我用于该项目的布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp">
<LinearLayout
android:id="@+id/document_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/ic_rounded"
android:backgroundTint="@color/ms_black_ms_gray"
android:gravity="center"
android:layout_gravity="bottom"
android:padding="5dp"
android:paddingStart="15dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="13sp"
android:singleLine="true"
android:maxWidth="80dp"
tools:text="example_form"/>
<TextView
android:id="@+id/format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="13sp" />
…
</LinearLayout>
<android.support.v7.widget.CardView
android:id="@+id/image_view"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_gravity="bottom"
app:cardCornerRadius="25dp"
app:cardElevation="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/preview_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
…
</RelativeLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
这是包含 的布局,RecyclerView基本上是这样的:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingStart="14dp"
android:paddingEnd="14dp">
<ImageView
android:id="@+id/attach"
android:layout_width="wrap_content" …Run Code Online (Sandbox Code Playgroud) android horizontal-scrolling android-recyclerview android-flexboxlayout
android ×3