【问题标题】:Scrollable TextView inside ScrollViewScrollView 内的可滚动 TextView
【发布时间】:2015-02-14 15:50:48
【问题描述】:

我今天刚刚在我的代码中实现了“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

【问题讨论】:

标签: android textview scrollview ontouchlistener


【解决方案1】:

让我们调用可滚动的文本视图scrolltext 和父滚动视图scrollview

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

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

【讨论】:

  • 如果我添加一个父滚动视图,整个父级将是可滚动的,看看我编辑的帖子。
  • @BrandonOng LyricsTextView 文本视图已经有一个父滚动视图,但我现在看到布局有两个滚动视图。这些滚动视图中的哪一个具有 PTR?如果是mainScrollView,那你为什么需要在它自己的scrollview里面有一个可滚动的textview呢?
  • 我不明白你的意思......你明白我发布的图片吗?
猜你喜欢
  • 1970-01-01
  • 2023-04-05
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
  • 2013-05-12
相关资源
最近更新 更多