Android活動(dòng)刷新(SwipeRefreshLayout)
在本教程中,我們將在Android中創(chuàng)建下拉刷新的功能。愛掏網(wǎng) - it200.com為此,應(yīng)使用SwipeRefreshLayout小部件。愛掏網(wǎng) - it200.com
SwipeRefreshLayout的實(shí)例添加了OnRefreshListener方法,并實(shí)現(xiàn)了在刷新時(shí)加載的代碼邏輯。愛掏網(wǎng) - it200.com當(dāng)用戶滑動(dòng)時(shí),垂直滑動(dòng)會(huì)顯示一個(gè)獨(dú)特的進(jìn)度條。愛掏網(wǎng) - it200.com進(jìn)度條在顯示進(jìn)度動(dòng)畫時(shí)調(diào)用setRefreshing(true),或者調(diào)用setRefreshing(false)來取消。愛掏網(wǎng) - it200.com
在activity_main.xml文件中實(shí)現(xiàn)SwipeRefreshLayout小部件。愛掏網(wǎng) - 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>
創(chuàng)建一個(gè)活動(dòng) MainActivity.java 并添加以下代碼。愛掏網(wǎng) - it200.com在這個(gè)類中,我們?cè)谒⑿聲r(shí)檢查網(wǎng)絡(luò)連接。愛掏網(wǎng) - 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;
}
}
所需權(quán)限
將以下權(quán)限添加到 AndroidMenifest.xml 文件中。愛掏網(wǎng) - it200.com下面給出的權(quán)限用于訪問網(wǎng)絡(luò)連接。愛掏網(wǎng) - it200.com
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。