TextView automatically scrolls from top to bottom, and textview scrolls

Source: Internet
Author: User

TextView automatically scrolls from top to bottom, and textview scrolls
TextView achieves automatic scrolling from top to bottom

One requirement today is to have a TextView automatically scrolling effect.
I tried to use Padding or Layout_margin of TextView at the beginning. Although it can be implemented, it is very troublesome. It is difficult to determine when it reaches the bottom and the portability is not high.
Next we will find that we can use ScrollView to wrap TextView and use scrollTo of ScrollView.
However, you need to determine whether TextView can slide and whether it has reached the bottom.
I wrote an AutoScrollView here. You only need to use this class to implement automatic TextView scrolling.

TextView for scrolling


Here there is a manual slide, so there is a choppy effect, normal scrolling is relatively normal display.

AutoScrollView code
Package com. nsd. lwx. autoscrolltextview;/*** Created by lWX537240 on 2018/3/14. */import android. content. context; import android. graphics. canvas; import android. OS. handler; import android. OS. message; import android. util. attributeSet; import android. util. log; import android. view. motionEvent; import android. view. view; import android. widget. scrollView;/*** listens to ScrollView to scroll to the top or bottom for event interception */public class AutoScro LlView extends ScrollView {private boolean isScrolledToTop = true; // set private boolean isScrolledToBottom = false during initialization; private int paddingTop = 0; private final int MSG_SCROLL = 10; private final int MSG_SCROLL_Loop = 11; private boolean scrollAble = false; // whether the slide can be performed // The Three configurable attributes private boolean autoToScroll = true; // whether to automatically scroll private boolean scrollLoop = false; // whether to scroll private int fistTimeScrol cyclically L = 5000; // The number of seconds before the scroll starts. The default value is 5 seconds private int scrollRate = 50; // The number of milliseconds to scroll to a pixel public AutoScrollView (Context context) {this (context, null);} public AutoScrollView (Context context, AttributeSet attrs) {this (context, attrs, 0);} public AutoScrollView (Context context, AttributeSet attrs, int defStyleAttr) {this (context, attrs, defStyleAttr, 0);} public AutoScrollView (Context context, AttributeSet attrs, in T defStyleAttr, int defStyleRes) {super (context, attrs, defStyleAttr, defStyleRes);} private ISmartScrollChangedListener mSmartScrollChangedListener; /*** define the listener interface */public interface ISmartScrollChangedListener {void onScrolledToBottom (); // slide to the bottom void onScrolledToTop (); // slide to top} // set the listener to slide to top or bottom public void setScanScrollChangedListener (ISmartScrollChangedListener smartScrollChangedListener) {MS MartScrollChangedListener = smartScrollChangedListener;} // The callback Method for sliding a view in the ScrollView. This method is called after API 9, however, I have tested the error @ Override protected void onOverScrolled (int scrollX, int scrollY, boolean clampedX, boolean clampedY) {super. onOverScrolled (scrollX, scrollY, clampedX, clampedY); if (scrollY = 0) {response = clampedY; response = false;} else {isScrolledToTop = false; isScrolledTo Bottom = clampedY; // The system callback tells you when to slide to the Bottom} notifyScrollChangedListeners () ;} int lastY; // The touch event @ Override public boolean onTouchEvent (MotionEvent) {int y = (int) event. getY (); switch (event. getAction () {case MotionEvent. ACTION_DOWN: // record the coordinate of the touch point lastY = y; break; case MotionEvent. ACTION_MOVE: // calculate the offset int offsetY = y-lastY; // Add the offset paddingTop = paddingTop-offsetY to the current left, top, right, and bottom. /10; // do not ask me how the above 10 came from. I probably estimated that it should be 7 or 8, I intentionally reduced the value of scrollTo (0, paddingTop); break;} return true;} // The callback Method for sliding a view in ScrollView, it is said that this method was called before API 9. the SDK of my new version is also or calls back this method @ Override protected void onScrollChanged (int l, int t, int oldl, int oldt) {super. onScrollChanged (l, t, oldl, oldt); // if (android. OS. build. VERSION. SDK_INT <9) {// API 9 and later use the onOverScrolled method to listen, if (getScrollY () = 0) {isScrolledTo Top = true; isScrolledToBottom = false;} else if (getScrollY () + getHeight ()-getPaddingTop ()-getPaddingBottom () = getChildAt (0 ). getHeight () {isScrolledToBottom = true; isScrolledToTop = false;} else {isScrolledToTop = false; isScrolledToBottom = false;} policyscrollchangedlisteners (); //}} // determine whether to slide to the bottom or the top private void policyscrollchangedlisteners () {if (isScrolledToTop) {if (mSmartScr OllChangedListener! = Null) {mSmartScrollChangedListener. onScrolledToTop () ;}} else if (isScrolledToBottom) {mHandler. removeMessages (MSG_SCROLL); if (! ScrollLoop) {scrollAble = false;} if (scrollLoop) {mHandler. sendEmptyMessageDelayed (MSG_SCROLL_Loop, fistTimeScroll);} if (mSmartScrollChangedListener! = Null) {mSmartScrollChangedListener. onScrolledToBottom () ;}}// handler private Handler mHandler = new Handler () {@ Override public void handleMessage (Message msg) {super. handleMessage (msg); switch (msg. what) {case MSG_SCROLL: if (scrollAble & autoToScroll) {scrollTo (0, paddingTop); paddingTop + = 1; mHandler. removeMessages (MSG_SCROLL); mHandler. sendEmptyMessageDelayed (MSG_SCROLL, scrollRate);} break; case MSG_SCROLL_Loop: paddingTop = 0; autoToScroll = true; mHandler. lag (MSG_SCROLL, fistTimeScroll) ;}}; // obtains the height of the subview and ScrollView to determine whether to slide the View @ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); View childAt = getChildAt (0); int childMeasuredHeight = childAt. getMeasuredHeight (); // get the height of the Child control int measuredHeight = getMeasuredHeight (); // obtain the height of the ScrollView // Log. e ("onMeasure", "childMeasuredHeight:" + childMeasuredHeight + ", measuredHeight:" + measuredHeight); if (childMeasuredHeight> measuredHeight) {// If the height of the Child control is greater than that of the parent control, you must scroll scrollAble = true; paddingTop = 0; mHandler. sendEmptyMessageDelayed (MSG_SCROLL, fistTimeScroll);} else {scrollAble = false; }}// set whether to automatically scroll public void setAutoToScroll (boolean autoToScroll) {this. autoToScroll = autoToScroll;} // sets the time when the first rolling starts. public void setFistTimeScroll (int fistTimeScroll) {this. fistTimeScroll = fistTimeScroll; mHandler. removeMessages (MSG_SCROLL); mHandler. sendEmptyMessageDelayed (MSG_SCROLL, fistTimeScroll);} // sets the rolling rate. The number of milliseconds to scroll to a pixel public void setScrollRate (int scrollRate) {this. scrollRate = scrollRate;} // sets whether to cyclically scroll public void setScrollLoop (boolean scrollLoop) {this. scrollLoop = scrollLoop ;}}
Other code of the Project 1. layout file activity_main.xml
 
     
      
           
        
    
       
   
    
    
   
  
 

2. MainActivity java code

Package com. nsd. lwx. autoscrolltextview; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. textView; import android. widget. toast; public class MainActivity extends AppCompatActivity {TextView textView; AutoScrollView autoScrollView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView (); initData (); initEvent ();} private void initView () {textView = findViewById (R. id. textView); autoScrollView = findViewById (R. id. scrollView);} private void initData () {autoScrollView. setAutoToScroll (true); // You can automatically slide the autoScrollView. setFistTimeScroll (2000); // sets the autoScrollView time for the first automatic slide. setScrollRate (50); // set the sliding rate autoScrollView. setScrollLoop (false); // set whether to slide cyclically} // whether the listener reaches the header or the private void initEvent () {autoScrollView. setScanScrollChangedListener (new AutoScrollView. ISmartScrollChangedListener () {@ Override public void onScrolledToBottom () {Toast. makeText (MainActivity. this, "bottom", Toast. LENGTH_SHORT ). show () ;}@ Override public void onScrolledToTop () {Toast. makeText (MainActivity. this, "TOP", Toast. LENGTH_SHORT ). show () ;}}) ;}// set a long public void setLongText (View view) {textView for the text. setText ("1aaaaaaaaaaa \ n2aaaaaaaaaaa \ Alibaba \ n" + "6aaaaaaaaaaa \ Alibaba \ n" + "11aaaaaaaaa \ Alibaba \ n14aaaaaaaaaaa \ Alibaba ");} // set the short string public void setShortText (View view) {textView. setText ("1aaaaaaaaaaa \ n2aaaaaaaaaaaaa ");}}

Let's take a look at the rolling effect:

Here, the TextView that implements automatic sliding is simple.
Some people may have doubts: If the height of the ScrollView is match_parent, and the height of the TextView is match_parent, is the scrolling effect effective?
In fact, it is effective. The size of the measurement layout is not absolutely the same as that of the form layout. If the content exceeds the size of the form, the measurement height will also exceed the size of the form. I have already tried it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.