1

I'm strugglingwith ScrollView, and don't really see what's wrong with it.

-My objective is to make a scrollable fragment with images and text(under each image). I've already added some simple images, and was trying to add a dedicated ScrollView for text under those images.

Problem explanation starts here: When the fragment has only 1 image and 1 ScrollView the ScrollView work fine, but when I add one more image below, ScrollView just freezes and doesn't scroll anymore. Where is my mistake?

Here is the code:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:fillViewport="true">
<LinearLayout
    android:id="@+id/LinearLayout_Parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/top_image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image1"
        android:adjustViewBounds="true"/>
        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="100dp">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/rly_large"/>
        </ScrollView>
    //when ImageView is added ScrollView stops wotking
    **<ImageView
        android:id="@+id/top_image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image2"/>**
</LinearLayout>

Whitout ImageView inside ** everything works fine but when that ImageView is added the ScrollView stops scrolling.

I hope you could point for the mistake, and hope didn't made a stupid question.

Thank you very much.

Android Stuido: 2.1.2 JRE 1.8.0-b15

2 Answers2

0

Having a ScrollView inside another ScrollView is not a good practice. However the code you have used could work in some cases. But it might fail when you are having multiple elements. In that case, it might not be able to identify the gesture.

However, you can try this answer, if it helps ScrollView Inside ScrollView. It disables the parent ScrollView's touch, when a touch for the child is identified. Also have a look at this post if it helps.

Community
  • 1
  • 1
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<ScrollView 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:fillViewport="true">
<LinearLayout
    android:id="@+id/LinearLayout_Parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/top_image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image1"
        android:adjustViewBounds="true"/>
        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="100dp">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/rly_large"/>
        </ScrollView>

    <ImageView
        android:id="@+id/top_image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image2"/>
</LinearLayout>

You are using ScrollView as root element but never add the close tag. (that might be a copy-paste problem)

Additionally, using ScrollView as root element causes problems in my experience, so if you push the ScrollView that currently is a root element one layer in and set LinearLayout as root element, that may solve your problems.

Additionally:

Whitout ImageView inside ** everything works fine but when that ImageView is added the ScrollView stops scrolling.

is a little hard to understand. Which of the two stops scrolling?

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • My bad, the one that stops scrolling is the one that is bellow ImageView. – Pavlo Zakharuk Jul 07 '16 at 08:19
  • Is there need to scroll? Is the text short enough for it to fit right under the image or is it so long scrolling is needed? – Zoe Jul 07 '16 at 08:20
  • Text will be long, so i thoung that it would be better to have a small scrolling box than a big text box. – Pavlo Zakharuk Jul 07 '16 at 08:24
  • You could try to add another scrollview under the second image to get some more space and test the scrollview again. Or just say android:layout_marginBottom="100dp" – Zoe Jul 07 '16 at 08:59