ScrollView 不使用 onTouch 滚动

ScrollView not scrolling with onTouch

我的滚动视图没有滚动。我有一种感觉,这可能是由于 "onTouch" 方法导致的,我没有滚动而是增加了显示的计数器。我试图找到一些解决方案,但 none 似乎与我的直接相关,并且任何更改滚动视图的尝试都没有奏效。 我不确定如何解决这个问题,我们将不胜感激。

Java代码

package com.alanlyne.tbm.Variables.varL;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alanlyne.tbm.Menu.Menu;
import com.alanlyne.tbm.Menu.menuCounter;
import com.alanlyne.tbm.R;
import com.alanlyne.tbm.Variables.Var;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class varL2 extends AppCompatActivity {
    TextView t0, t1, t2, t3, t4, t5, t6, t7, t8, x;
    Button b1;
    int counter = 0;
    DatabaseReference databaseName;
    private long mLastClickTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_var_l2);
        databaseName = FirebaseDatabase.getInstance().getReference("counter");

        t0 = findViewById(R.id.t0);
        t1 = findViewById(R.id.t1);
        t2 = findViewById(R.id.t2);
        t3 = findViewById(R.id.t3);
        t4 = findViewById(R.id.t4);
        t5 = findViewById(R.id.t5);
        t6 = findViewById(R.id.t6);
        t7 = findViewById(R.id.t7);
        t8 = findViewById(R.id.t8);
        b1 = findViewById(R.id.b1);

        x = findViewById(R.id.x);
        x.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent x = new Intent(varL2.this, Var.class);
                startActivity(x);
            }
        });

        View view = findViewById(R.id.ScrollView01);
        view.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent event) {

                if (SystemClock.elapsedRealtime() - mLastClickTime < 500) {
                    System.out.println("Fun");
                }
                else{
                    mLastClickTime = SystemClock.elapsedRealtime();

                    counter++;

                    System.out.println(counter);
                    if (counter > 0) {
                        TextView tv = findViewById(R.id.t1);
                        tv.setVisibility(View.VISIBLE);

                    }
                    if (counter > 1) {
                        TextView tv = findViewById(R.id.t2);
                        tv.setVisibility(View.VISIBLE);
                    }
                    if (counter > 2) {
                        TextView tv = findViewById(R.id.t3);
                        tv.setVisibility(View.VISIBLE);
                    }
                    if (counter > 3) {
                        TextView tv = findViewById(R.id.t4);
                        tv.setVisibility(View.VISIBLE);
                    }
                    if (counter > 4) {
                        TextView tv = findViewById(R.id.t5);
                        tv.setVisibility(View.VISIBLE);
                    }
                    if (counter > 5) {
                        TextView tv = findViewById(R.id.t6);
                        tv.setVisibility(View.VISIBLE);
                    }
                    if (counter > 6) {
                        TextView tv = findViewById(R.id.t7);
                        tv.setVisibility(View.VISIBLE);
                    }
                    if (counter > 7) {
                        TextView tv = findViewById(R.id.t8);
                        tv.setVisibility(View.VISIBLE);
                    }
                    if (counter > 8) {
                        Button b1 = findViewById(R.id.b1);
                        b1.setVisibility(View.VISIBLE);
                        b1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent Menu = new Intent(varL2.this, Var.class);
                                startActivity(Menu);
                                addCounter();
                            }
                        });
                    }}
                return true;
            }
        });
    }
    private void addCounter(){
        if(Menu.counter == 12) {
            Menu.counter = 13;
            menuCounter counter = new menuCounter(Menu.counter);
            databaseName.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(counter);
            System.out.println("AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH");
        }
    }
}

XML代码

<?xml version="1.0" encoding="utf-8"?>

