Java 滚动视图不使用onTouch滚动

Java 滚动视图不使用onTouch滚动,java,android,android-scrollview,Java,Android,Android Scrollview,我的滚动视图没有滚动。我有一种感觉,这可能是由于我有一个“onTouch”方法,它不是滚动而是增加显示的计数器。我试图找到一些解决方案,但似乎没有一个与我的直接相关,任何改变滚动视图的尝试都没有成功。 我不知道如何避开这个问题,任何帮助都将不胜感激 Java代码 package com.alanlyne.tbm.Variables.varL; import androidx.appcompat.app.AppCompatActivity; import android.content.Int

我的滚动视图没有滚动。我有一种感觉,这可能是由于我有一个“onTouch”方法,它不是滚动而是增加显示的计数器。我试图找到一些解决方案,但似乎没有一个与我的直接相关,任何改变滚动视图的尝试都没有成功。 我不知道如何避开这个问题,任何帮助都将不胜感激

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");
        }
    }
}
package com.alanlyne.tbm.Variables.varL;
导入androidx.appcompat.app.appcompat活动;
导入android.content.Intent;
导入android.os.Bundle;
导入android.os.SystemClock;
导入android.view.MotionEvent;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.TextView;
导入com.alanlyne.tbm.Menu.Menu;
导入com.alanlyne.tbm.Menu.Menu计数器;
导入com.alanlyne.tbm.R;
导入com.alanlyne.tbm.Variables.Var;
导入com.google.firebase.auth.FirebaseAuth;
导入com.google.firebase.database.DatabaseReference;
导入com.google.firebase.database.FirebaseDatabase;
公共类varL2扩展了AppCompatActivity{
文本视图t0、t1、t2、t3、t4、t5、t6、t7、t8、x;
按钮b1;
int计数器=0;
数据库引用数据库名称;
私有长mLastClickTime=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u var\u l2);
databaseName=FirebaseDatabase.getInstance().getReference(“计数器”);
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(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
意图x=新意图(varL2.this,Var.class);
星触觉(x);
}
});
视图=findViewById(R.id.ScrollView01);
view.setOnTouchListener(新的view.OnTouchListener(){
@凌驾
公共布尔onTouch(视图、运动事件){
if(SystemClock.elapsedRealtime()-mLastClickTime<500){
System.out.println(“乐趣”);
}
否则{
mLastClickTime=SystemClock.elapsedRealtime();
计数器++;
系统输出打印项次(计数器);
如果(计数器>0){
TextView tv=findViewById(R.id.t1);
tv.setVisibility(View.VISIBLE);
}
如果(计数器>1){
TextView tv=findViewById(R.id.t2);
tv.setVisibility(View.VISIBLE);
}
如果(计数器>2){
TextView tv=findViewById(R.id.t3);
tv.setVisibility(View.VISIBLE);
}
如果(计数器>3){
TextView tv=findViewById(R.id.t4);
tv.setVisibility(View.VISIBLE);
}
如果(计数器>4){
TextView tv=findViewById(R.id.t5);
tv.setVisibility(View.VISIBLE);
}
如果(计数器>5){
TextView tv=findViewById(R.id.t6);
tv.setVisibility(View.VISIBLE);
}
如果(计数器>6){
TextView tv=findViewById(R.id.t7);
tv.setVisibility(View.VISIBLE);
}
如果(计数器>7){
TextView tv=findViewById(R.id.t8);
tv.setVisibility(View.VISIBLE);
}
如果(计数器>8){
按钮b1=findViewById(R.id.b1);
b1.设置可见性(View.VISIBLE);
b1.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意图菜单=新意图(varL2.this,Var.class);
启动触觉(菜单);
addCounter();
}
});
}}
返回true;
}
});
}
私有void addCounter(){
如果(Menu.counter==12){
Menu.counter=13;
菜单计数器=新菜单计数器(Menu.counter);
databaseName.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(计数器);
System.out.println(“ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh”);
}
}
}
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,而不是滚动视图。

通常在处理滚动视图时,需要将android:fillViewport设置为true

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