-1

I am working with fragments I have a requirement to use horizontal scroll view inside a fragment. I know how to add it in activity but I have no idea how to add horizontal scroll view in a fragment?

INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63
Anwesh
  • 11
  • 2
  • 3
    Why would you think it's any different in a `Fragment`? That is, what specific problems are you having in using one in a `Fragment`'s layout? – Mike M. Apr 25 '19 at 04:59
  • See link: https://stackoverflow.com/questions/18656949/how-to-implement-horizontalscrollview-like-gallery – Shahid Ahmad Apr 25 '19 at 05:06
  • 1
    Possible duplicate of [Horizontal ScrollView in fragment Android](https://stackoverflow.com/questions/30476690/horizontal-scrollview-in-fragment-android) – Lasith Jayalath Apr 25 '19 at 05:11

1 Answers1

1

I don't think there's a difference between create a horizontal scroll view on activity or fragment. Just remember that you should add a layout inside it. Some rare cases I got bug's using scrollview on activity and can't move the fragment scrollview, so, I've the practice to use scrollview only on fragment.

An exemple:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    android:background="#f1f1f1"
    tools:context=".Fragments.Example">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.v7.widget.CardView
                    android:layout_width="900dp"
                    android:layout_height="300dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <android.support.constraint.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Example"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />

                    </android.support.constraint.ConstraintLayout>

                </android.support.v7.widget.CardView>

            </android.support.constraint.ConstraintLayout>

        </HorizontalScrollView>

    </android.support.constraint.ConstraintLayout>

</ScrollView>

It worked successfully. (Tested)

Marcello Câmara
  • 187
  • 1
  • 13