Android活動刷新(SwipeRefreshLayout)含代碼

Android活動刷新(SwipeRefreshLayout)

在本教程中,我們將在Android中創建下拉刷新的功能。愛掏網 - it200.com為此,應使用SwipeRefreshLayout小部件。愛掏網 - it200.com

SwipeRefreshLayout的實例添加了OnRefreshListener方法,并實現了在刷新時加載的代碼邏輯。愛掏網 - it200.com當用戶滑動時,垂直滑動會顯示一個獨特的進度條。愛掏網 - it200.com進度條在顯示進度動畫時調用setRefreshing(true),或者調用setRefreshing(false)來取消。愛掏網 - it200.com

在activity_main.xml文件中實現SwipeRefreshLayout小部件。愛掏網 - it200.com

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.javatpoint.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>

創建一個活動 MainActivity.java 并添加以下代碼。愛掏網 - it200.com在這個類中,我們在刷新時檢查網絡連接。愛掏網 - it200.com

MainActivity.java

package example.javatpoint.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;
    }
}

所需權限

將以下權限添加到 AndroidMenifest.xml 文件中。愛掏網 - it200.com下面給出的權限用于訪問網絡連接。愛掏網 - it200.com

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

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 麻豆安全免费网址入口| 亚洲最大av网站在线观看| 久久精品国产69国产精品亚洲| 2018天天干夜夜操| 激情六月在线视频观看| 好硬啊进得太深了h动态图120秒| 国产一级不卡毛片| 中文字幕伊人久久网| 美女被免费视频网站a国产| 成年丰满熟妇午夜免费视频| 国产主播福利精品一区二区| 久久久久久久99精品免费| 色多多视频免费网站下载| 无码毛片视频一区二区本码 | 老师开嫩苞在线观看| 日本强不卡在线观看| 国产又粗又猛又大的视频| 久久亚洲精品无码VA大香大香| 黄色一级片在线播放| 日本口工h全彩漫画大全| 国产一区在线看| 一级特黄录像在线观看| 空白tk2一一视频丨vk| 大陆三级理论电影有哪些| 亚洲熟妇色xxxxx欧美老妇| 8x8x在线观看视频高清视频| 欧美黑人videos巨大18tee| 国产精品自在线拍国产手青青机版| 亚洲欧洲自拍拍偷午夜色无码| 毛片基地看看成人免费| 最新jizz欧美| 国产一级黄色片子| 一级性生活免费| 欧美高清一区二区三| 国产精品午夜无码av体验区| 亚洲中字慕日产2021| 青青青国产免费一夜七次郎| 影音先锋成人资源| 亚洲欧美黄色片| 狠狠色噜噜狠狠狠狠98| 日本一区二区三区在线视频观看免费|