यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android ListView में स्क्रॉल दिशा (ऊपर/नीचे) को कैसे डिटेक्ट किया जाता है। AbsListView.OnScrollListener का उपयोग किया जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: activity_main.xml में ListView जोड़ें
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
android:orientation="vertical"
android:padding="8dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
चरण 3: MainActivity.kt में स्क्रॉल लिस्टनर लागू करें
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private var scrollView: ScrollView? = null
private lateinit var listView: ListView
private var numbers = arrayOf(
"1", "2", "3", "4", "5", "6", "7", "8", "9", "X",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25"
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
// नोट: XML में ScrollView नहीं है, यह कोड में गलती है
// scrollView = findViewById(R.id.scrollView)
val adapter: ArrayAdapter<*> = ArrayAdapter(
this,
R.layout.support_simple_spinner_dropdown_item,
numbers
)
listView = findViewById(R.id.listView)
listView.adapter = adapter
listView.setOnScrollListener(object : AbsListView.OnScrollListener {
private var lastFirstVisibleItem = 0
override fun onScrollStateChanged(view: AbsListView, scrollState: Int) {}
override fun onScroll(
view: AbsListView,
firstVisibleItem: Int,
visibleItemCount: Int,
totalItemCount: Int
) {
if (lastFirstVisibleItem < firstVisibleItem) {
Toast.makeText(
applicationContext,
"Scrolling down the listView",
Toast.LENGTH_SHORT
).show()
}
if (lastFirstVisibleItem > firstVisibleItem) {
Toast.makeText(
applicationContext,
"Scrolling up the listView",
Toast.LENGTH_SHORT
).show()
}
lastFirstVisibleItem = firstVisibleItem
}
})
}
}
चरण 4: RecyclerView के लिए आधुनिक दृष्टिकोण
नोट: ListView लिगेसी है। नए प्रोजेक्ट्स के लिए RecyclerView उपयोग करें:
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy > 0) {
// स्क्रॉल डाउन
} else if (dy < 0) {
// स्क्रॉल अप
}
}
})
चरण 5: AndroidManifest.xml कॉन्फ़िगर करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<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>
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में किसी एक्टिविटी फ़ाइल को खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप चलने पर डिफ़ॉल्ट स्क्रीन देखें।
ऑप्टिमाइज़ेशन टिप: बार-बार Toast दिखाने से UI लैग हो सकता है। डिबाउंस या लॉगकैट का उपयोग करें:
private var lastToastTime = 0L
override fun onScroll(...) {
val now = System.currentTimeMillis()
if (now - lastToastTime > 1000) { // 1 सेकंड डिबाउंस
// Toast दिखाएं
lastToastTime = now
}
}