您的位置:首页 > 移动开发 > Android开发

android使用PulltoRefushGridview实现下拉刷新

2016-04-26 10:29 501 查看
主界面
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: