Contoh SwipeRefreshLayout di Android

Dalam tutorial ini, kita akan membuat fungsi swipe-to-refresh di Android. Untuk tujuan ini, SwipeRefreshLayout widget harus digunakan.

Contoh SwipeRefreshLayout menambahkan metode OnRefreshListener dan menerapkan logika kode yang akan memuat refresh. Gesek vertikal menampilkan bilah kemajuan yang khas saat pengguna menggesek. Progress bar memanggil setRefreshing(true) ketika menunjukkan animasi kemajuan atau panggilan setRefreshing(false) untuk membatalkan.

Contoh SwipeRefreshLayout di Android

Dalam berkas activity_main.xml, menerapkan SwipeRefreshLayout widget.

File : activity_main.xml

<?xml version="1.0" encoding="utf-8"?>  
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/refreshLayout"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="example.android.com.swiperefreshlayout.MainActivity">  
 
    <RelativeLayout  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content">  
 
        <TextView  
            android:id="@+id/textView"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignParentTop="true"  
            android:layout_centerHorizontal="true"  
            android:layout_marginTop="229dp"  
            android:text="Hello World!"  
            android:textSize="18dp"  
            app:layout_constraintBottom_toBottomOf="parent"  
            app:layout_constraintLeft_toLeftOf="parent"  
            app:layout_constraintRight_toRightOf="parent"  
            app:layout_constraintTop_toTopOf="parent" />  
    </RelativeLayout>  
</android.support.v4.widget.SwipeRefreshLayout>  


Buat aktivitas MainActivity.java dan tambahkan kode berikut. Di kelas ini, kita memeriksa konektivitas jaringan onRefresh()

gesekan.

File : MainActivity.java

package example.android.com.swiperefreshlayout;  
 
import android.content.Context;  
import android.graphics.Color;  
import android.net.ConnectivityManager;  
import android.net.NetworkInfo;  
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 swipeRefreshLayout;  
    TextView textView;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        swipeRefreshLayout = findViewById(R.id.refreshLayout);  
        textView = findViewById(R.id.textView);  
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {  
            @Override  
            public void onRefresh() {  
                       swipeRefreshLayout.setRefreshing(false);  
                       //your code on swipe refresh  
                       //we are checking networking connectivity  
                        boolean connection=isNetworkAvailable();  
                        if(connection){  
                            textView.setText("internet connect");  
                            textView.setTextColor(Color.GREEN);  
                        }  
                        else{  
                            textView.setText("not connected");  
                            textView.setTextColor(Color.RED);  
                        }  
 
            }  
        });  
        swipeRefreshLayout.setColorSchemeColors(Color.YELLOW);  
    }  
    public boolean isNetworkAvailable(){  
 
        ConnectivityManager connectivityManager=(ConnectivityManager) this.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);  
        NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();  
        return networkInfo !=null;  
    }  
}  


Izin yang Diperlukan

Tambahkan izin penggunaan di file AndroidMenifest.xml. Izin yang diberikan di bawah ini digunakan untuk mengakses konektivitas jaringan.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  


Output :

SwipeRefreshLayout


Berlangganan update artikel terbaru via email:

0 Response to "Contoh SwipeRefreshLayout di Android"

Posting Komentar

Iklan Atas Artikel

Iklan Bawah Artikel