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

Android Pull-to-Refresh कैसे लागू करें?

Android Pull-to-Refresh (Swipe-to-Refresh) लागू करना

उदाहरण में जाने से पहले, हमें पता होना चाहिए कि Android में Pull to Refresh लेआउट क्या है। हम Android में Pull to Refresh को Swipe-to-Refresh कह सकते हैं। जब आप स्क्रीन को ऊपर से नीचे की ओर स्वाइप करते हैं तो यह setOnRefreshListener के आधार पर कुछ एक्शन करेगा।

यह उदाहरण दर्शाता है कि Android Pull to Refresh कैसे लागू करें।

चरण 1 - नया प्रोजेक्ट बनाएं

Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।

चरण 2 - activity_main.xml लेआउट

res/layout/activity_main.xml में निम्न कोड जोड़ें:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/swipeRefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:gravity="center"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text"
            android:textSize="30sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"/>
    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>

उपरोक्त कोड में हमने SwipeRefreshLayout को पैरेंट लेआउट के रूप में दिया है, जब उपयोगकर्ता लेआउट को स्वाइप करता है, तो यह चाइल्ड व्यू को रीफ्रेश कर सकता है।

चरण 3 - MainActivity.java

src/MainActivity.java में निम्न कोड जोड़ें:

package com.example.andy.myapplication;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    SwipeRefreshLayout swipeRefresh;
    static int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView = findViewById(R.id.text);
        swipeRefresh = findViewById(R.id.swipeRefresh);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                i++;
                textView.setText("Tutorialspoint.com " + i);
                swipeRefresh.setRefreshing(false);
            }
        });
    }
}

उपरोक्त कोड में हमने onRefreshListener दिया है, जब आप पैरेंट लेआउट को स्वाइप करते हैं तो यह RefreshListener से onRefresh() को कॉल करेगा, हम स्वाइप काउंट के साथ टेक्स्ट अपडेट कर रहे हैं:

swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        i++;
        textView.setText("Tutorialspoint.com " + i);
        swipeRefresh.setRefreshing(false);
    }
});

आइए अपने एप्लिकेशन को चलाने का प्रयास करें। मान लें कि आपने अपने वास्तविक Android मोबाइल डिवाइस को कंप्यूटर से जोड़ा है। Android Studio से ऐप चलाने के लिए, अपनी प्रोजेक्ट की एक्टिविटी फ़ाइलों में से एक खोलें और टूलबार से Run आइकन पर क्लिक करें। विकल्प के रूप में अपना मोबाइल डिवाइस चुनें और फिर अपने मोबाइल डिवाइस की जांच करें जो आपकी डिफ़ॉल्ट स्क्रीन प्रदर्शित करेगा।

Android Pull-to-Refresh कैसे लागू करें?

उपरोक्त परिणाम प्रारंभिक स्क्रीन दिखाता है, अब ऊपर से नीचे स्वाइप करें यह टेक्स्टव्यू को अपडेट करेगा:

Android Pull-to-Refresh कैसे लागू करें?


Android Pull-to-Refresh कैसे लागू करें?

  1. एंड्रॉइड में बॉटम शीट (BottomSheet) विजेट कैसे इम्प्लीमेंट करें? एंड्रॉइड में बॉटम शीट (BottomSheet) विजेट कैसे इम्प्लीमेंट करें?

    एंड्रॉइड में BottomSheetBehavior के साथ बॉटम शीट इम्प्लीमेंट करना Bottom Sheet मटीरियल डिज़ाइन कंपोनेंट है जो स्क्रीन के नीचे से स्लाइड होकर कंटेंट दिखाता है। दो प्रकार हैं: Modal Bottom Sheet — डायलॉग जैसा, बैकग्राउंड डिम, बाहर टैप करके बंद होता है। BottomSheetDialogFragment से बनता है। Persistent

  1. Android में कस्टम AlertDialog View कैसे इम्प्लीमेंट करें? Android में कस्टम AlertDialog View कैसे इम्प्लीमेंट करें?

    Android में कस्टम AlertDialog इम्प्लीमेंट करना यह ट्यूटोरियल दिखाता है कि कैसे कस्टम लेआउट के साथ AlertDialog बनाया जाता है, जिसमें कस्टम टाइटल, आइकन, मैसेज, और बटन स्टाइलिंग शामिल है। चरण 1: प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: मेन लेआउट में बटन res/layout/activity_main.

  1. Android में RecyclerView से एंडलेस/इनफिनिट स्क्रॉलिंग लिस्ट कैसे बनाएं? Android में RecyclerView से एंडलेस/इनफिनिट स्क्रॉलिंग लिस्ट कैसे बनाएं?

    परिचय यह ट्यूटोरियल दिखाता है कि RecyclerView में एंडलेस स्क्रॉलिंग (पेजिनेशन) कैसे इम्प्लीमेंट करें। जब उपयोगकर्ता सूची के अंत तक पहुंचता है, तो अगला पेज ऑटोमैटिकली लोड होता है। आवश्यक चरण चरण 1: नया प्रोजेक्ट बनाएं और डिपेंडेंसी जोड़ें Android Studio में File → New Project से नया प्रोजेक्ट बनाएं