【问题标题】:How to adjust recyclerview height?如何调整recyclerview的高度?
【发布时间】:2018-02-26 15:04:02
【问题描述】:

我希望 recyclerview 调整它的高度,这样商品就不会隐藏在价格栏后面(见图)

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutMainContainer"
    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.mridulahuja.groceryapp.activities.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@null" />

    <RelativeLayout
        android:id="@+id/layoutPricebar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#454560"
        android:layout_alignParentBottom="true"
        android:visibility="gone">

        ...
        ...

    </RelativeLayout>

</RelativeLayout>

我用来调整 recyclerview 大小的代码:

ViewTreeObserver pricebarvto = layoutPricebar.getViewTreeObserver();
pricebarvto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            layoutPricebar.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            layoutPricebar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        pricebarHeight = layoutPricebar.getMeasuredHeight();
    }
});


RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) recyclerView.getLayoutParams();
params.height = relativeLayoutContainerHeight - pricebarHeight;
recyclerView.setLayoutParams(params);

其中relativeLayoutContainerHeight 是根RelativeLayout 的高度

但高度没有变化。当我硬编码一个大值时它正在改变 到pricebarHeight 例如160。我给pricebar 的高度只有50dp。

我也在使用这个动画来显示价格栏:

<translate
xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fromYDelta="180%"
    android:toYDelta="0%">
</translate>

【问题讨论】:

  • 使用线性布局作为带有recyclerview和价格栏的垂直方向。
  • 将主 RelativeLayout 转换为 LinearLayout 并具有垂直方向。你不需要设置RecyclerView的高度
  • 是的,你明白我的意思。
  • 谢谢,我会尝试并告诉你
  • 如果您的价格栏高度为 50dp,则使 RecyclerView android:layout_marginBottom="50dp" 这将解决您的问题,无需使用 Java 代码来调整 Recyclerview 的大小

标签: android android-layout


【解决方案1】:

如果你使用RelativeLayout,那么你应该使用android:layout_marginBottom

指定此视图底部的额外空间。这个空间是 在这个视图的范围之外。边距值应为正数。

XML

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        ................
        android:layout_marginBottom="50dp" />

如果你想要LinearLayout 那就关注android:weightSum 逻辑吧。

定义最大重量总和。如果未指定,则总和由下式计算 添加所有孩子的 layout_weight。这可以用于 实例给一个孩子总可用空间的 50% 给它一个 0.5 的 layout_weight 并将 weightSum 设置为 1.0。

【讨论】:

  • 谢谢,一切正常。我已经删除了在 java 中调整 recyclerview 大小的代码。但是现在,由于底部的价格栏是动画的,并且需要几毫秒才能出现,它在价格栏和回收站视图的新高度之间留下了差距。有没有办法为 recyclerview 的高度变化设置动画?
【解决方案2】:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutMainContainer"
    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.mridulahuja.groceryapp.activities.MainActivity">

    <RelativeLayout
        android:id="@+id/layoutPricebar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#454560"
        android:layout_alignParentBottom="true"
        android:visibility="gone">

        ...
        ...

    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_above = "@id/layoutPricebar"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@null" />



</RelativeLayout>

【讨论】:

    【解决方案3】:

    尝试使用 android:layout_below: "@+id/recycler_view" 将布局价格栏 id : layoutPricebar 放在 recycler_view 下面

     <RelativeLayout
        android:id="@+id/layoutPricebar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#454560"
        android:layout_below="@+id/recycler_view"
        android:layout_alignParentBottom="true"
        android:visibility="gone">
    

    【讨论】:

      【解决方案4】:

      使用垂直方向的线性布局而不是相对布局并提供回收器视图

      android:layout_weight="1"
      

      【讨论】:

        【解决方案5】:

        如果您的价格栏高度为 50dp,则将 RecyclerView 设为 android:layout_marginBottom="50dp" 这将解决您的问题,无需使用 Java 代码来调整 Recyclerview 的大小

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-07
          • 1970-01-01
          • 1970-01-01
          • 2016-02-18
          • 2020-06-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多