0

I just implemented "Pull to Refresh" to my code today and had a little problem. While scrolling the lyrics up, the pull to refresh (PTR) action occurs. I would like to disable that while the user touches on the lyricsTextView, or rather the whole space around that TextView. I have the title shown above the lyricsTextView which will not be scrollable.

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/playerFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:tag="playerFragment">


        <TextView
            android:id="@+id/titleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="title"
            android:textSize="25sp"
            android:theme="@style/AppTheme" />

        <TextView
            android:id="@+id/artistTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/titleTextView"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="artist"
            android:textSize="20sp" />

            <ImageView
                android:id="@+id/albumImageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_below="@id/artistTextView"
                android:alpha="76"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_launcher" />
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/artistTextView"
        android:orientation="vertical">
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

            android:id="@+id/lyricsScrollView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:scrollbars="none"
            android:layout_marginLeft="7dip"
            android:layout_marginRight="7dip"
            android:layout_marginTop="7dip">
            <TextView
                android:id="@+id/lyricsTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:maxLines="999999"
                android:scrollbars="vertical"
                android:text="lyrics/>
        </ScrollView>
    </RelativeLayout>
    </RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

I tried using this code which I found on StackOverFlow but wasn't able to accomplish what I want.

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

lyricsScrollView.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        // Disallow the touch request for parent scroll on touch of
        // child view
        v.getParent().requestDisallowInterceptTouchEvent(true);
        return false;
    }
});

screenshot:

https://dl.dropboxusercontent.com/u/36670712/Screenshot_2015-02-15-00-40-26%5B1%5D.png

  • Can you please link to the stack overflow question where you found this code? – SilverCorvus Feb 14 '15 at 16:14
  • @CurlyCorvus here you go: http://stackoverflow.com/questions/15062365/how-to-work-when-listview-inside-the-scrollview – Brandon Ong Feb 14 '15 at 16:17
  • see the following link for the answer: [http://stackoverflow.com/questions/16605486/edittext-not-scrollable-inside-scrollview/41134777#41134777](http://stackoverflow.com/questions/16605486/edittext-not-scrollable-inside-scrollview/41134777#41134777) – HungNM2 Dec 14 '16 at 04:22

1 Answers1

0

Let's call the scrollable textview scrolltext and the parent scrollview scrollview.

The first problem that i can see in your code is that in the parent's OnTouchListener, you are referencing the parent of v, and in that context v is the parent itself. In the answer you are relying on, you need to get the parent view on scrolltext, not the parent of the parent itself.

Second, the views you are setting the OnTouchListeners on are wrong. You need to set on on the scrollview and one on the textview. In the code you posted you set one on a fragment containing the entire layout and one on the scrollview. (I assume these are the fragment and scroll view based on their names.)

SilverCorvus
  • 2,671
  • 1
  • 13
  • 26
  • If I add a parent scrollview, the whole parent will be scrollable, take a look at the post I have edited. – Brandon Ong Feb 14 '15 at 16:52
  • @BrandonOng The lyricsTextView text view already has a parent scrollview, but i see now that the layout has two scrollviews. Which one of these scroll views has PTR? If it's mainScrollView, then why do you need to have a scrollable textview inside it's own scrollview? – SilverCorvus Feb 14 '15 at 16:59
  • I dont get what you mean... do you understand the pic i posted? – Brandon Ong Feb 14 '15 at 17:15