PulltorefreshLibary为依赖包
layout 中的 activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="3"
ptr:ptrMode="both" />
</LinearLayout>
ExpandGridView
/**
* Copyright (C) 2013-2014 EaseMobTechnologies. All rights reserved.
*
* Licensed under the Apache License,Version 2.0 (the "License");
* you may not use this file except incompliance with the License.
* You may obtain a copy of the Licenseat
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law oragreed to in writing, software
* distributed under the License isdistributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OFANY KIND, either express or implied.
* See the License for the specificlanguage governing permissions and
* limitations under the License.
*/
package com.example.mygridview;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;
public class ExpandGridView extends GridView {
public ExpandGridView(Context context){
super(context);
}
public ExpandGridView(Context context,AttributeSet attrs) {
super(context, attrs);
}
/*
* 测量宽度和高
* */
@Override
public void onMeasure(intwidthMeasureSpec, int heightMeasureSpec) {
//实际高度
int expandSpec =MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec,expandSpec);
}
}
MainActivity
packagecom.example.mygridview;
importjava.util.ArrayList;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.widget.GridView;
importcom.handmark.pulltorefresh.library.PullToRefreshBase;
importcom.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
importcom.handmark.pulltorefresh.library.PullToRefreshGridView;
importcom.handmark.pulltorefresh.library.PullToRefreshScrollView;
publicclass MainActivity extends Activity {
private PullToRefreshGridViewmPullRefreshGridView ;
private PullToRefreshScrollViewscro_luck_PullToRefreshScrollView;
private MyGridViewAdapter myGridViewAdapter;
ArrayList<Integer> imgIdList=newArrayList<Integer>();
ArrayList<Integer> allImgIdList=newArrayList<Integer>();
private GridView gridView;
private int currPage;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPullRefreshGridView = (PullToRefreshGridView)findViewById(R.id.pull_refresh_grid);
gridView =mPullRefreshGridView.getRefreshableView();
myGridViewAdapter = newMyGridViewAdapter(this);
mPullRefreshGridView.setAdapter(myGridViewAdapter);
getData();
myGridViewAdapter.addrest(imgIdList);
//ʵÏÖÉÏÀË¢ÐÂÓëÏÂÀ¼ÓÔØ
mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2() {
@SuppressWarnings("rawtypes")
@Override
public voidonPullDownToRefresh(PullToRefreshBase refreshView) {
imgIdList.clear();
getData();
myGridViewAdapter.addrest(imgIdList);
refreshView.onRefreshComplete();
}
@SuppressWarnings("rawtypes")
@Override
public void onPullUpToRefresh(PullToRefreshBaserefreshView) {
allImgIdList.addAll(imgIdList);
myGridViewAdapter.addrest(allImgIdList);
refreshView.onRefreshComplete();
}
});
}
private void getData() {
for(int i = 0; i < 50; i++) {
imgIdList.add(R.drawable.ic_launcher);
}
}
}
package com.example.mygridview;
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class MyGridViewAdapter extends BaseAdapter {
Contextcontext;
ArrayList<Integer>imgIdList=new ArrayList<Integer>();
publicMyGridViewAdapter(Context context) {
super();
this.context= context;
}
public voidaddrest(ArrayList<Integer> imgIdList){
this.imgIdList.clear();
this.imgIdList.addAll(imgIdList);
this.notifyDataSetChanged();
}
@Override
public intgetCount() {
returnimgIdList.size();
}
@Override
public ObjectgetItem(int position) {
// TODOAuto-generated method stub
returnnull;
}
@Override
public longgetItemId(int position) {
// TODOAuto-generated method stub
return 0;
}
@Override
public ViewgetView(int position, View convertView, ViewGroup parent) {
ImageViewimageView=new ImageView(context);
imageView.setImageResource(R.drawable.ic_launcher);
returnimageView;
}
}