Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีปิดใช้งาน ScrollView แบบโปรแกรมใน Android

ภาพรวม

ตัวอย่างนี้แสดงวิธีการปิดการเลื่อน (Scroll) ของ ScrollView โดยการตั้งค่า OnTouchListener คืนค่า true เพื่อบล็อก Touch Event

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio ไปที่ File → New Project แล้วกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอ (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="16dp">
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 4" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 5" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 6" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 7" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 8" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 9" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 10" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 11" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 12" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 13" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 14" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 15" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 16" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 17" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 18" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 19" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 20" />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ดหลัก (MainActivity.java)

package com.example.scrollviewdisable;

import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ScrollView scrollView = findViewById(R.id.scrollView);
        
        // ปิดการ Scroll โดยคืนค่า true (Consume Event)
        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Toast.makeText(MainActivity.this, "ScrollView Disabled", Toast.LENGTH_SHORT).show();
                return true; // true = จัดการ Event เอง (บล็อก Scroll)
            }
        });
    }
}

ขั้นตอนที่ 4: ตั้งค่า Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.scrollviewdisable">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

การทดสอบ

รันแอปพลิเคชันบนอุปกรณ์จริง พยายามเลื่อน ScrollView จะไม่เลื่อนได้ และแสดง Toast "ScrollView Disabled"

วิธีปิดใช้งาน ScrollView แบบโปรแกรมใน Android

วิธีอื่นๆ และ Best Practices

1. ใช้ setEnabled(false) - แต่อาจไม่ทำงานกับ ScrollView

scrollView.setEnabled(false); // ไม่แนะนำ ScrollView ไม่สนใจ enabled state

2. สร้าง Custom ScrollView (แนะนำ)

public class LockableScrollView extends ScrollView {
    private boolean scrollable = true;
    
    public void setScrollable(boolean scrollable) {
        this.scrollable = scrollable;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return scrollable && super.onTouchEvent(ev);
    }
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return scrollable && super.onInterceptTouchEvent(ev);
    }
}

// ใช้งาน
LockableScrollView scrollView = findViewById(R.id.scrollView);
scrollView.setScrollable(false); // ปิด
scrollView.setScrollable(true);  // เปิด

3. ใช้ requestDisallowInterceptTouchEvent (สำหรับ Nested Scroll)

scrollView.requestDisallowInterceptTouchEvent(true); // บล็อก Parent Intercept

สรุป

  • OnTouchListener + return true: เร็ว แต่จะบล็อกทุก Touch รวมถึง Button Click ด้วย
  • Custom ScrollView: ยืดหยุ่นที่สุด ควบคุมได้แบบเปิด/ปิด
  • หากมี Button ภายใน ScrollView วิธี OnTouchListener จะทำให้ Button ไม่กดได้ ต้องใช้ Custom View แทน