【发布时间】: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 的大小