简体   繁体   中英

ScrollView inside ScrollView not work

I tried to search tutorial for ScrollView inside ScrollView, and I found many but no one work for me. This is my layout bellow. The second ScrollView (id = scroll_view_carousel) do not work. Please help me.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context="com.rytmus.app.activities.DetailsActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            android:theme="@style/Toolbar"
            app:collapseIcon="@drawable/ic_arrow_back_white_24dp"
            app:layout_scrollFlags="scroll|enterAlways"
            app:title="Rytmus"
            app:titleTextColor="@color/colorPrimary">

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

    </android.support.design.widget.AppBarLayout>

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="0dp">

            <ImageView
                android:id="@+id/big_logo"
                android:layout_width="match_parent"
                android:layout_height="160dp"
                android:scaleType="centerCrop" />

            <ScrollView
                android:id="@+id/scroll_view_carousel"
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:layout_below="@id/big_logo"
                android:background="@color/colorBlack">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_marginTop="5dp"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_marginRight="5dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/background_rytmus_disco" />

                    <ImageView
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_marginRight="5dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/background_rytmus_disco" />

                    <ImageView
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_marginRight="5dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/ic_arrow_back_white_24dp" />

                    <ImageView
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_marginRight="5dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/background_rytmus_disco" />

                    <ImageView
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_marginRight="5dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/com_facebook_button_like_background" />

                    <ImageView
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_marginRight="5dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/background_rytmus_disco" />

                    <ImageView
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_marginRight="5dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/background_rytmus_disco" />

                    <ImageView
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_marginRight="5dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/background_rytmus_disco" />

                    <ImageView
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_marginRight="5dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/background_rytmus_disco" />

                    <ImageView
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_marginRight="5dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/background_rytmus_disco" />

                </LinearLayout>

            </ScrollView>

            <LinearLayout
                android:id="@+id/four_buttons"
                android:layout_width="match_parent"
                android:layout_height="46dp"
                android:layout_below="@id/scroll_view_carousel"
                android:background="@color/colorAccent"
                android:orientation="horizontal">

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/tabFirstsButtonsLinearLayout"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:orientation="vertical">


                </LinearLayout>

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="100dp"
                    android:layout_height="fill_parent"
                    android:orientation="vertical" />

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/tabSecondsButtonsLinearLayout"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_centerHorizontal="true"
                    android:layout_weight="1"
                    android:orientation="vertical">


                </LinearLayout>

            </LinearLayout>

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

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tabs_disco_details"
                android:layout_width="match_parent"
                android:layout_height="46dp"
                android:layout_below="@id/four_buttons"
                android:background="@color/colorPrimary"
                android:orientation="horizontal">

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/tabDescriptionLinearLayout"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/tabDescription"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_margin="4dp"
                        android:gravity="center"
                        android:paddingLeft="10dp"
                        android:text="Descripción"
                        android:textColor="@color/white"
                        android:textSize="13sp" />
                </LinearLayout>

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="90dp"
                    android:layout_height="fill_parent"
                    android:orientation="vertical" />

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/tabValorationLinearLayout"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_centerHorizontal="true"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/tabValoration"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_margin="4dp"
                        android:gravity="center"
                        android:paddingLeft="10dp"
                        android:text="Valoraciones"
                        android:textColor="@color/white"
                        android:textSize="13sp" />
                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/container_fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/tabs_disco_details"
                android:orientation="vertical" />

            <LinearLayout
                android:layout_width="101dp"
                android:layout_height="101dp"
                android:layout_below="@id/scroll_view_carousel"
                android:layout_centerHorizontal="true"
                android:background="@drawable/border"
                android:translationY="-5dp">

                <ImageView
                    android:id="@+id/imagen_logo"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_centerHorizontal="true"
                    android:src="@drawable/background_rytmus_disco" />

            </LinearLayout>

        </RelativeLayout>

    </ScrollView>
</LinearLayout>

SOLUTION

I change the second ScrollView for one HorizontalScrollView and now work!!!

Very Bad Programming Practice. You can't embed scrollView inside scrollView.

in general ListView inside scrollView also not working because ListView has its own scrolling functionality and we are again embedding it into scrollView class thats why it will create jerk.

same in this how should you integrate scrollView inside scrollView? think logically how it should be possible? it will confuse which scrollView getting triggered?

I think you would want to use nested RecyclerViews. There are plenty of examples if you search around. I made a simple one here https://github.com/ozv101/NestedRecycler

Read about ScrollView.

ScrollView is a special kind of layout, designed to hold view larger than its actual size. When the Views size goes beyond the ScrollView size, it automatically adds scroll bars and can be scrolled vertically. ScrollView can hold only one direct child. This means that, if you have complex layout with more view controls, you must enclose them inside another standard layout like LinearLayout, TableLayout or RelativeLayout. You can specify layout_height and layout_width to adjust height and width of screen.

Scrollview is ideal for screens where scrolling is required, but it is an overhead when scroll view is used to render a larger list of data. Android provides specialized adapter views like ListView, GridView and RecyclerView (introduced in Android Lollipop) for long lists.

You should never use a ScrollView with a ListView or GridView, because they both take care of their own vertical scrolling. ScrollView only supports vertical scrolling. HorizontalScrollView is used for horizontal scrolling.

The android:fillViewport property defines whether the ScrollView should stretch its content to fill the viewport. You can set the same property by calling setFillViewport(boolean) method from java class.

Do not use scrollview inside scrollview...may be above documents helping to you !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM