androidandroid-layouttextviewandroid-scroll

How do I completely prevent a TextView from scrolling?


I have a TextView with a lot of text. This TextView has maxLines set, so it only shows the first 8 or so lines. I also have a "Read More" button so I handle expanding the TextView on my own.

My problem is that sometimes the TextView scrolls a little (just half a line at a time), even though I never specified any scroll bars. This issue is made worse because the TextView is inside a ListView, so when the user scrolls the main ListView, the TextView sometimes scrolls a little, like this:

blahblah

How do I prevent the TextView from scrolling?


Solution

  • I have the same problem,My solution is create a NoScrollTextView extends TextView like this

     public class NoScrollTextView extends TextView {
        public NoScrollTextView(Context context) {
            super(context);
        }
    
        public NoScrollTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public NoScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public NoScrollTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
    
        @Override
        public void scrollTo(int x, int y) {
            //do nothing
        }
    }
    

    set scrollTo do nothing

    In Kotlin:

    class NonScrollingTextView : TextView {
        constructor(context: Context) : super(context) {}
        constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
        constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        }
    
        override fun scrollTo(x: Int, y: Int) {
            //do nothing
        }
    }