ScrollView स्क्रॉल अंत पहचान
यह उदाहरण दर्शाता है कि ViewTreeObserver.OnScrollChangedListener का उपयोग करके ScrollView के शीर्ष या अंत तक पहुंचने का पता कैसे लगाएं।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट में ScrollView और WebView (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"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</RelativeLayout>
चरण 3: strings.xml में लंबा HTML टेक्स्ट जोड़ें
<resources>
<string name="app_name">ScrollView Demo</string>
<string name="sample_data"><![CDATA[
<html><body><p align="justify" class="small">
लंबा टेक्स्ट यहाँ... (कई पैराग्राफ)
</p></body></html>
]]></string>
</resources>
चरण 4: MainActivity.java में स्क्रॉल लिस्टनर
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.webkit.WebView;
import android.widget.ScrollView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener, ViewTreeObserver.OnScrollChangedListener {
ScrollView scrollView;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = findViewById(R.id.scrollView);
webView = findViewById(R.id.webView);
scrollView.setOnTouchListener(this);
scrollView.getViewTreeObserver().addOnScrollChangedListener(this);
webView.loadData(getString(R.string.sample_data), "text/html", null);
}
@Override
public void onScrollChanged() {
View view = scrollView.getChildAt(scrollView.getChildCount() - 1);
int scrollY = scrollView.getScrollY();
int bottom = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY());
if (bottom == 0) {
Toast.makeText(this, "ScrollView का अंत पहुँच गया", Toast.LENGTH_SHORT).show();
}
if (scrollY <= 0) {
Toast.makeText(this, "ScrollView का शीर्ष पहुँच गया", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
}
चरण 5: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.scrollend">
<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>
ऐप चलाएं
डिवाइस कनेक्ट करें, Run दबाएं, और WebView में स्क्रॉल करके शीर्ष/अंत पर टोस्ट देखें।
