android studio 环境下使用PullToRefreshScrollView

本文介绍如何在Android项目中集成PullToRefreshScrollView库,并演示了如何在实际项目中应用该库实现下拉刷新功能,包括网络状态检查、异步任务处理等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.导入和修改相关library

https://github.com/chrisbanes/Android-PullToRefresh下载好Android-PullToRefresh-master文件,在as中选择file  -->  new  -->  import module,文件路径选择Android-PullToRefresh-master中的library文件,然后next finish即可。






这步操作完成后as会报错,暂时不用管,选择file  -->  project structure -->  app -->  Dependencies ,点击“+”选择 Module Dependency ,  windows环境下在界面右侧,选择“:library”后确认添加。




最后更改library目录下build.gradle 文件  compileSdkVersion 和 buildToolsVersion,只要改成与src目录下的build.gradle文件中compileSdkVersion 和 buildToolsVersion相同的值即可。



同步之后as就不会报错,若可以使用PullToRefreshScrollView相关类即导入成功。如果sdk使用的是6.0以上版本,编译时会报错,只要在源码中相关地方的FloatMath类型改为Math重新编译即可。


2.PullToRefreshScrollView的应用

demo中用到了网络状态,所以现在AndroidManifest中申请ACCESS_NETWORK_STATE的权限。

layout布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.testforpulltorefreshscrollview.MainActivity">

    <com.handmark.pulltorefresh.library.PullToRefreshScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/pull_to_refresh_scrollview">
        
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="text for test"/>
    </com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</android.support.constraint.ConstraintLayout>


MainActivity文件:

package com.example.administrator.testforpulltorefreshscrollview;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;

public class MainActivity extends AppCompatActivity {
    private PullToRefreshScrollView scrollView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scrollView = (PullToRefreshScrollView) findViewById(R.id.pull_to_refresh_scrollview);
        scrollView.setMode(PullToRefreshBase.Mode.PULL_DOWN_TO_REFRESH);
        scrollView.setReleaseLabel("松开刷新", PullToRefreshBase.Mode.PULL_FROM_START);
        scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                ConnectivityManager manager = (ConnectivityManager) MainActivity.this.getSystemService(CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = manager.getActiveNetworkInfo();
                if(networkInfo != null && networkInfo.isAvailable()){
                    new GetDataTask().execute();
                }else {
                    Toast.makeText(MainActivity.this,"connectivity is down",Toast.LENGTH_SHORT).show();
                    scrollView.onRefreshComplete();
                }
            }
        });
    }

    class GetDataTask extends AsyncTask<Void,Void,String[]>{
        @Override
        protected String[] doInBackground(Void... voids) {
            try {
                Thread.sleep(2000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String[] strings) {
            scrollView.onRefreshComplete();
            super.onPostExecute(strings);
        }
    }
}













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值