ListView में स्क्रॉल डायरेक्शन डिटेक्ट करने का तरीका
यह उदाहरण दिखाता है कि AbsListView.OnScrollListener का उपयोग करके ListView में ऊपर और नीचे स्क्रॉल को कैसे पहचाना जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project से प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?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:orientation="vertical"
android:gravity="center"
android:padding="8dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
चरण 3: MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ListView listView;
String[] numbers = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, numbers);
listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
private int lastFirstVisibleItem = 0;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// स्क्रॉल स्टेट चेंज: SCROLL_STATE_IDLE, TOUCH_SCROLL, FLING
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (lastFirstVisibleItem < firstVisibleItem) {
Toast.makeText(getApplicationContext(), "नीचे स्क्रॉल हो रहा है", Toast.LENGTH_SHORT).show();
} else if (lastFirstVisibleItem > firstVisibleItem) {
Toast.makeText(getApplicationContext(), "ऊपर स्क्रॉल हो रहा है", Toast.LENGTH_SHORT).show();
}
lastFirstVisibleItem = firstVisibleItem;
}
});
}
}
चरण 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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रेफरेंस था जिसे हटा दिया गया है। - RecyclerView आधुनिक विकल्प है; इसमें
addOnScrollListenerउपयोग करें। - हर स्क्रॉल इवेंट पर Toast दिखाना UX के लिए खराब है; लॉगिंग या UI अपडेट बेहतर है।