Giới thiệu
Bài viết này hướng dẫn cách vô hiệu hóa cuộn (scroll) của ScrollView theo điều kiện hoặc hoàn toàn bằng code Java/Kotlin trong Android.
Các bước thực hiện
Bước 1: Tạo dự án mới
Khởi tạo project Android Studio mới.
Bước 2: Layout có ScrollView (res/layout/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:orientation="vertical"
android:gravity="center"
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>
Bước 3: Code Java vô hiệu hóa ScrollView (src/MainActivity.java)
package app.com.sample;
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 {
private ScrollView scrollView;
private boolean isScrollEnabled = true;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = findViewById(R.id.scrollView);
// Cách 1: setOnTouchListener - chặn hoàn toàn sự kiện touch
scrollView.setOnTouchListener((v, event) -> {
if (!isScrollEnabled) {
Toast.makeText(MainActivity.this, "ScrollView đã bị vô hiệu hóa", Toast.LENGTH_SHORT).show();
return true; // true = consume event, không cho scroll
}
return false; // false = cho phép xử lý bình thường
});
// Demo: Tự động disable sau 3 giây (tùy chọn)
// scrollView.postDelayed(() -> {
// isScrollEnabled = false;
// Toast.makeText(this, "Scroll disabled!", Toast.LENGTH_SHORT).show();
// }, 3000);
}
// Cách 2: Custom ScrollView class (tái sử dụng tốt hơn)
public static class LockableScrollView extends ScrollView {
private boolean scrollEnabled = true;
public LockableScrollView(Context context) { super(context); }
public LockableScrollView(Context context, AttributeSet attrs) { super(context, attrs); }
public LockableScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
public void setScrollingEnabled(boolean enabled) {
this.scrollEnabled = enabled;
}
public boolean isScrollingEnabled() {
return scrollEnabled;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return scrollEnabled && super.onTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return scrollEnabled && super.onInterceptTouchEvent(ev);
}
}
// Cách 3: Vô hiệu hóa tạm thời khi đang scroll programmatically
public void smoothScrollAndDisable(int x, int y) {
scrollView.post(() -> {
// Disable trước khi smooth scroll
isScrollEnabled = false;
scrollView.smoothScrollTo(x, y);
// Re-enable sau khi scroll xong (cần OnScrollChangeListener API 23+)
scrollView.setOnScrollChangeListener((v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
if (!v.canScrollVertically(1) && !v.canScrollVertically(-1)) {
isScrollEnabled = true;
v.setOnScrollChangeListener(null);
}
});
});
}
}
Giải thích 3 cách:
| Cách | Mô tả | Ưu điểm | Nhược điểm |
|---|---|---|---|
| OnTouchListener return true | Chặn touch event ở view cha | Đơn giản, không cần custom class | Chặn cả click vào child view |
| Custom LockableScrollView | Override onTouchEvent & onInterceptTouchEvent | Tái sử dụng, kiểm soát tốt, không chặn click child | Cần dùng custom view trong XML |
| setOnScrollChangeListener | API 23+, detect khi scroll kết thúc | Cho phép scroll bằng code nhưng chặn user | Chỉ API 23+ |
