读取网上gson数据,展示到gridview上,然后上拉加载,下拉刷新

NetWorkUtil.class

package com.example.liangminghui20160425;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

public class NetWorkUtil {

    public static String loginCheck_Get_HttpClient(String url) {
        // TODO Auto-generated method stub
String result = "";

        HttpGet get = new HttpGet(url);//创建httpClient的get请求对象
        //设置请求参数
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 5*1000);
        HttpConnectionParams.setSoTimeout(params, 5*1000);

        HttpClient client = new DefaultHttpClient(params);//创建HttpClient对象

        try {
            HttpResponse res = client.execute(get);//执行请求,获得结果
            if(res.getStatusLine().getStatusCode() == 200){
                HttpEntity entity = res.getEntity();//获得相应结果,是一个HttpEntity对象
                result = EntityUtils.toString(entity, "utf-8");//转换为字符串
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

}

MainActivity.class:

package com.example.liangminghui20160425;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.widget.BaseAdapter;
import android.widget.GridView;

import com.example.adapter.Mybase;
import com.example.vo.Data;
import com.example.vo.Demo;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
//主页面
public class MainActivity extends Activity {

    private PullToRefreshGridView mPullRefreshListView;
    // 网址

    private String URL="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=20&pn=";
//    private String URL = "http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=10&rn=10";
   private Mybase adapter;
  //设置初始值
    int z = 10;
    int p = 10;
    private List<Data> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 找控件
        mPullRefreshListView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
        // 刚进入初次加载
        new GetDataTask().execute(URL + z);
        // 监听
        mPullRefreshListView
                .setOnRefreshListener(new OnRefreshListener2<GridView>() {
                    // 下拉刷新加载
                    @Override
                    public void onPullDownToRefresh(
                            PullToRefreshBase<GridView> refreshView) {
                        Log.e("TAG", "onPullDownToRefresh"); // Do work to
                        // 刷新时间
                        String label = DateUtils.formatDateTime(
                                getApplicationContext(),
                                System.currentTimeMillis(),
                                DateUtils.FORMAT_SHOW_TIME
                                        | DateUtils.FORMAT_SHOW_DATE
                                        | DateUtils.FORMAT_ABBREV_ALL);

                        // Update the LastUpdatedLabel
                        refreshView.getLoadingLayoutProxy()
                                .setLastUpdatedLabel(label);
                        // 页数
                        z = z - 1;
                        // AsyncTask异步交互加载数据
                        new GetDataTask().execute(URL + z);
                    }

                    // 上拉加载跟多
                    @Override
                    public void onPullUpToRefresh(
                            PullToRefreshBase<GridView> refreshView) {
                        Log.e("TAG", "onPullUpToRefresh"); // Do work to refresh

                        // 页数
                        p = p + 1;
                        // AsyncTask异步交互加载数据
                        new GetDataTask2().execute(URL + p);
                    }
                });
    }

    // 下拉刷新加载数据
    private class GetDataTask extends AsyncTask<String, Integer, String> {

        private String s;

        @Override
        protected String doInBackground(String... params) {

            String url = params[0];
            // 请求数据
            s = NetWorkUtil.loginCheck_Get_HttpClient(url);

            return s;
        }

        protected void onPostExecute(String result) {
            // 解析
            Gson gson = new Gson();

            Demo demo = gson.fromJson(result, Demo.class);
            List<Data> data =demo.getResult().getData();

            list = data;
            // 适配
            adapter = new Mybase(list, getApplicationContext());
            mPullRefreshListView.setAdapter(adapter);
            // 刷新适配器
            adapter.notifyDataSetChanged();
            // 关闭刷新下拉
            mPullRefreshListView.onRefreshComplete();

        }
    }

    // 上拉加载更多
    private class GetDataTask2 extends AsyncTask<String, Integer, String> {

        private String s;

        @Override
        protected String doInBackground(String... params) {

            String url = params[0];
            // 请求数据
            s = NetWorkUtil.loginCheck_Get_HttpClient(url);

            return s;
        }

        @Override
        protected void onPostExecute(String result) {
            // 解析
            Gson gson = new Gson();
            Demo demo = gson.fromJson(result, Demo.class);
            List<Data> data =demo.getResult().getData();

            list.addAll(data);
            // 刷新适配器
            adapter.notifyDataSetChanged();
            // Call onRefreshComplete when the list has been refreshed.
            // 关闭上拉加载
            mPullRefreshListView.onRefreshComplete();
        }
    }
}

Mybase.class

package com.example.adapter;

import java.util.List;

import com.example.liangminghui20160425.R;
import com.example.vo.Data;
import com.loopj.android.image.SmartImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

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

public class Mybase extends BaseAdapter{
    private List<Data> data;
    private Context context;

