scrollView Inside Horizontal Scrollview not working properly

Hikmat Khan picture Hikmat Khan · Oct 19, 2011 · Viewed 9.4k times · Source

Hope we have good time.dude i m stuck in a little problem .when i place vertical scroll view inside horizontal scroll view then vertical scrollview not works properly.(i tried vice verse too by placing horizontal scroll view inside vertical.)

when i try to scroll the layout just allow me to scroll in one direction not in both simultaneously. i think this is android genuine problem.

kindly provide any solution . Regards

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

    <LinearLayout
        android:id="@+id/linearLayout21"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:background="@drawable/top_bar_bg" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="1dip"
            android:layout_marginTop="1dip"
            android:text=" Image card"
            android:textAppearance="?android:attr/textAppearanceLarge" >
        </TextView>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" Description"
            android:textSize="20sp" >
        </TextView>

        <EditText
            android:id="@+id/IMAGEVIEW_TEXTVEIW"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_below="@+id/textView1"
            android:clickable="false"
            android:cursorVisible="false"
            android:focusable="false"
            android:gravity="top" >
        </EditText>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" Image"
            android:textSize="20sp" >
        </TextView>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dip"
        android:layout_marginTop="5dip"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <HorizontalScrollView
                android:id="@+id/horizontalScrollView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="fill_vertical|fill_horizontal" >

                <RelativeLayout
                    android:id="@+id/RelativeLayout01"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center"
                    android:layout_margin="5dip" >

                    <ScrollView
                        android:id="@+id/scrollView1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" >

                        <RelativeLayout
                            android:id="@+id/RelativeLayout01"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center" >

                            <RelativeLayout
                                android:id="@+id/relativeLayout2"
                                android:layout_width="fill_parent"
                                android:layout_height="fill_parent"
                                android:layout_centerInParent="true" >

                                <ImageView
                                    android:id="@+id/IMAGE_VIEW"
                                    android:layout_width="fill_parent"
                                    android:layout_height="fill_parent"
                                    android:layout_alignParentLeft="true"
                                    android:layout_alignParentTop="true"
                                    android:src="@drawable/diamond" >
                                </ImageView>
                            </RelativeLayout>
                        </RelativeLayout>
                    </ScrollView>
                </RelativeLayout>
            </HorizontalScrollView>
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

Answer

Yan.Yurkin picture Yan.Yurkin · Jan 10, 2013

There is much more simpler solution than creating a custom view:

Layout:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

        <WateverViewYouWant/>

    </ScrollView>
</HorizontalScrollView>

Code (onCreate/onCreateView):

    final HorizontalScrollView hScroll = (HorizontalScrollView) value.findViewById(R.id.scrollHorizontal);
    final ScrollView vScroll = (ScrollView) value.findViewById(R.id.scrollVertical);
    vScroll.setOnTouchListener(new View.OnTouchListener() { //inner scroll listener         
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return false;
        }
    });
    hScroll.setOnTouchListener(new View.OnTouchListener() { //outer scroll listener         
        private float mx, my, curX, curY;
        private boolean started = false;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            curX = event.getX();
            curY = event.getY();
            int dx = (int) (mx - curX);
            int dy = (int) (my - curY);
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    if (started) {
                        vScroll.scrollBy(0, dy);
                        hScroll.scrollBy(dx, 0);
                    } else {
                        started = true;
                    }
                    mx = curX;
                    my = curY;
                    break;
                case MotionEvent.ACTION_UP: 
                    vScroll.scrollBy(0, dy);
                    hScroll.scrollBy(dx, 0);
                    started = false;
                    break;
            }
            return true;
        }
    });

You can change the order of the scrollviews. Just change their order in layout and in the code. And obviously instead of WateverViewYouWant you put the layout/views you want to scroll both directions.