PullToRefreshGridView实现上拉加载和下拉刷新,使用Volley框架解析数据

1、加载布局

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.my.Main2Activity">

   <com.handmark.pulltorefresh.library.PullToRefreshGridView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:numColumns="2"
       android:stretchMode="columnWidth"
       android:columnWidth="100dp"
       android:horizontalSpacing="5dp"
       android:verticalSpacing="5dp"
       android:cacheColorHint="#00000000"
       android:listSelector="#00000000"
       android:scrollbars="none"
       android:id="@+id/gv"
       android:background="#cac9c9">
   com.handmark.pulltorefresh.library.PullToRefreshGridView>
  
LinearLayout>
2.实体类
 
  
package com.example.administrator.my;

/**
 * Created by Administrator on 2017/1/19.
 */
public class News {
    private String name;
    private String introduce;
    private String distance;
    private String pic;
    public News(){

    }
    public News(String name, String introduce, String distance, String pic) {
        this.name = name;
        this.introduce = introduce;
        this.distance = distance;
        this.pic = pic;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIntroduce() {
        return introduce;
    }

    public void setIntroduce(String introduce) {
        this.introduce = introduce;
    }

    public String getDistance() {
        return distance;
    }

    public void setDistance(String distance) {
        this.distance = distance;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }
}
3.GridView适配器
 
  
package com.example.administrator.my;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;


import java.util.List;

/**
 * Created by Administrator on 2017/1/19.
 */
public class NewsAdapter extends BaseAdapter{
   private List newsList;
    private Context context;

    public NewsAdapter(List newsList, Context context) {
        this.newsList = newsList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return newsList.size();
    }

    @Override
    public Object getItem(int i) {
        return newsList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
         ViewHolder vh;
        if(view==null){
            vh=new ViewHolder();
            view=LayoutInflater.from(context).inflate(R.layout.gridview,null);
            vh.img= (ImageView) view.findViewById(R.id.img);
            vh.name= (TextView) view.findViewById(R.id.name);
            vh.distance= (TextView) view.findViewById(R.id.distance);
            vh.introduce= (TextView) view.findViewById(R.id.introduce);
            view.setTag(vh);

        }else{
          vh= (ViewHolder) view.getTag();
        }
        News news=newsList.get(i);
        Glide.with(context).load(news.getPic()).into(vh.img);
        vh.name.setText(news.getName());
        vh.distance.setText(news.getDistance());
        vh.introduce.setText(news.getIntroduce());
        return view;
    }
    public class ViewHolder{
        ImageView img;
        TextView name;
        TextView distance,introduce;
    }
}
4、我的Demo工程所有代码
package com.example.administrator.my;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;


public class Main2Activity extends AppCompatActivity {
    private List newsList;
    private NewsAdapter newsAdapter;
    private RequestQueue queue;
    private PullToRefreshGridView PullToRefresh;
    private String url;
    private int i;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        PullToRefresh= (PullToRefreshGridView) findViewById(R.id.gv);
        queue= Volley.newRequestQueue(this);
        newsList=new ArrayList();
        newsAdapter=new NewsAdapter(newsList,this);
        PullToRefresh.setAdapter(newsAdapter);
        i=1;
        url="http://58.56.17.148:93/api/org/pages?Current="+i;
        getNews(url);
        PullToRefresh.setOnRefreshListener( new PullListenner());
        // 设置PullToRefreshListView的模式
        //上拉加载和下拉刷新都能支持
        PullToRefresh.setMode(PullToRefreshBase.Mode.BOTH);
        // 设置PullRefreshListView上提加载时的加载提示
        PullToRefresh.getLoadingLayoutProxy(false, true).setPullLabel("上拉加载...");
        PullToRefresh.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载...");
        PullToRefresh.getLoadingLayoutProxy(false, true).setReleaseLabel("松开加载更多...");

        // 设置PullRefreshListView下拉加载时的加载提示
        PullToRefresh.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新...");
        PullToRefresh.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在加载...");
        PullToRefresh.getLoadingLayoutProxy(true, false).setReleaseLabel("松开加载更多...");

    }
    //使用Volley框架解析数据
    private void getNews(String url){
        StringGetRequest sgr=new StringGetRequest(Request.Method.GET, url, new Response.Listener() {
            @Override
            public void onResponse(String s) {
                Log.d("---",s);
                try {
                    JSONObject object=new JSONObject(s);
                    JSONObject object1=object.getJSONObject("Datas");
                    JSONArray ja=object1.getJSONArray("Orgs");
                    for(int i=0;inew News();
                        news.setName(object2.getString("Name"));
                        news.setDistance(object2.getString("Distance"));
                        news.setIntroduce(object2.getString("Introduce"));
                        news.setPic("http://58.56.17.148:93"+object2.getString("Pic"));
                        Log.d("---",news.getDistance());
                        newsList.add(news);
                    }
                    newsAdapter.notifyDataSetChanged();
                    PullToRefresh.onRefreshComplete();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                PullToRefresh.onRefreshComplete();
            }
        });
        queue.add(sgr);
    }

    //下拉上拉刷新 监听(内部类)
    public class PullListenner implements PullToRefreshBase.OnRefreshListener2{
    //下拉刷新(清空之前所有,只显示第一页)
        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            newsList.clear();
            i=1;
           url="http://58.56.17.148:93/api/org/pages?Current="+i;
            getNews(url);
        }
        //上拉加载更多
        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            i++;
            url="http://58.56.17.148:93/api/org/pages?Current="+i;
            getNews(url);
        }
    }


}



你可能感兴趣的:(Android数据存储,Android网络框架基础,Android基础)