Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android scrollview inside another scrollview doesn't scroll

I'm trying to put a ScrollView inside another ScrollView and I tried the answers found on this site but it still doesn't seems to work completely. Here is the XML:

 <ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="false" >

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

        <TextView
            android:id="@+id/textViewDirectoryDetailName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Name"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/red" />

        <LinearLayout
            android:id="@+id/linearLayoutDirectoryDetailContainImage"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:layout_gravity="center_vertical|center_horizontal"
            android:background="@drawable/general_image"
            android:gravity="center"
            android:orientation="vertical" >
        </LinearLayout>

        <ScrollView
            android:id="@+id/scrollView2"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:focusableInTouchMode="false" >

            <TextView
                android:id="@+id/textViewDirectoryDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:autoLink="phone"
                android:text="098 123 45 678"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </ScrollView>

And here is the Java code:

parentScrollView = (ScrollView) findViewById(R.id.scrollView1);
    childScrollView = (ScrollView) findViewById(R.id.scrollView2);

    parentScrollView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View p_v, MotionEvent p_event) {
            childScrollView.getParent().requestDisallowInterceptTouchEvent(
                    false);
            // We will have to follow above for all scrollable contents
            return false;
        }
    });
    childScrollView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View p_v, MotionEvent p_event) {
            // this will disallow the touch request for parent scroll on
            // touch of child view
            p_v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });
    // We will have to follow above for all child scrollable contents

What seems to happen is this: when I have text in the TextView it doesn't seem to let me scroll it at all. It just scrolls the parent. But when I have no text and I touch the child ScrollView it doesn't scroll the parent so I guess it's working then. But do you have any ideas why it doesn't work when I have text?

like image 907
Mihai Boisteanu Avatar asked Feb 17 '14 08:02

Mihai Boisteanu


People also ask

Why is ScrollView not scrolling?

To fix ScrollView Not scrolling with React Native, we wrap the content of the ScrollView with the ScrollView. to wrap ScrollView around the Text components that we render inside. Anything inside the ScrollView will scroll. As a result, we should see text inside and we can scroll up and down.

What is nested scroll view?

NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

How to add Scroll View in Android studio?

To add multiple views within the scroll view, make the direct child you add a view group, for example LinearLayout , and place additional views within that LinearLayout. Scroll view supports vertical scrolling only. For horizontal scrolling, use HorizontalScrollView instead.

How to set ScrollView in Android?

In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it. A ScrollView supports Vertical scrolling only, so in order to create a horizontally scrollable view, HorizontalScrollView is used.


1 Answers

You should use NestedScrollView.

<android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <!-- -->
</android.support.v4.widget.NestedScrollView>
like image 77
Volodymyr Kulyk Avatar answered Oct 16 '22 21:10

Volodymyr Kulyk