【问题标题】:android scrollable textview inside scrollview滚动视图内的android可滚动文本视图
【发布时间】:2014-04-20 19:02:20
【问题描述】:

我需要在 ScrollView 内创建像滚动 TextView 一样。像 1 main ScrollView 在可滚动 TextView 内我已将此代码放在 RelativeLayout

  <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/img_view_flag"
        android:layout_marginTop="17dp" 
        android:paddingBottom="20dip">

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



                <TextView
                    android:id="@+id/tv_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/rel_layout"
                    android:layout_marginTop="15dp"
                    android:maxLines="7"
                    android:scrollbars="vertical"
                    android:text="Medium Text"
                    android:paddingBottom="10dip"
                     android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black" />


                <RelativeLayout
                    android:id="@+id/rel_layout2"
                    android:layout_width="match_parent"
                    android:layout_height="4dp"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/tv_desc"
                    android:background="@color/black" >
                </RelativeLayout>

                <TextView
                    android:id="@+id/tv_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/rel_layout2"
                    android:layout_marginTop="24dp"
                    android:maxLines="5"
                    android:scrollbars="vertical"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/rel_layout2"
                    android:layout_centerHorizontal="true"
                    android:text="Title"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/black" />

                <RelativeLayout
                    android:id="@+id/rel_layout3"
                    android:layout_width="match_parent"
                    android:layout_height="4dp"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/tv_title"
                    android:background="@color/black" >
                </RelativeLayout>

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/rel_layout3"
                    android:layout_centerHorizontal="true"
                    android:text="Lyricist"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/tv_l"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/textView2"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black" />

                <RelativeLayout
                    android:id="@+id/rel_layout4"
                    android:layout_width="match_parent"
                    android:layout_height="4dp"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/tv_l"
                    android:background="@color/black" >
                </RelativeLayout>

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/textView2"
                    android:layout_below="@+id/rel_layout4"
                    android:text="Composer"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/tv_composer"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/textView3"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black" />

                <RelativeLayout
                    android:id="@+id/rel_layout5"
                    android:layout_width="match_parent"
                    android:layout_height="4dp"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/tv_composer"
                    android:background="@color/black" >
                </RelativeLayout>

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/textView3"
                    android:layout_below="@+id/rel_layout5"
                    android:text="Adopted"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/tv_adopted"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/textView4"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black" />

                <RelativeLayout
                    android:id="@+id/rel_layout6"
                    android:layout_width="match_parent"
                    android:layout_height="4dp"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/tv_adopted"
                    android:background="@color/black" >
                </RelativeLayout>

                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Lyrics"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/tv_lyri"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/textView5"
                    android:maxLines="10"
                    android:scrollbars="vertical"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black" />

        </LinearLayout>
    </ScrollView>

但不适合我.... 主滚动条工作

【问题讨论】:

标签: android scroll textview scrollview


【解决方案1】:

这个问题有点复杂,我没有足够的时间解释,但是你可以通过在 onCreat 类或 onCreatView 类的末尾添加这些 onTouch 类来轻松摆脱它(如果你使用的是片段)。

scrollview1.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                tv_desc.getParent().requestDisallowInterceptTouchEvent(false);

                return false;
            }
        });

tv_desc.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                tv_desc.getParent().requestDisallowInterceptTouchEvent(true);

                return false;
            }
        });

也适用于其他文本视图。

【讨论】:

  • 我只需要在MotionEvent.ACTION_DOWN 上调用requestDisallowInterceptTouchEvent(true) 即可允许滚动。
【解决方案2】:

只需设置

android:maxLines = "AN_INTEGER"

android:scrollbars = "vertical"

布局的 xml 文件中 TextView 的属性。

然后使用:

tv_desc.setMovementMethod(new ScrollingMovementMethod());


scrollview.setOnTouchListener(new OnTouchListener() { 

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            tv_desc.getParent().requestDisallowInterceptTouchEvent(false);

            return false;
        }
    });

tv_desc.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            tv_desc.getParent().requestDisallowInterceptTouchEvent(true);

            return false;
        }
    });

希望能成功

【讨论】:

    【解决方案3】:

    实际上,使 TextView/EditText 在 ScrollView 内滚动的唯一方法(对我有用)是

    public class Utils {
        public static void enableScroll(View view) {
            if (view instanceof TextView) {
                TextView textView = (TextView) view;
                textView.setMovementMethod(new ScrollingMovementMethod());
            }
    
            view.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    switch (event.getAction() & MotionEvent.ACTION_MASK) {
                        case MotionEvent.ACTION_UP:
                            v.getParent().requestDisallowInterceptTouchEvent(false);
                            break;
                    }
                    return false;
                }
            });
        }
    }
    

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />
    

    (无需设置android:maxLines属性)

    使用

    TextView textView = findViewById(R.id.textView);
    Utils.enableScroll(textView);
    

    【讨论】:

      【解决方案4】:

      首先,设置android:scrollbars="vertical":

      <AppCompatTextView
          android:id="@+id/actv"
          ...
          android:scrollbars="vertical" />
      

      另外,添加这个扩展功能:

      /**
       * If this [AppCompatTextView] is placed inside ScrollView then we allow it get scrolled inside
       * that ScrollView
       */
      fun AppCompatTextView.makeScrollableInsideScrollView() {
          movementMethod = ScrollingMovementMethod()
          setOnTouchListener { v, event ->
              v.parent.requestDisallowInterceptTouchEvent(true)
              when (event.action and MotionEvent.ACTION_MASK) {
                  MotionEvent.ACTION_UP -> {
                      v.parent.requestDisallowInterceptTouchEvent(false)
                      //It is required to call performClick() in onTouch event.
                      performClick()
                  }
              }
              false
          }
      

      然后调用:

      actv.makeScrollableInsideScrollView()
      

      【讨论】:

      • 这就是我要找的! :)
      • 为什么需要 performClick()?
      • 需要执行点击,因为检测到点击而不是滚动/滑动等
      【解决方案5】:

      您也可以简单地向 textView 添加一点高度。

          <TextView
              android:id="@+id/tv_composer"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:elevation="1dp"
              android:layout_below="@+id/textView3"
              android:text="Small Text"
              android:textAppearance="?android:attr/textAppearanceSmall"
              android:textColor="@color/black" />
      

      它对我有用。 :)

      【讨论】:

      • 在 2021 年 8 月的 Galaxy s9 上不适合我。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      相关资源
      最近更新 更多