SoFunction
Updated on 2025-03-02

Android uses ScrollView to achieve scrolling effect

This example shares the specific code for ScrollView to achieve scrolling effect for your reference. The specific content is as follows

If the content of long text exceeds one screen, only one screen of content can be displayed
Set up ScrollView to browse the following contents by scrolling

If you change the label to <HorizontalScrollView></HorizontalScrollView>, it will be a horizontal scrolling effect.

xml file:

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt; xmlns:andro
  xmlns:app="/apk/res-auto"
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=""&gt;
 
  &lt;ScrollView
    android:
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none"&gt;&lt;!--The right scroll bar is not displayed --&gt;
 
    &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:
      /&gt;
 
  &lt;/ScrollView&gt;
 
&lt;/&gt;

MainActivity File:

package ;
 
import ;
import ;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
 
public class MainActivity extends Activity {
 
  private TextView tv;
  private ScrollView scrollView;
 
  @SuppressLint("ClickableViewAccessibility")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    tv=findViewById();
    (getResources().getString());
 
    scrollView=findViewById();
    //Set the listener    (new () {
      public boolean onTouch(View view, MotionEvent motionEvent) {
        //Judge the parameters of motionEvent        switch (()){
          case MotionEvent.ACTION_UP:
          {
            break;
          }
          case MotionEvent.ACTION_DOWN:
          {
            break;
          }
          case MotionEvent.ACTION_MOVE:{
            /*
             * (1) getScrollY()--The distance of the scroll bar sliding, calculated from 0
             * (2)getMeasuredHeight()--Full length
             * (3)getHeight()--the height of a screen
             * */
            //Top Status            if(()&lt;=0){
              ("Main","Slide to top");
            }
            //Bottom Status            if((0).getMeasuredHeight()&lt;=()+()){
              ("Main","Slide to the bottom");
              (getResources().getString());//Add this text again when sliding to the bottom            }
 
            break;
          }
 
        }
        return false;
      }
    });
  }
 
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.