pulltorefresh——刷新

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    xmlns:ptr="http://schemas.android.com/apk/res-auto"
    tools:context="zhanghaijiao.bawei.com.pulltorefresh_demo.MainActivity">

    <!--
    ptr:ptrDrawable="@drawable/default_ptr_flip":刷新时显示的图片
    ptr:ptrAnimationStyle="flip":刷新的图片以何种方式显示出来
    ptr:ptrHeaderBackground="#383838":刷新时头部的布局
    ptr:ptrHeaderTextColor="#FFFFFF":刷新时头部字体的颜色

    还需要添加以下的命名空间
   xmlns:ptr="http://schemas.android.com/apk/res-auto"
-->

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/plv"
        ptr:ptrDrawable="@drawable/default_ptr_flip"
        ptr:ptrAnimationStyle="flip"
        ptr:ptrHeaderBackground="#383838"
        ptr:ptrHeaderTextColor="#FFFFFF"
        ></com.handmark.pulltorefresh.library.PullToRefreshListView>

</RelativeLayout>


辅助界面:
package zhanghaijiao.bawei.com.pulltorefresh_demo.utls;

import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by jane on 2017/12/12.
 */

public class MyNetTask extends AsyncTask<String,Void,String> {

    //2.申明一个接口对象
    public  Icallbacks icallbacks;

    //3.定义一个有带构造方法
    public MyNetTask(Icallbacks icallbacks) {
        this.icallbacks = icallbacks;
    }

    @Override
    protected String doInBackground(String... strings) {
        StringBuilder builder=new StringBuilder();
        //1.根据url创建URL
        try {
            URL url=new URL(strings[0]);
            //2.打开连接
            HttpURLConnection conn =(HttpURLConnection) url.openConnection();
            //3.设置
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);

            //4.判断结果码
            if(conn.getResponseCode()==200){
                //5.获取数据
                InputStream inputStream = conn.getInputStream();

                BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
                String str;
                while ((str=reader.readLine())!=null){
                    builder.append(str);
                }

            }


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Log.d("zzz",builder.toString());
        return builder.toString();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        //4.调用方法
        icallbacks.getJsonData(s);


    }

    //1.定义接口
    public interface Icallbacks{
        public void getJsonData(String jsonStr);
    }


}


主界面:
package zhanghaijiao.bawei.com.pulltorefresh_demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import zhanghaijiao.bawei.com.pulltorefresh_demo.utls.MyNetTask;

public class MainActivity extends AppCompatActivity {

    private PullToRefreshListView pullToRefreshListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pullToRefreshListView = findViewById(R.id.plv);

        initPlv();
        //1.开关  2.监听事件
        MyNetTask myNetTask=new MyNetTask(new MyNetTask.Icallbacks() {
            @Override
            public void getJsonData(String jsonStr) {

            }
        });
        myNetTask.execute("https://api.tianapi.com/wxnew/?key=48a7d7193e11bd2dd4a683b6e2f90a4f&num=10&page=1");






    }

    private void initPlv() {
        //1.设置模式  BOTH:上下拉刷新  PULL_FROM_START:支持刷新   PULL_FROM_END:支持加载更多
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        //2.设置头尾布局的文字
        ILoadingLayout headerLayout = pullToRefreshListView.getLoadingLayoutProxy(true, false);
        headerLayout.setRefreshingLabel("正在刷新");//正在刷新
        headerLayout.setRefreshingLabel("放开刷新");//下拉到一定的距离,显示的文字
        headerLayout.setPullLabel("下拉刷新");//刚下拉时,显示的文字

        ILoadingLayout footerLayout = pullToRefreshListView.getLoadingLayoutProxy(false, true);
        footerLayout.setRefreshingLabel("正在加载");//正在刷新
        footerLayout.setRefreshingLabel("放开加载");//下拉到一定的距离,显示的文字
        footerLayout.setPullLabel("上拉加载");//刚下拉时,显示的文字

        //3.设置回调监听
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                //刷新时回调
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                //加载更多的回调

            }
        });









    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值