本文概述
- 滑动以刷新Android活动示例
SwipeRefreshLayout的实例添加了OnRefreshListener方法,并实现了将在刷新时加载的代码逻辑。当用户滑动时,垂直滑动会显示一个独特的进度条。进度条显示进度动画时调用setRefreshing(true)或调用setRefreshing(false)取消。
滑动以刷新Android活动示例在activity_main.xml文件中,实现SwipeRefreshLayout小部件。
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.srcmini.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并添加以下代码。在此类中,我们将检查滑动的onRefresh()网络连接。
MainActivity.java
package example.srcmini.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;
}
}
所需权限
【滑动刷新android activity】在AndroidMenifest.xml文件中添加uses-permission。下面给出的权限用于访问网络连接。
<
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
文章图片
文章图片
推荐阅读
- android volley库注册登录注销
- Android 使用FACE++架构包实现人脸识别
- Android Hook技术
- Android笔记
- android 搭建https Server
- Android中的Glide加载图片
- Android补间动画笔记
- Android 使用Fragment界面向下跳转并一级级返回
- 解决某些Android Permission denied