How do I completely prevent a TextView from scrolling?

后端 未结 5 2113
借酒劲吻你
借酒劲吻你 2020-12-17 17:05

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 e

5条回答
  •  天涯浪人
    2020-12-17 17:40

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

提交回复
热议问题