Android में ScrollView से वर्टिकल स्क्रॉलिंग
ScrollView एक व्यू ग्रुप है जो अपने सिंगल चाइल्ड को वर्टिकली स्क्रॉल करने योग्य बनाता है। कंटेंट स्क्रीन से बड़ा होने पर उपयोगी।
चरण 1: प्रोजेक्ट बनाएं
Android Studio: File → New Project।
चरण 2: लेआउट (activity_main.xml) - ScrollView के साथ
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#C1C1C1"
android:src="@drawable/a"
android:scaleType="centerCrop"
android:contentDescription="Image A" />
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="30dp"
android:background="#C1C1C1"
android:src="@drawable/b"
android:scaleType="centerCrop"
android:contentDescription="Image B" />
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="30dp"
android:background="#C1C1C1"
android:src="@drawable/c"
android:scaleType="centerCrop"
android:contentDescription="Image C" />
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="30dp"
android:background="#C1C1C1"
android:src="@drawable/d"
android:scaleType="centerCrop"
android:contentDescription="Image D" />
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="30dp"
android:background="#C1C1C1"
android:src="@drawable/e"
android:scaleType="centerCrop"
android:contentDescription="Image E" />
</LinearLayout>
</ScrollView>
</LinearLayout>
मुख्य बिंदु:
- सिंगल चाइल्ड: ScrollView का केवल **एक** डायरेक्ट चाइल्ड हो सकता है (यहां LinearLayout)
- fillViewport="true": चाइल्ड को व्यूपोर्ट जितना ऊंचा बनाता है अगर कंटेंट छोटा हो
- वर्टिकल ओनली: क्षैतिज स्क्रॉल के लिए
HorizontalScrollViewउपयोग करें - नेस्टिंग न करें: ScrollView के अंदर ListView/RecyclerView न डालें
RecyclerView के साथ NestedScrollView (मॉडर्न):
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout ...>
<!-- अन्य व्यूज -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
आउटपुट



नतीजा: 5 बड़ी इमेजेस वर्टिकली स्क्रॉल होती हैं।