    public Mybase(List<Data> data, Context context) {
        super();
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Viehoder vh;
        if(convertView==null){
            vh=new Viehoder();
            convertView=LayoutInflater.from(context).inflate(R.layout.item, null);
            vh.image_view=(SmartImageView) convertView.findViewById(R.id.image_view);
            vh.text_view=(TextView) convertView.findViewById(R.id.text_view);
            vh.text_count=(TextView) convertView.findViewById(R.id.text_count);
            vh.text_time=(TextView) convertView.findViewById(R.id.text_time);
            convertView.setTag(vh);
        }else{
            vh=(Viehoder) convertView.getTag();

        }
        vh.text_view.setText(data.get(position).getTitle());
        vh.image_view.setImageUrl(data.get(position).getImg());
        vh.text_time.setText(data.get(position).getBytime());
        vh.text_count.setText(data.get(position).getReading());
        return convertView;
    }
class Viehoder{
    SmartImageView image_view;
    TextView text_view,text_time,text_count;

}
}

activity.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

   <com.handmark.pulltorefresh.library.PullToRefreshGridView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        ptr:ptrDrawable="@drawable/ic_launcher"
        ptr:ptrMode="both" />

</RelativeLayout>

item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<com.loopj.android.image.SmartImageView
    android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    />
<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <TextView
    android:id="@+id/text_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
    <TextView
    android:id="@+id/text_count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="20dp"
    />
</LinearLayout>
</LinearLayout>

总结:这是项目二的第三次周考题,主要考察的是gridview的上拉加载,下拉刷新,通过集成libary类,获得其自定义的PullToRefreshGridView,然后通过以下方法设置,

时间: 2024-10-14 21:07:38

读取网上gson数据,展示到gridview上,然后上拉加载,下拉刷新的相关文章

使用dragloader.js插件实现上拉加载/下拉刷新..

在写代码时候有个需求是,在触屏页面,为了加快页面加载速度,案件列表每页展示5条数据: 然后点击更多,展示下一页数据: 但是为了触屏更好的体验,改为往上滑动案件列表,加载下一页数据:就是要实现上拉加载/下拉刷新的效果: 我只用到了 上拉加载: 参考资料:http://blog.csdn.net/xb12369/article/details/39202711 下面是写的demo: html代码: <!DOCTYPE html> <html lang="en"> &

ListView上拉加载下拉刷新

主要用到了这个几个文件,MainActivity是界面的Activity,MyAdapter是ListView的自定义适配,MyListView是自定义带头部LIistView,如果只需要上拉加载就不需要:activity_main.xml是住界面,item.xml是ListView的子布局里面只有一个TextView,listview_footer.xml是listview的加载更多的底部布局,listview_header.xml是listview的头部布局. MainActivity.ja

XML解析及上拉加载下拉刷新

XML解析及上拉加载下拉刷新 1.XML格式 2.GData和XPath遍历 //配置XML库(配置完才能使用) //(1)添加头文件搜索路径 // Header Search Paths-> /usr/include/libxml2 //(2)添加二进制库 // Link library -> lixml2.dylib //(3)源文件添加编译选项 // -fno-objc-arc //(4)添加头文件 // #import "GDataXMLNode.h"*/ XPat

微信小程序上拉加载下拉刷新

微信小程序实现上拉加载下拉刷新 使用小程序默认提供方法. (1). 在xxx.json 中开启下拉刷新,需要设置backgroundColor,或者是backgroundTextStyle ,因为加载的动画可能会是白色背景,会看不清. { "usingComponents": { "annicate": "/components/annicate/index" }, "navigationBarTitleText": &quo

Mint-ui中loadmore(上拉加载下拉刷新)组件在ios中滑动会触发点击事件的解决方法

bug说明: Mint-ui中loadmore(上拉加载下拉刷新)组件 在 使用fastclick的情况下 ,在ios设备中滑动会触发点击事件: 解决方法: 我是按需引入,去项目中找到loadmore下的index.js,全部引入的要找mint下面mint-ui.common.js 路径如下:你的项目名/node_modules\mint-ui\lib\loadmore\index.js 搜索 handleTouchEnd ,记得写event进去 handleTouchEnd: function

移动端上拉加载下拉刷新

<template> <div class="wrapper" ref="wrapper"> <div class="content" > <div class="refresh" :class="{ativeRefresh:refresh}">刷新</div> <div class="ct-row" v-for=&quo

【PullToRefresh 系列一】 Android上拉加载下拉刷新控件详解

转载请注明:http://blog.csdn.net/duguang77/article/details/40921601 作者信息: PullToRefresh控件下载地址:https://github.com/chrisbanes/Android-PullToRefresh 作者:https://github.com/chrisbanes 百度网盘下载地址:http://pan.baidu.com/s/1o6umifw (一)导入方法: 1.下载后将extras,library,sample

xlistview上拉加载 下拉刷新

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >

zepto.js + iscroll.js上拉加载 下拉加载的 移动端 新闻列表页面

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="viewport" content="width=device-width,initial-scale=1.0

ios 上拉加载下拉刷新Dome

为练手写了一个小的上拉加载更多下拉刷新的小的Dome . 没有太多的技术含量,只是作为新手的启发用.是上一篇下拉加载的扩展.先看一下那个再看这个就容易很多. Dome下载:http://download.csdn.net/detail/u010123208/8062715 先来梳理一下: 上拉家在更多就是上拉之后在底部现实一个视图,用来提示用户上拉加载更多,如果用户上拉就出发事件,进行加载并在试图中给予提示,同时后台加载数据,将添加的数据加入数据数组,最后重新导入列表: 下拉刷新同样的操作,只不