ScrollView中的可滚动TextView

时间:2015-02-14 15:50:49

标签: android textview scrollview ontouchlistener

我刚刚在我的代码中实现了“Pull to Refresh”并且遇到了一些问题。在向上滚动歌词时,会发生拉动刷新(PTR)动作。当用户触及lyricsTextView时,我想禁用它,或者更确切地说是TextView周围的整个空间。我的lyricsTextView上方显示的标题不可滚动。

<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>

我尝试使用我在StackOverFlow上找到的代码但是无法完成我想要的代码。

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;
    }
});

屏幕截图:

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

1 个答案:

答案 0 :(得分:0)

让我们调用可滚动的textview scrolltext和父卷轴视图scrollview

我在代码中可以看到的第一个问题是,在父代的OnTouchListener中,您引用了v的父代,并且在该上下文中v是父代本身。在您所依赖的答案中,您需要在scrolltext上获取父视图,而不是父视图的父视图。

其次,您正在设置OnTouchListeners的视图是错误的。您需要设置scrollviewtextview上的scrollview。在您发布的代码中,您在包含整个布局的片段上设置一个,在{{1}}上设置一个。 (我假设这些是基于其名称的片段和滚动视图。)

相关问题