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%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。