0

I want the recyclerview to adjust it's height so that the items don't get hidden behind the pricebar ( see image )

This is my 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>

Code I'm using to resize 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);

where relativeLayoutContainerHeight is the height of the root RelativeLayout

But the height isn't changing. It's changing when I hard-code a big value to pricebarHeight such as 160. And I've given pricebar the height of just 50dp.

Also I'm using this animation to display the pricebar:

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

enter image description here

mrid
  • 5,782
  • 5
  • 28
  • 71

5 Answers5

2

Use linear layout with vertical orientation instead of relative layout and give recycler view

android:layout_weight="1"
Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
Jyoti Sharma
  • 233
  • 1
  • 9
1

If you use RelativeLayout then you should use android:layout_marginBottom

Specifies extra space on the bottom side of this view. This space is outside this view's bounds. Margin values should be positive.

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

If you want LinearLayout Then follow android:weightSum Logic .

Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    thanks, it's working fine. I've removed the code to resize recyclerview in java. But now, since the bottom pricebar is animated and takes a couple of millisecs to come up, it leaves a gap between the pricebar and the new height of the recyclerview. is there a way to animate the recyclerview height change ? – mrid Sep 18 '17 at 08:00
  • 1
    @mrid Ji, Kindly check https://stackoverflow.com/questions/22454839/android-adding-simple-animations-while-setvisibilityview-gone – IntelliJ Amiya Sep 18 '17 at 08:14
1
<?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>
Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89
0

Try to put the layout pricebar with id : layoutPricebar below the recycler_view by using android:layout_below: "@+id/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">
Jason
  • 122
  • 2
  • 11
0

if your pricebar height is 50dp make RecyclerView android:layout_marginBottom="50dp" this will solve your issue no need of java code for resizing the Recyclerview

Pranav Ashok
  • 581
  • 1
  • 6
  • 18