android使用PulltoRefushGridview實現下拉刷新

主界面
package com.bawei.wuxiaopeng20160425;

import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.GridView;
import android.widget.Toast;

import com.bawei.adapter.MyAdapter;
import com.bawei.vo.ObjectDada;
import com.bawei.vo.User;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class MainActivity extends Activity {

    private PullToRefreshGridView mGridView;

    private List<User> list_user;
    private MyAdapter adapter;      
    int index=0;

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

        mGridView=(PullToRefreshGridView) findViewById(R.id.mygridview);
        //定義方法
        gedata();
        //定義刷新
        mGridView.setOnRefreshListener(new OnRefreshListener<GridView>() {

            @Override
            public void onRefresh(PullToRefreshBase<GridView> refreshView) {
                String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
                DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);//3.刷新界面處理代理,顯示新的時間
                gedata();//調用聯網方法
            }
        });

        //5.設置上拉加載處理
        mGridView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
        @Override
        public void onLastItemVisible() {
           gedata();//調用聯網方法
           Toast.makeText(MainActivity.this, "正在加載", 1).show();
        }
     });
    }
    private void gedata() {
        // TODO Auto-generated method stu
        HttpUtils httpUtils=new HttpUtils();
        String url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=20&rn=20&pn="+index+"";
        httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                // TODO Auto-generated method stub
                String json=arg0.result;
                Gson gson=new Gson();
                ObjectDada objectDada=gson.fromJson(json, ObjectDada.class);
                list_user=objectDada.getResult().getData();

                adapter=new MyAdapter(MainActivity.this,list_user);
                mGridView.setAdapter(adapter);

                adapter.notifyDataSetChanged();
                index= index+1;
                //Call onRefreshComplete when the list has been refreshed.
                mGridView.onRefreshComplete();
            }
        });


    }
}
xml文件
<LinearLayout 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"
    android:orientation="vertical"
    >

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="40dp">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="暢讀書城" />

    </RelativeLayout>


     <com.handmark.pulltorefresh.library.PullToRefreshGridView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/mygridview"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:numColumns="auto_fit"
        android:verticalSpacing="1dp"
        android:horizontalSpacing="1dp"
        android:columnWidth="100dp"
        android:stretchMode="columnWidth"
        android:gravity="fill"
        ptr:ptrMode="both"
       />

</LinearLayout>
配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bawei.wuxiaopeng20160425"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章