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 आइकन पर क्लिक करें। विकल्प के रूप में अपना मोबाइल डिवाइस चुनें और फिर अपने मोबाइल डिवाइस की जांच करें जो आपकी डिफ़ॉल्ट स्क्रीन प्रदर्शित करेगा।

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

