mpullrefreshlistviewGridView怎么实现自动刷新的问题

31613人阅读
PullToRefresh是一套实现非常好的下拉刷新库,它支持:
ExpandableListView
ScrollView
HorizontalScrollView
等多种常用的需要刷新的View类型,而且使用起来也十分方便。
(下载地址:)
PullToRefresh基本用法:
1、在布局文件中添加PullToRefresh控件,比如PullToRefreshListView;
2、在Activity中,设置监听器OnRefreshListener以响应用户下拉操作;
3、在监听器的onRefresh()方法中执行数据刷新操作,可以通过AsyncTask来实现;
4、在AsyncTask中获取到数据后,记得调用onRefreshComplete()方法通知PullToRefresh控件数据已获取完毕,可以结束刷新操作。
实例:PullToRefreshDemo
运行效果:
代码清单:
布局文件:activity_main.xml
&RelativeLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:paddingBottom=&@dimen/activity_vertical_margin&
android:paddingLeft=&@dimen/activity_horizontal_margin&
android:paddingRight=&@dimen/activity_horizontal_margin&
android:paddingTop=&@dimen/activity_vertical_margin&
tools:context=&.MainActivity& &
&com.handmark.pulltorefresh.library.PullToRefreshListView
android:id=&@+id/pull_to_refresh_listview&
android:layout_height=&fill_parent&
android:layout_width=&fill_parent& /&
&/RelativeLayout&
Java源代码文件:MainActivity.java
package com.rainsong.
import java.util.A
import java.util.LinkedL
import android.app.A
import android.os.AsyncT
import android.os.B
import android.text.format.DateU
import android.view.M
import android.widget.ArrayA
import android.widget.ListV
import com.handmark.pulltorefresh.library.PullToRefreshB
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshL
import com.handmark.pulltorefresh.library.PullToRefreshListV
public class MainActivity extends Activity {
private PullToRefreshListView mPullToRefreshListV
private LinkedList&String& mListI
private ArrayAdapter&String& mA
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set a listener to be invoked when the list should be refreshed.
mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);
mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener&ListView&() {
public void onRefresh(PullToRefreshBase&ListView& refreshView) {
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);
// Do work to refresh the list here.
new GetDataTask().execute();
ListView actualListView = mPullToRefreshListView.getRefreshableView();
mListItems = new LinkedList&String&();
mListItems.addAll(Arrays.asList(mStrings));
mAdapter = new ArrayAdapter&String&(this, android.R.layout.simple_list_item_1, mListItems);
actualListView.setAdapter(mAdapter);
private class GetDataTask extends AsyncTask&Void, Void, String[]& {
protected String[] doInBackground(Void... params) {
// Simulates a background job.
Thread.sleep(4000);
} catch (InterruptedException e) {
protected void onPostExecute(String[] result) {
mListItems.addFirst(&Added after refresh...&);
mAdapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
mPullToRefreshListView.onRefreshComplete();
super.onPostExecute(result);
public boolean onCreateOptionsMenu(Menu menu) {
// I this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
private String[] mStrings = { &John&, &Michelle&, &Amy&, &Kim&, &Mary&,
&David&, &Sunny&, &James&, &Maria&, &Michael&, &Sarah&, &Robert&,
&Lily&, &William&, &Jessica&, &Paul&, &Crystal&, &Peter&,
&Jennifer&, &George&, &Rachel&, &Thomas&, &Lisa&, &Daniel&, &Elizabeth&,
&Kevin& };
实例源代码下载&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:406337次
积分:4331
积分:4331
排名:第5026名
原创:93篇
评论:131条
(2)(4)(9)(9)(5)(5)(5)(7)(5)(11)(3)(5)(1)(5)(4)(1)(1)(1)(1)(5)(1)(1)(3)(7)(2)(1)
文章:24篇
阅读:86656package com.baway.
import java.util.L
import com.baway.adapter.MyBaseAimport com.baway.utils.NetWimport com.baway.vo.Mimport com.baway.vo.Supper1;
import com.google.gson.Gimport com.handmark.pulltorefresh.library.PullToRefreshBimport com.handmark.pulltorefresh.library.PullToRefreshGridVimport com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import android.os.AsyncTimport android.os.Bimport android.os.Himport android.app.Aimport android.content.Iimport android.text.format.DateUimport android.util.Limport android.view.Mimport android.view.Vimport android.widget.AdapterVimport android.widget.AdapterView.OnItemClickLimport android.widget.GridV
public class MainActivity extends Activity {private String url="/yunifang/mobile/goods/getall?random=9949&encole=6ce7d36ad461e3caa68ca7";private PullToRefreshGridView mPullRefreshGridV
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
new Myasnktask().execute(url);
mPullRefreshGridView
.setOnRefreshListener(new OnRefreshListener2&GridView&()
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);
new Myasnktask().execute(url);
public void onPullUpToRefresh(
PullToRefreshBase&GridView& refreshView)
Log.e("TAG", "onPullUpToRefresh"); // Do work to refresh
// the list here.
new Myasnktask().execute(url);
class Myasnktask extends AsyncTask&String, Integer, String&{
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String ss=params[0];
String s=NetWork.httpclient_get(ss);
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Gson gson=new Gson();
Supper1 supper1=gson.fromJson(result, Supper1.class);
final List&Mydata& list=supper1.getData();
mPullRefreshGridView.setAdapter(new MyBaseAdapter(list, getApplicationContext()));
mPullRefreshGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView&?& parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,NewActivity.class);
intent.putExtra("id", list.get(position).getId());
startActivity(intent);
mPullRefreshGridView.onRefreshComplete();
super.onPostExecute(result);
xml里面的布局文件;
&RelativeLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" &
&com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="/apk/res-auto"
android:id="@+id/pull_refresh_grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="100dp"
android:gravity="center_horizontal"
android:horizontalSpacing="1dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp"
ptr:ptrDrawable="@drawable/ic_launcher"
ptr:ptrMode="both" /&
&/RelativeLayout&
阅读(...) 评论()该开源项目地址:
3.往上滑动隐藏头部,往下滑动显示头部
先看效果图:
这里要说明一下的是:
a、头部的隐藏与显示是一个动画效果。
b、头部的隐藏与显示对PullToRefreshGridView的布局不会产生任何影响,主要是往上隐藏的时候PullToRefreshGridView不会突然往上移
& & 往下显示的时候PullToRefreshGridView不会突然往下移,不会有这些不连贯或者卡顿的感觉。
c、是否隐藏与显示是对比滑动过程中两次firstVisiblePosition的大小,也就是判断是上滑还是下滑。
核心代码:
a、布局部分:
&?xml version="1.0" encoding="utf-8"?&
&RelativeLayout xmlns:android="/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray"
android:orientation="vertical" &
&!--PullToRefreshGridView--&
&include layout="@layout/pull_gridview_refresh" /&
&!--头部--&
&include layout="@layout/pull_gridview_refresh_head" /&
&/RelativeLayout&
注意上面提到的头部不是mPullToRefreshGridView的头部。
mPullToRefreshGridView必须设置一个头部,怎么设置头部,请参考,mPullToRefreshGridView的头部的高度必须与布局里面的头部高度一致,可以在布局
中设定,也可以通过代码动态设定。
b、mPullToRefreshGridView设置滑动监听
mPullToRefreshGridView.setOnScrollListener(new OnScrollListener()
public void onScrollStateChanged(AbsListView view , int scrollState)
// TODO Auto-generated method stub
if (OnScrollListener.SCROLL_STATE_IDLE == scrollState)
if (mAdapter.type == Type.LINE_ONE)
firstVisiblePosition = view.getFirstVisiblePosition() + 1;
firstVisiblePosition = view.getFirstVisiblePosition() + 2;
public void onScroll(AbsListView view , int firstVisibleItem , int visibleItemCount , int totalItemCount)
// TODO Auto-generated method stub
if (ll_animation == null || toUp == null)
if (mAdapter.type == Type.LINE_ONE)
temp = view.getFirstVisiblePosition() + 1;
temp = view.getFirstVisiblePosition() + 2;
if (mAdapter.type == Type.LINE_ONE)
if (temp == 1 && !ll_animation.isShown() && !isDownStarted)
ll_animation.startAnimation(toDown);
if (temp == 2 && !ll_animation.isShown() && !isDownStarted)
ll_animation.startAnimation(toDown);
if ((temp & firstVisiblePosition) && firstVisibleItem != 0)//向上滑
if (!isUpStarted && ll_animation.isShown())
ll_animation.startAnimation(toUp);
ll_animation.setVisibility(View.GONE);
else if (temp & firstVisiblePosition)
if (!isDownStarted && !ll_animation.isShown())
ll_animation.startAnimation(toDown);
ll_animation.setVisibility(View.VISIBLE);
firstVisiblePosition =
c、动画部分
&?xml version="1.0" encoding="utf-8"?&
&translate xmlns:android="/apk/res/android"
android:duration="500"
android:fromYDelta="0%p"
android:toYDelta="-100%p" /&
&?xml version="1.0" encoding="utf-8"?&
&translate xmlns:android="/apk/res/android"
android:duration="500"
android:fromYDelta="-100%p"
android:toYDelta="0%p" /&
private void initAnimation()
// TODO Auto-generated method stub
toUp = AnimationUtils.loadAnimation(mActivity, R.anim.searchresult_toup_animation);
toDown = AnimationUtils.loadAnimation(mActivity, R.anim.searchresult_todown_animation);
toUp.setAnimationListener(new AnimationListener()
public void onAnimationStart(Animation animation)
// TODO Auto-generated method stub
isUpStarted =
public void onAnimationRepeat(Animation animation)
// TODO Auto-generated method stub
public void onAnimationEnd(Animation animation)
// TODO Auto-generated method stub
ll_animation.setVisibility(View.GONE);
isUpStarted =
toDown.setAnimationListener(new AnimationListener()
public void onAnimationStart(Animation animation)
// TODO Auto-generated method stub
isDownStarted =
public void onAnimationRepeat(Animation animation)
// TODO Auto-generated method stub
public void onAnimationEnd(Animation animation)
// TODO Auto-generated method stub
ll_animation.setVisibility(View.VISIBLE);
isDownStarted =
ps:里面有些变量需参考前一章节
阅读(...) 评论()pulltorefresh(一)使用介绍,实现下拉刷新上拉加载更多 - 推酷
pulltorefresh(一)使用介绍,实现下拉刷新上拉加载更多
在Android开发中,上拉刷新,下拉加载在很多项目中被需求着,这个功能具体怎么实现呢,很简单,用一个第三方插件chrisbanes写的android-PullToRefresh库即可了,或者谷歌官方推出的SwipeRefreshLayout。个人觉得PullToRefresh比较好用,并且可扩展,在外面很多企业项目中被使用了,所以我介绍下这个插件的用法。
PullToRefresh支持系数:
支持下拉与上拉
支持listfragment
支持众多控件,如:&& &
ExpandableListView
ScrollView
HorizontalScrollView
下载库路径:
下载完成,导入项目,在你自己的项目library里面添加刚引入的项目。
不扯废话,直接步入介绍使用的正题!
Ps:它支持的控件比较多,不过会一个其他的就能举一反三的使用出来,我们以使用比较多的listview的下拉上拉刷新为实例。
一:写布局
1 &com.handmark.pulltorefresh.library.PullToRefreshListView
android:id=&@+id/plistview&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
二:绑定控件
1 private PullToRefreshListView pListV// PullToRefreshListView控件对象
2 @Override
3 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_layout);
pListView = (PullToRefreshListView) findViewById(R.id.plistview);
三: 为listview绑定适配器
1 ArrayList&String& arrayList = new ArrayList&String&();
2 // 初始化适配器
3 adapter = new ArrayAdapter&String&(this, R.layout.item_layout, R.id.tv_item_name, arrayList);
4 adapter.add(&snail&);
5 adapter.add(&_snail&);
6 adapter.add(&__snail&);
7 adapter.add(&___snail&);
8 // 绑定适配器
9 pListView.setAdapter(adapter);
四:设置刷新模式
* 设置PullToRefresh刷新模式
* BOTH:上拉刷新和下拉刷新都支持
* DISABLED:禁用上拉下拉刷新
* PULL_FROM_START:仅支持下拉刷新(默认)
* PULL_FROM_END:仅支持上拉刷新
* MANUAL_REFRESH_ONLY:只允许手动触发
9 pListView.setMode(Mode.PULL_FROM_START);
五:绑定刷新监听事件
// 设置刷新监听
pListView.setOnRefreshListener(new OnRefreshListener&ListView&() {
public void onRefresh(PullToRefreshBase&ListView& refreshView) {
String str = DateUtils.formatDateTime(MainActivity.this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
//设置刷新标签
pListView.getLoadingLayoutProxy().setRefreshingLabel(&正在刷新&);
//设置下拉标签
pListView.getLoadingLayoutProxy().setPullLabel(&下拉刷新&);
//设置释放标签
pListView.getLoadingLayoutProxy().setReleaseLabel(&释放开始刷新&);
//设置上一次刷新的提示标签
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(&最后更新时间:& + str);
//加载数据操作
new MyTask().execute();
其中加载数据代码(我这是本地的一般都是网络获取数据):
1 private class MyTask extends AsyncTask&Void, Void, ArrayList&String&& {
protected ArrayList&String& doInBackground(Void... params) {
Thread.sleep(2000);//睡眠2秒,延迟加载数据
} catch (InterruptedException e) {
e.printStackTrace();
ArrayList&String& mArrayList = new ArrayList&String&();
for (int i = 0; i & 5; i++) {
counter++;
mArrayList.add(&-----& + String.valueOf(counter) + &-------&);
return mArrayL
protected void onPostExecute(ArrayList&String& result) {
for (String string : result) {
adapter.add(string);
pListView.onRefreshComplete();//数据加载到适配器完成后,刷新完成,
super.onPostExecute(result);
pListView.onRefreshComplete(); 在数据加载完成一定要调用这个方法,否则界面刷新那块头部会一直在。
就这简简单单的五部就可以下拉刷新了。效果图如下
但是我们的需求还有个上拉加载更多,这个怎么做呢,只需要把我们的第四部和第五部小小修改下,就可以支持上拉加载更多了。
首先》我们讲我们第四部的模式调为 BOTH
然后》在puutorefresh库的基类文件PullToRefreshBase添加一个判别是上拉还是下拉的方法,代码如下
//判别头部是否展示出来,如果展示出来代表下拉使得头部展示。true为下拉
public boolean isShownHeader() {
return getHeaderLayout().isShown();
//判别低部是否展示出来,如果展示出来代表上拉使得低部展示。true为上拉
public boolean isShownFooter() {
return getFooterLayout().isShown();
最后》修改我们的监听事件
1 // 设置刷新监听
2 pListView.setOnRefreshListener(new OnRefreshListener&ListView&() {
public void onRefresh(PullToRefreshBase&ListView& refreshView) {
String str = DateUtils.formatDateTime(MainActivity.this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
// 下拉刷新 业务代码
if (refreshView.isShownHeader()) {
pListView.getLoadingLayoutProxy().setRefreshingLabel(&正在刷新&);
pListView.getLoadingLayoutProxy().setPullLabel(&下拉刷新&);
pListView.getLoadingLayoutProxy().setReleaseLabel(&释放开始刷新&);
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(&最后更新时间:& + str);
new MyTask().execute();
// 上拉加载更多 业务代码
if(refreshView.isShownFooter()) {
pListView.getLoadingLayoutProxy().setRefreshingLabel(&正在加载&);
pListView.getLoadingLayoutProxy().setPullLabel(&上拉加载更多&);
pListView.getLoadingLayoutProxy().setReleaseLabel(&释放开始加载&);
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(&最后加载时间:& + str);
new MyTask().execute();
就这样我们这个上拉下拉都支持了。效果图如下
另附代码:链接:
密码: 12w5
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致  这里介绍的是PullToRefreshGridView的使用方法,和之前的PullToRefreshListView方法如出一辙,因为这个开源项目模块化很棒,所以很容易实现。等于说我们可以按照之前使用控件的方式来操作,不用考虑其他的问题。
1.写布局文件,放入可以下拉刷新的控件
2.找到下拉刷新的控件,设置监听器,并且在刷新方法中开启一个异步任务来操作
3.通过这个下拉刷新控件的getRefreshableView()方法来得到GridView对象,按照正常的操作来设置适配器
4.在异步任务中通过LinkedList来给头部或者是尾部添加新的数据
1.布局文件
我们可以看到,我们仍旧可以像使用GridView一样,定义GridView的属性。当然可以通过ptr:命名空间来设置专属属性
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" &
&!-- The PullToRefreshGridView replaces a standard GridView widget. --&
&com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="/apk/res-auto"
android:id="@+id/pull_refresh_grid"
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"
ptr:ptrDrawable="@drawable/ic_launcher" /&
&/LinearLayout&
2.找到这个可以下拉刷新的控件,并且设置监听器
这里的监听器和上篇文章讲的不同,是双向的。所以很方便监听滑动操作!
*设置下拉刷新的view,设置双向监听器
private void initPTRGrideView() {
// 得到下拉刷新的GridView
mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
// 设置监听器,这个监听器是可以监听双向滑动的,这样可以触发不同的事件
mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2&GridView&() {
public void onPullDownToRefresh(PullToRefreshBase&GridView& refreshView) {
Toast.makeText(getApplicationContext(), "下拉", Toast.LENGTH_SHORT).show();
new GetDataTask(mPullRefreshGridView, mAdapter, mListItems).execute();
public void onPullUpToRefresh(PullToRefreshBase&GridView& refreshView) {
Toast.makeText(getApplicationContext(), "上拉", Toast.LENGTH_SHORT).show();
new GetDataTask(mPullRefreshGridView, mAdapter, mListItems).execute();
3.找到GridView来进行适配器的设置
//链表数组对象,用来方便添加string对象
private LinkedList&String& mListI
//用来下拉刷新的控件
private PullToRefreshGridView mPullRefreshGridV
//真正用到的控件,它被隐含到PullToRefreshGridView中,所以需要找出来才能使用
private GridView mGridV
//定义GridView的适配器
private ArrayAdapter&String& mA
这里也可以设置适配器中无数据时显示的内容,调用的方法是:setEmptyView()
* 设置GridView,首先找到它,然后设置适配器
private void initGrideView() {
mGridView = mPullRefreshGridView.getRefreshableView();
//定义String数组,然后把它放到LinkedList中,之后只要在异步任务中用LinkedList就可以添加开头和结尾的数据了
String []data = new String[] {"android","ios","wp","java","c++","c#"};
mListItems = new LinkedList&String&();
mListItems.addAll(Arrays.asList(data));
//当适配器中没有数据的时候显示的东西,这里因为我给适配器中填充了string数组,所以不会显示&这里很空,下拉刷新试试&
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setText("这里很空,下拉刷新试试");
//当界面为空的时候显示的视图
mPullRefreshGridView.setEmptyView(tv);
//设置适配器
mAdapter = new ArrayAdapter&String&(this,
android.R.layout.simple_list_item_1, mListItems);
mGridView.setAdapter(mAdapter);
4.执行异步任务,模拟加载数据,这个和之前的写法一样
package com.kale.
import java.util.LinkedL
import android.os.AsyncT
import android.widget.ArrayA
import com.handmark.pulltorefresh.library.PullToRefreshBase.M
import com.handmark.pulltorefresh.library.PullToRefreshGridV
* @author:Jack Tony
:通过异步任务来加载网络中的数据,进行更新
public class GetDataTask extends AsyncTask&Void, Void, Void&{
private PullToRefreshGridView mPullRefreshGridV
private ArrayAdapter&String& mA
private LinkedList&String& mListI
public GetDataTask(PullToRefreshGridView gridView,
ArrayAdapter&String& adapter,LinkedList&String& listItems) {
// TODO 自动生成的构造函数存根
mPullRefreshGridView = gridV
mAdapter =
mListItems = listI
protected Void doInBackground(Void... params) {
//模拟请求,舒眠2秒钟
Thread.sleep(2000);
} catch (InterruptedException e) {
return null;
protected void onPostExecute(Void result) {
// TODO 自动生成的方法存根
super.onPostExecute(result);
//得到当前的模式,来判断数据应该加载到哪个位置
Mode mode = mPullRefreshGridView.getCurrentMode();
if(mode == Mode.PULL_FROM_START) {
mListItems.addFirst("这是刷新出来的数据");
mListItems.addLast("这是刷新出来的数据");
// 通知数据改变了
mAdapter.notifyDataSetChanged();
// 加载完成后停止刷新
mPullRefreshGridView.onRefreshComplete();
MainActivity.JAVA中的全部代码
package com.kale.
import java.util.A
import java.util.LinkedL
import android.app.A
import android.os.B
import android.view.G
import android.widget.ArrayA
import android.widget.GridV
import android.widget.TextV
import android.widget.T
import com.handmark.pulltorefresh.library.PullToRefreshB
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridV
public class MainActivity extends Activity {
//链表数组对象,用来方便添加string对象
private LinkedList&String& mListI
//用来下拉刷新的控件
private PullToRefreshGridView mPullRefreshGridV
//真正用到的控件,它被隐含到PullToRefreshGridView中,所以需要找出来才能使用
private GridView mGridV
//定义GridView的适配器
private ArrayAdapter&String& mA
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
private void initView() {
// TODO 自动生成的方法存根
initPTRGrideView();
initGrideView();
*设置下拉刷新的view,设置双向监听器
private void initPTRGrideView() {
// 得到下拉刷新的GridView
mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
// 设置监听器,这个监听器是可以监听双向滑动的,这样可以触发不同的事件
mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2&GridView&() {
public void onPullDownToRefresh(PullToRefreshBase&GridView& refreshView) {
Toast.makeText(getApplicationContext(), "下拉", Toast.LENGTH_SHORT).show();
new GetDataTask(mPullRefreshGridView, mAdapter, mListItems).execute();
public void onPullUpToRefresh(PullToRefreshBase&GridView& refreshView) {
Toast.makeText(getApplicationContext(), "上拉", Toast.LENGTH_SHORT).show();
new GetDataTask(mPullRefreshGridView, mAdapter, mListItems).execute();
* 设置GridView,首先找到它,然后设置适配器
private void initGrideView() {
mGridView = mPullRefreshGridView.getRefreshableView();
//定义String数组,然后把它放到LinkedList中,之后只要在异步任务中用LinkedList就可以添加开头和结尾的数据了
String []data = new String[] {"android","ios","wp","java","c++","c#"};
mListItems = new LinkedList&String&();
mListItems.addAll(Arrays.asList(data));
//当适配器中没有数据的时候显示的东西,这里因为我给适配器中填充了string数组,所以不会显示&这里很空,下拉刷新试试&
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setText("这里很空,下拉刷新试试");
//当界面为空的时候显示的视图
mPullRefreshGridView.setEmptyView(tv);
//设置适配器
mAdapter = new ArrayAdapter&String&(this,
android.R.layout.simple_list_item_1, mListItems);
mGridView.setAdapter(mAdapter);
源码下载:
阅读(...) 评论()}

我要回帖

更多关于 pullrefreshlistview 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信