0

I just started app development trying to make an e-commerce app since using fragments is a better way rather than activities I made a home page inside which I want to show first the action bar then top-selling (top-4 selling products) and after that all the products.

this is some i want to achieve image

I am using guideline to constraint view pager to limited section but since its a fragment its not scrollable so i have to set it to scrollable but the second I set it to scroll view the size of screen increase and the view pager gets distorted.

code of my home fragment without scroll view


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".home.HomeFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include
            android:id="@+id/home_actionbar"
            layout="@layout/actionbar"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/top_selling_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="15sp"
            android:layout_marginTop="15sp"
            android:text="@string/top_selling_heading"
            android:textSize="14sp"
            app:fontFamily="@font/roboto_bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/home_actionbar" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.45"
            app:layout_constraintTop_toBottomOf="@id/top_selling_heading" />


        <androidx.viewpager.widget.ViewPager
            android:id="@+id/top_seller_vp"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:padding="5sp"
            android:layout_margin="15sp"
            android:clipToPadding="false"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/top_selling_heading"
            app:layout_constraintBottom_toTopOf="@id/guideline2"
            app:layout_constrainedHeight="true" />

        <TextView
            android:id="@+id/product_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/guideline2"
            app:layout_constraintStart_toStartOf="@id/top_selling_heading"
            app:layout_constraintEnd_toEndOf="@id/top_selling_heading"
            android:text="@string/all_products_heading"
            android:textSize="20sp"
            android:fontFamily="@font/roboto_bold"/>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/productRV"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:scrollbars="vertical"
                app:layout_constraintTop_toBottomOf="@id/product_heading"
                android:layout_margin="10sp"
                tools:listitem="@layout/product_item"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

is there any other way to achieve this. please help fast..

1 Answers1

-1

try using LinearLayout inside scrollView(Copy and paste)

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <include
                    android:id="@+id/home_actionbar"
                    layout="@layout/actionbar"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/top_selling_heading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="15sp"
                    android:layout_marginTop="15sp"
                    android:text="@string/top_selling_heading"
                    android:textSize="14sp"
                    app:fontFamily="@font/roboto_bold"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/home_actionbar" />

                <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guideline2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layout_constraintGuide_percent="0.45"
                    app:layout_constraintTop_toBottomOf="@id/top_selling_heading" />

                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/top_seller_vp"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="15sp"
                    android:clipToPadding="false"
                    android:padding="5sp"
                    app:layout_constrainedHeight="true"
                    app:layout_constraintBottom_toTopOf="@id/guideline2"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/top_selling_heading" />

                <TextView
                    android:id="@+id/product_heading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/roboto_bold"
                    android:text="@string/all_products_heading"
                    android:textSize="20sp"
                    app:layout_constraintEnd_toEndOf="@id/top_selling_heading"
                    app:layout_constraintStart_toStartOf="@id/top_selling_heading"
                    app:layout_constraintTop_toBottomOf="@id/guideline2" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/productRV"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="10sp"
                    android:scrollbars="vertical"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/product_heading"
                    tools:listitem="@layout/product_item" />

            </LinearLayout>


        </ScrollView>


    </LinearLayout>
</FrameLayout>
Dhruv Sakariya
  • 792
  • 4
  • 10
  • as you can see my view pager is attached to the guideline and since it's a linear layout guideline doesn't work – Anmol Sethi Dec 12 '22 at 07:36