Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में ScrollView के अंत का पता कैसे लगाएं?

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 में स्क्रॉल करके शीर्ष/अंत पर टोस्ट देखें।

Android में ScrollView के अंत का पता कैसे लगाएं?

  1. एंड्रॉइड में डिवाइस फोन है या टैबलेट कैसे पहचानें एंड्रॉइड में डिवाइस फोन है या टैबलेट कैसे पहचानें

    TelephonyManager और स्क्रीन आकार के माध्यम से डिवाइस प्रकार निर्धारित करना एंड्रॉइड में फोन और टैबलेट के बीच अंतर करने के कई तरीके हैं। सबसे विश्वसनीय विधि TelephonyManager.getPhoneType() है जो वॉयस कॉल क्षमता जांचता है, लेकिन स्क्रीन आकार और UI मोड भी उपयोगी संकेत देते हैं। चरण 1: नया प्रोजेक्ट बन

  1. Android में ScrollView को प्रोग्रामेटिकली कैसे डिसेबल करें? Android में ScrollView को प्रोग्रामेटिकली कैसे डिसेबल करें?

    परिचय यह उदाहरण दर्शाता है कि Android में ScrollView को प्रोग्रामेटिक रूप से कैसे डिसेबल किया जाए। हम OnTouchListener का उपयोग करके टच इवेंट को इंटरसेप्ट करेंगे। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project के माध्यम से नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML में ScrollView res/l

  1. Android में 5 सेकंड यूजर इनएक्टिविटी कैसे डिटेक्ट करें? Android में 5 सेकंड यूजर इनएक्टिविटी कैसे डिटेक्ट करें?

    यह उदाहरण दर्शाता है कि Handler और onUserInteraction() का उपयोग करके Android में यूजर इनएक्टिविटी (कोई टच/की इवेंट नहीं) को कैसे डिटेक्ट किया जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project। चरण 2: लेआउट XML res/layout/activity_main.xml: <?xml version=1.0 encoding=utf-