<ScrollView 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:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bgcolour"
    tools:context=".Variables.varL.varL2">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:id="@+id/t0"
            android:layout_width="300sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:background="@drawable/edit_round"
            android:gravity="center"
            android:padding="10dp"
            android:text="There are 8 primitive data types for creating variables in Java."
            android:textColor="@android:color/black"
            android:textColorHighlight="#FF000000"
            android:textSize="15sp"
            android:visibility="visible"
            app:layout_constraintBottom_toTopOf="@+id/t1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/t1"
            android:layout_width="300sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/edit_round"
            android:gravity="center"
            android:padding="10dp"
            android:text="byte\n8 bit integer\n-128 to 127\nDefault of 0"
            android:textColor="@android:color/black"
            android:textColorHighlight="#FF000000"
            android:textSize="15sp"
            android:visibility="invisible"
            app:layout_constraintBottom_toTopOf="@+id/t2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.495"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/t0" />

        <TextView
            android:id="@+id/t2"
            android:layout_width="300sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/edit_round"
            android:gravity="center"
            android:padding="10dp"
            android:text="short\n16 bit integer\n-32768 to 32767\nDefault of 0"
            android:textColor="@android:color/black"
            android:textColorHighlight="#FF000000"
            android:textSize="15sp"
            android:visibility="invisible"
            app:layout_constraintBottom_toTopOf="@+id/t3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.495"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/t1" />

        <TextView
            android:id="@+id/t3"
            android:layout_width="300sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/edit_round"
            android:gravity="center"
            android:padding="10dp"
            android:text="int\n32 bit integer\n-2147483648 to 2147483647\nDefault of 0"
            android:textColor="@android:color/black"
            android:textColorHighlight="#FF000000"
            android:textSize="15sp"
            android:visibility="invisible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.495"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/t2" />

        <TextView
            android:id="@+id/t4"
            android:layout_width="300sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/edit_round"
            android:gravity="center"
            android:padding="10dp"
            android:text="long\n64 bit integer\n-9223372036854775808 to 9223372036854775807\nDefault of 0L"
            android:textColor="@android:color/black"
            android:textColorHighlight="#FF000000"
            android:textSize="15sp"
            android:visibility="invisible"
            app:layout_constraintBottom_toTopOf="@+id/t5"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.495"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/t3" />

        <TextView
            android:id="@+id/t5"
            android:layout_width="300sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/edit_round"
            android:gravity="center"
            android:padding="10dp"
            android:text="float\nDecimal point number\n1.5*10^-45 to 3.4*10^38\nDefault of 0.0f"
            android:textColor="@android:color/black"
            android:textColorHighlight="#FF000000"
            android:textSize="15sp"
            android:visibility="invisible"
            app:layout_constraintBottom_toTopOf="@+id/t6"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.495"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/t4" />


        <TextView
            android:id="@+id/t6"
            android:layout_width="300sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/edit_round"
            android:gravity="center"
            android:padding="10dp"
            android:text="double\nDecimal point number\n5.0*10^-324 to 1.7*10^308\nDefault of 0.0"
            android:textColor="@android:color/black"
            android:textColorHighlight="#FF000000"
            android:textSize="15sp"
            android:visibility="invisible"
            app:layout_constraintBottom_toTopOf="@+id/t7"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/t5" />

        <TextView
            android:id="@+id/t7"
            android:layout_width="300sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/edit_round"
            android:gravity="center"
            android:padding="10dp"
            android:text="char\nA unicode character e.g 'a','b'\n(\u0000 to \uffff) or (0 to 65535)\nDefault of null"
            android:textColor="@android:color/black"
            android:textColorHighlight="#FF000000"
            android:textSize="15sp"
            android:visibility="invisible"
            app:layout_constraintBottom_toTopOf="@+id/t8"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.504"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/t6" />

        <TextView
            android:id="@+id/t8"
            android:layout_width="300sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/edit_round"
            android:gravity="center"
            android:padding="10dp"
            android:text="boolean\nLogical true or false\ntrue or false\nDefault of false"
            android:textColor="@android:color/black"
            android:textColorHighlight="#FF000000"
            android:textSize="15sp"
            android:visibility="invisible"
            app:layout_constraintBottom_toTopOf="@+id/b1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.504"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/t7" />

        <Button
            android:id="@+id/b1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="150dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="150dp"
            android:layout_marginBottom="20dp"
            android:background="@drawable/b_round"
            android:text="Continue"
            android:textColor="@android:color/white"
            android:visibility="invisible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/t8" />

        <TextView
            android:id="@+id/x"
            android:layout_width="15dp"
            android:layout_height="35dp"
            android:layout_marginStart="10dp"
            android:text="x"
            android:textColor="@android:color/white"
            android:textSize="30sp"
            app:layout_constraintBottom_toTopOf="@+id/t0"
            app:layout_constraintEnd_toEndOf="@+id/t0"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />


    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

通过

解决了问题
View view = findViewById(R.id.ScrollView01);

引用约束布局的 id 而不是滚动视图。

通常在处理 ScrollView 时需要将 android:fillViewport 设置为 true

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">