Search code examples
javaandroidhorizontalscrollview

Android HorizontalScrollView


I'm making a fragment with a horizontal scroll on the previous screen. the HorizontalScrollView is in a fragment it worked fine when it was an activity but when you change it to a fragment the interface gets stuck

I have a HorizontalScrollView inside my ViewPager. but when putting it in a fragment the interface does not work as it should, but at the moment of moving the view to the right or left it remains as the image

Like shown in Image below.

View Example

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/FragmentoLayout_fonemasTar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1Tar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/secondary_text_light"
        android:fadeScrollbars="false"
        android:scrollbarSize="2dp"
        android:scrollbarThumbHorizontal="@drawable/border_scroll_listview">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="150dp"
                    android:layout_height="match_parent"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/lblTarCampo1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/LayoutTar_lblTarCampo1" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="80dp"
                    android:layout_height="match_parent"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/lblTarCampo2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/LayoutTar_lblTarCampo2" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="150dp"
                    android:layout_height="match_parent"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/lblTarCampo3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/LayoutTar_lblTarCampo3" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="30dp"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/lblEspacio1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/LayoutTar_FormatoTar_txtTarSubCampo3" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="150dp"
                    android:layout_height="match_parent"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/lblTarSubCampo3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/LayoutTar_lblTarSubCampo345" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="150dp"
                    android:layout_height="match_parent"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/lblTarCampo4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/LayoutTar_lblTarCampo4" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="30dp"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/lblEspacio2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/LayoutTar_FormatoTar_txtTarSubCampo4" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="150dp"
                    android:layout_height="match_parent"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/lblTarSubCampo4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/LayoutTar_lblTarSubCampo345" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="150dp"
                    android:layout_height="match_parent"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/lblTarCampo5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/LayoutTar_lblTarCampo5" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="30dp"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/lblEspacio3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/LayoutTar_FormatoTar_txtTarSubCampo5" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="150dp"
                    android:layout_height="match_parent"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/lblTarSubCampo5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/LayoutTar_lblTarSubCampo345" />

                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ListView
                    android:id="@+id/lst_PuntoArtFon1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fadeScrollbars="false"
                    android:scrollbarThumbVertical="@drawable/border_scroll_listview"
                    tools:listitem="@layout/formato_tar_layout"></ListView>

            </LinearLayout>

        </LinearLayout>

    </HorizontalScrollView>

</LinearLayout>

Solution

  • This is happening because the new view that you are loading(by swiping right/left) is above the HorizontalScrollView & its background colour is transparent. You must give a white background colour to the parent LinearLayout of all the fragments.

    Also set android:clickable to true so that the touch events don't trigger the previous fragments.