PullToRefreshGridView刷新加载

时间:2021-12-12 12:58:13

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

package com.example.day_00000;

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.widget.GridView;

import com.baidu.net.Network;
import com.baidu.vo.Mydata;
import com.baidu.vo.Super;
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 implements
        OnRefreshListener2<GridView> {

private PullToRefreshGridView gv;

private ArrayList<Mydata> data;
    List<Mydata> list = new ArrayList<Mydata>();

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gv = (PullToRefreshGridView) findViewById(R.id.lv);
        gv.setOnRefreshListener(this);
        new Asy().execute();
    }

private Base b;

class Asy extends AsyncTask<String, Integer, String> {

private String ss1;

protected String doInBackground(String... params) {

ss1 = Network
                    .getjson("http://m.yunifang.com/yunifang/mobile/goods/getall?random=9949&encode=6c2154232994e736ad461e3caa68ca7");
            return ss1;
        }

protected void onPostExecute(String result) {

Gson g = new Gson();
            Super s = g.fromJson(result, Super.class);
            ArrayList<Mydata> data2 = s.getData();
            list = data2;
            b = new Base(list, MainActivity.this);
            gv.setAdapter(b);
            gv.onRefreshComplete();

}
    }

class Asynt extends AsyncTask<String, Integer, String> {

private String ss1;

protected String doInBackground(String... params) {

ss1 = Network
                    .getjson("http://m.yunifang.com/yunifang/mobile/goods/getall?random=9949&encode=6c2154232994e736ad461e3caa68ca7");
            return ss1;
        }

protected void onPostExecute(String result) {

Gson g = new Gson();
            Super s = g.fromJson(result, Super.class);
            ArrayList<Mydata> data2 = s.getData();
            list.addAll(data2);
            b.notifyDataSetChanged();
            gv.onRefreshComplete();
        }
    }

public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
        new Asy().execute();
        initIndicator();
    }

int p = 2;

public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
        initIndicator();
        new Asynt().execute();
        p++;
    }

private void initIndicator() {
        String label = DateUtils.formatDateTime(getApplicationContext(),
                System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
                        | DateUtils.FORMAT_SHOW_DATE
                        | DateUtils.FORMAT_ABBREV_ALL);
        gv.getLoadingLayoutProxy().setLastUpdatedLabel(label);
    }
}