android scrollable textview inside scrollview

后端 未结 5 1538
無奈伤痛
無奈伤痛 2020-12-17 14:38

i need to create like scrolling TextView inside the ScrollView.. like 1 main ScrollView inside that scrollable TextView i

相关标签:
5条回答
  • 2020-12-17 15:14

    This problem is a little complicated and I don't have enough time to explain, but you can easily get rid of it by adding these onTouch classes at the end of onCreat class or onCreatView class (if you are using fragment).

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

    and also for other textviews.

    0 讨论(0)
  • 2020-12-17 15:18

    Just set the

    android:maxLines = "AN_INTEGER"
    
    android:scrollbars = "vertical"
    

    properties of your TextView in your layout's xml file.

    Then use:

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

    Hope it will work

    0 讨论(0)
  • 2020-12-17 15:20

    Actually, the only method (that worked for me) to make a TextView/EditText to scroll inside a ScrollView is

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

    with

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

    (No need to set an android:maxLines attributes)

    use

    TextView textView = findViewById(R.id.textView);
    Utils.enableScroll(textView);
    
    0 讨论(0)
  • 2020-12-17 15:25

    You can also simply add a little elevation to the 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" />
    

    It did the trick for me. :)

    0 讨论(0)
  • 2020-12-17 15:32

    First, set android:scrollbars="vertical":

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

    Also, add this extension function:

    /**
     * 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
        }
    

    Then call:

    actv.makeScrollableInsideScrollView()
    
    0 讨论(0)
提交回复
热议问题