Android uses ScrollView to achieve scrolling effect

  • 2021-12-11 18:50:09
  • OfStack

In this paper, we share the specific code of ScrollView to achieve scrolling effect for your reference. The specific contents are as follows

If the content of long text exceeds 1 screen, only 1 screen content can be displayed
Set ScrollView to scroll through the following

If you change the label to < HorizontalScrollView > < /HorizontalScrollView > It is a horizontal scrolling effect

xml file:


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.lenovo.scrollview.MainActivity">
 
  <ScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none"><!-- Do not display the right scroll bar  -->
 
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/content"
      />
 
  </ScrollView>
 
</android.support.constraint.ConstraintLayout>

MainActivity file:


package com.example.lenovo.scrollview;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
  private TextView tv;
  private ScrollView scrollView;
 
  @SuppressLint("ClickableViewAccessibility")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv=findViewById(R.id.content);
    tv.setText(getResources().getString(R.string.content));
 
    scrollView=findViewById(R.id.scroll);
    // Set up the listener 
    scrollView.setOnTouchListener(new View.OnTouchListener() {
      public boolean onTouch(View view, MotionEvent motionEvent) {
        // Right motionEvent To judge the parameters of 
        switch (motionEvent.getAction()){
          case MotionEvent.ACTION_UP:
          {
            break;
          }
          case MotionEvent.ACTION_DOWN:
          {
            break;
          }
          case MotionEvent.ACTION_MOVE:{
            /*
            * (1)getScrollY()-- The distance the scroll bar slides , From 0 Begin calculation 
            * (2)getMeasuredHeight()-- Full length 
            * (3)getHeight()--1 Height of screen 
            * */
            // Top state 
            if(scrollView.getScrollY()<=0){
              Log.i("Main"," Slide to the top ");
            }
            // Bottom state 
            if(scrollView.getChildAt(0).getMeasuredHeight()<=scrollView.getHeight()+scrollView.getScrollY()){
              Log.i("Main"," Slide to the bottom ");
              tv.append(getResources().getString(R.string.content));// Append this text again when sliding to the bottom 
            }
 
            break;
          }
 
        }
        return false;
      }
    });
  }
 
}

Related articles: