0

in my layout i have recyclerview of product listing..below that im displaying total price in linearlayout ...but on scroll linearlayout is not displaying

sceneorio of my code:

1--> when i have one item on recyclerview -->linearlayout displays fine

2--> when i have more items on recyclerview -->on scrolling down linearlayout cant be seen(linear layout is not displaying)

need help in second sceneorio...thanks in advance

Following here is code xml:--

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relative">
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:collapseIcon="@drawable/ic_arrow_back_black_24dp"

        android:id="@+id/toolbartable"
        android:background="@color/colorPrimaryDark">

    </androidx.appcompat.widget.Toolbar>

</RelativeLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/relative"
    android:id="@+id/nest"
    android:scrollbars="vertical">
  <androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_below="@id/relative"
    android:layout_height="wrap_content"
    android:id="@+id/recyleview"/>
</ScrollView>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/linearlayoutorder"
    android:layout_below="@id/nest"
    android:orientation="horizontal"
    android:weightSum="2"
    android:background="@drawable/border"
    android:backgroundTintMode="@color/colorPrimary">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TOTAL"
        android:layout_gravity="center"
        android:layout_weight="1"></TextView>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/totalidcost"
        android:layout_gravity="right|center"
        android:textAlignment="textEnd"
        tools:ignore="RtlCompat"></TextView>
</LinearLayout>
</RelativeLayout>

Updated code:--

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relative">
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:collapseIcon="@drawable/ic_arrow_back_black_24dp"

        android:id="@+id/toolbartable"
        android:background="@color/colorPrimaryDark">

    </androidx.appcompat.widget.Toolbar>

</RelativeLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/nest"
android:scrollbars="vertical"
android:layout_below="@id/relative">
  <androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
      android:nestedScrollingEnabled="false"
      android:id="@+id/recyleview"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/linearlayoutorder"

    android:orientation="horizontal"
    android:weightSum="2"
    android:background="@drawable/border"
    android:backgroundTintMode="@color/colorPrimary">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TOTAL"
        android:layout_gravity="center"
        android:layout_weight="1"></TextView>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/totalidcost"
        android:layout_gravity="right|center"
        android:textAlignment="textEnd"
        tools:ignore="RtlCompat"></TextView>
</LinearLayout>
 </androidx.core.widget.NestedScrollView>
 </RelativeLayout>

error getting--:Caused by: android.view.InflateException: Binary XML file line #34: ScrollView can host only one direct child

 `Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child`

need help

IRON MAN
  • 191
  • 3
  • 18

4 Answers4

1

following solution worked for me:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relative">
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:collapseIcon="@drawable/ic_arrow_back_black_24dp"

        android:id="@+id/toolbartable"
        android:background="@color/colorPrimaryDark">

    </androidx.appcompat.widget.Toolbar>

</RelativeLayout>

 <androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/nest"
android:scrollbars="vertical"
android:layout_below="@id/relative">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
  <androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
      android:nestedScrollingEnabled="false"
      android:id="@+id/recyleview"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/linearlayoutorder"
     android:layout_below="@id/recyleview"
    android:orientation="horizontal"
    android:weightSum="2"
    android:background="@drawable/border"
    android:backgroundTintMode="@color/colorPrimary">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TOTAL"
        android:layout_gravity="center"
        android:layout_weight="1"></TextView>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/totalidcost"
        android:layout_gravity="right|center"
        android:textAlignment="textEnd"
        tools:ignore="RtlCompat"></TextView>
</LinearLayout>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
 </RelativeLayout>
IRON MAN
  • 191
  • 3
  • 18
0

You haven't added weightSum and orientation values ​​for LinearLayout.

Source

Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50
0

You can use a NestedScrollView instead of the ScrollView. And also put your RecyclerView as well as the LinearLayout inside the NestedScrollView. And enable android:nestedScrollingEnabled=true. Should work for you.

<NestedScrollView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:nestedScrollingEnabled=true>
      <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyleview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/linearlayoutorder"
        app:layout_constraintTop_toBottomOf="@id/relative" />

    <LinearLayout
        android:id="@+id/linearlayoutorder"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#000000"
        android:backgroundTintMode="@color/colorPrimary"
        android:orientation="horizontal"
        android:weightSum="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="TOTAL" />

        <TextView
            android:id="@+id/totalidcost"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center"
            android:layout_weight="1"
            android:textAlignment="textEnd"
            tools:ignore="RtlCompat" />
    </LinearLayout>
</LinearLayout>

And in your java class where you have your recyclerview set recyclerView.setNestedScrollingEnabled(true or false);

KalanaChinthaka
  • 323
  • 4
  • 13
  • i did nestedscrolling false inside recyclerview i got error ==> Caused by: android.view.InflateException: Binary XML file line #34: ScrollView can host only one direct child Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child – IRON MAN Sep 03 '20 at 05:03
  • Just create a linearLayout or a RelativeLayout as a parent of your view inside the nestedScrollView. Then you should be fine. I will update my answer with the hierarchy. – KalanaChinthaka Sep 03 '20 at 07:04
0

Use ConstraintLayout like this way, Here is full xml code what you need

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

    <RelativeLayout
        android:id="@+id/relative"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbartable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryDark"
            app:collapseIcon="@drawable/back" />

    </RelativeLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyleview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/linearlayoutorder"
        app:layout_constraintTop_toBottomOf="@id/relative" />

    <LinearLayout
        android:id="@+id/linearlayoutorder"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#000000"
        android:backgroundTintMode="@color/colorPrimary"
        android:orientation="horizontal"
        android:weightSum="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="TOTAL" />

        <TextView
            android:id="@+id/totalidcost"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center"
            android:layout_weight="1"
            android:textAlignment="textEnd"
            tools:ignore="RtlCompat" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Pratik Fagadiya
  • 1,195
  • 7
  • 21