GridView刷新

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);

}     

    }

}




MyGridViewAdapter

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;

    }

 

 

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值