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>
相关文章推荐
- Android事件分发机制完全解析,带你从源码的角度彻底理解(上)
- Android学习技巧
- Android Activity和Fragment的转场动画
- Error:Cause: org/gradle/api/publication/maven/internal/DefaultMavenFactory Android
- android入门四 数据持久化
- android报错汇总
- Android系统服务源码位置解析
- 与Android应用程序相关的文件目录都有哪些?
- android 动态添加视图与静态添加视图
- android反编译工具
- android轻松管理安卓应用中的log日志 发布应用时log日志全部去掉的方法
- Android自定义标尺控件RulerView
- android studio 中导入 已经建好的aidl
- Android Native 代码开发学习笔记
- 笔记:Android问题
- Android中应用程序如何获得系统签名权限
- Adapter的优化使用
- Android布局优化之include、merge、ViewStub的使用
- 笔记:Android存储数据
- Android NavigationView item点击