下载地址:https://github.com/chrisbanes/Android-PullToRefresh
下载解压后,导入解压包中的extras项目
xml:
<span style="font-size:18px;"><com.handmark.pulltorefresh.library.PullToRefreshGridView android:id="@+id/gvList" android:layout_width="match_parent" android:layout_height="match_parent" android:horizontalSpacing="5dp" android:verticalSpacing="5dp" android:layout_margin="5dp" android:numColumns="2" /></span>
注:高度一定要设置match_parent,否则高度不够,会渲染不出来,到时候自定义适配器中的getview()的position会一直为0
注:一定不能在xml中设置不可见。否则就算在Java中设置可见也显示不出来,可以在需要的地方Java设置不可见。
<span style="font-family:SimSun;font-size:18px;">private PullToRefreshGridView gvList;</span>
private CommodityFiltrateGridViewAdapter mAdapter;
gvList = (PullToRefreshGridView ) findViewById(R.id.gvList);mAdapter = new CommodityFiltrateGridViewAdapter(this);
protected void setGridView() {
initIndicator(); gvList.setAdapter(mAdapter); gvList.setMode(Mode.BOTH);//能下拉刷新和上拉加载 gvList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //在这里执行PullToRefreshGridView的点击item后要做的事 } }); gvList.setOnRefreshListener(new OnRefreshListener2<GridView>(){ @Override public void onPullDownToRefresh( PullToRefreshBase<GridView> refreshView) { // Log.e("TAG", "onPullDownToRefresh");//下拉刷新 pageindex = 1; getData_GridView(pageindex);//异步获取数据 } @Override public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) { // Log.e("TAG", "onPullUpToRefresh"); // 上拉加载 pageindex++; getData_GridView(pageindex);//异步获取数据 } }); } private void initIndicator() { ILoadingLayout startLabels = gvList .getLoadingLayoutProxy(true, false); startLabels.setPullLabel("下拉刷新...");// 刚下拉时,显示的提示 startLabels.setRefreshingLabel("刷新中...");// 刷新时 startLabels.setReleaseLabel("松开刷新数据...");// 下来达到一定距离时,显示的提示 ILoadingLayout endLabels = gvList.getLoadingLayoutProxy( false, true); endLabels.setPullLabel("上拉加载...");// 刚下拉时,显示的提示 endLabels.setRefreshingLabel("加载中...");// 刷新时 endLabels.setReleaseLabel("松开加载数据...");// 下来达到一定距离时,显示的提示 }
protected void getData_GridView(final int page) {
//这里是使用异步加载数据操作
<span style="font-family: Arial, Helvetica, sans-serif;">gvList.onRefreshComplete();</span>//加载完成后,设置刷新完成!</span>
}
注:当上拉没有新数据时,执行:gvList.setMode(Mode.PULL_DOWN_TO_REFRESH); 表示只能下拉刷新
注:列表滑动到指定位置:gvList.getRefreshableView().setSelection(count);
时间: 2024-11-09 11:03:17