Nilanchala
Nilanchala

Reputation: 5941

How to delegate touch events for ScrollView in background?

I have a specific requirement to add two ScrollViews to an FrameLayout. The ScrollView in the background should display list of images and scroll along with the user touch feedback.

The ScrollView on the top should be scrolled programmatically using smoothScrollTo(x, y) method based on the background scroll view position.

For this, I must disable the scroll event for the ScrollView on the top and allow background ScrollView to scroll along with user touch event.

I could disable the scroll event for the scrollView on the top but unable to scroll the ScrollView in the background. Need help here.

I'hv pasted the sample layout and java code below.

Activity Layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


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

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

            <View
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="#ff0000" />

            <View
                android:id="@+id/imageView2"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="#ffff00" />

            <View
                android:id="@+id/imageView3"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="#00ffff" />
        </LinearLayout>
    </ScrollView>

    <ScrollView
        android:id="@+id/list_scrollview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

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


            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="20dp"
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="20dp"
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
                android:textAppearance="?android:attr/textAppearanceLarge" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="20dp"
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="20dp"
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="20dp"
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
                android:textAppearance="?android:attr/textAppearanceLarge" />


        </LinearLayout>
    </ScrollView>
</FrameLayout>

Activity code

package com.javatechig.overscroll;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ScrollView textScrollView = (ScrollView) findViewById(R.id.list_scrollview);
        textScrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });


        ScrollView backgroundScrollView = (ScrollView) findViewById(R.id.scrollView);

        backgroundScrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
    }
}

Edit-1 I have solve the problem by calling backgroundScrollView.onTouchEvent(event); from textScrollView onTouch() method. But is is appropriate? or Do we have any other way of doing this?

textScrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                backgroundScrollView.onTouchEvent(event);
                return true;
            }
        });

Upvotes: 1

Views: 917

Answers (1)

Nilanchala
Nilanchala

Reputation: 5941

I have solve the problem by calling backgroundScrollView.onTouchEvent(event). It works great.

textScrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                backgroundScrollView.onTouchEvent(event);
                return true;
            }
        });

Upvotes: 1

Related Questions