PullToRefreshGridView

来源:互联网 发布:网络继续教育学院 编辑:程序博客网 时间:2024/05/21 19:28

<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

=============mainactivity===============================================

package wangaimin.bwie.com.example.lenovo.gridview;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.GridView;import com.google.gson.Gson;import com.handmark.pulltorefresh.library.ILoadingLayout;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshGridView;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;public class MainActivity extends AppCompatActivity {    private PullToRefreshGridView refreshGridView;    private int page_num = 1;    private List<DataDataBean.ResultsBean> list = new ArrayList<>();//记录当前展示的所有数据    private ILoadingLayout startLabels;    private GridViewAdapter gridViewAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        refreshGridView = (PullToRefreshGridView) findViewById(R.id.refresh_grid_view);        getDataFromNet();        //2.设置可以刷新的模式        refreshGridView.setMode(PullToRefreshBase.Mode.BOTH);        //3.设置刷新的时候展示的文字(状态)        startLabels = refreshGridView.getLoadingLayoutProxy(true, false);        startLabels.setPullLabel("下拉刷新");        startLabels.setRefreshingLabel("正在刷新...");        startLabels.setReleaseLabel("放开刷新");        ILoadingLayout endLabels = refreshGridView.getLoadingLayoutProxy(false, true);        endLabels.setPullLabel("上拉刷新");        endLabels.setRefreshingLabel("正在载入...");        endLabels.setReleaseLabel("放开刷新...");        //4.设置下拉和上拉刷新的监听事件        refreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {            @Override            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {                //下拉刷新....请求第一页的数据,清空之前的数据,然后再添加设置适配器                getRefreshData();            }            @Override            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {                //上拉刷新/加载...加载的时候页数++                page_num++;                getDataFromNet();            }        });    }    private void getRefreshData() {        NetDataUtil.getData("http://gank.io/api/data/Android/10/1",MainActivity.this, new JsonCallBack() {            @Override            public void getJsonString(String json) {                //解析                Gson gson = new Gson();                DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);                //先清空一下数据                list.clear();                //添加到集合的最前边,,,,(0,,,,)                list.addAll(0,dataDataBean.getResults());                //设置适配器                setAdapter();                //设置适配器之后停止刷新的操作                refreshGridView.onRefreshComplete();                //可以设置刷新的时间....                startLabels.setLastUpdatedLabel("上次更新时间:"+new SimpleDateFormat("HH:mm").format(new Date(System.currentTimeMillis())));//last最近的,最后一次update修改/更新            }        });    }    private void setAdapter() {        if (gridViewAdapter == null){            gridViewAdapter = new GridViewAdapter(MainActivity.this,list);            refreshGridView.setAdapter(gridViewAdapter);        }else {            gridViewAdapter.notifyDataSetChanged();        }    }    private void getDataFromNet() {        //第一个参数是接口,第二个上下文,第三个回调json数据        NetDataUtil.getData("http://gank.io/api/data/Android/10/"+page_num,MainActivity.this, new JsonCallBack() {            @Override            public void getJsonString(String json) {                //解析                Gson gson = new Gson();                DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);                //往后面添加...                list.addAll(dataDataBean.getResults());                //设置适配器                setAdapter();                //停止刷新                refreshGridView.onRefreshComplete();            }        });    }}
=================activity_main=======================================================

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="wangaimin.bwie.com.example.lenovo.gridview.MainActivity">    <com.handmark.pulltorefresh.library.PullToRefreshGridView        xmlns:ptr="http://schemas.android.com/apk/res-auto"        android:layout_height="match_parent"        android:layout_width="match_parent"        android:numColumns="3"        android:id="@+id/refresh_grid_view"        ptr:ptrDrawable="@drawable/default_ptr_flip"        ptr:ptrAnimationStyle="flip"        ptr:ptrHeaderBackground="#383838"        ptr:ptrHeaderTextColor="#FFFFFF"  >    </com.handmark.pulltorefresh.library.PullToRefreshGridView></LinearLayout>
=============item_gridviewlayout=========================================================

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/grid_image"        android:src="@mipmap/ic_launcher"        android:layout_width="100dp"        android:layout_height="100dp" />    <TextView        android:id="@+id/grid_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 逆水寒包裹满了怎么办 逆水寒包裹里满了怎么办 grim soul包裹满了怎么办 剑三包裹满了怎么办 电脑开机后都是英文怎么办 欠员工工资仲裁老板不到庭怎么办 乔丹拖鞋鞋底硬怎么办 公牛插座电阻烧了怎么办 公牛led灯太刺眼怎么办 公牛插座usb坏了怎么办 墙壁上开关坏了怎么办 刑事二审判决后不服的怎么办 空调外机空间小怎么办 离婚判决生效书丢了怎么办 法院判决离婚对方上诉怎么办 法院判决离婚上诉中级法院怎么办 刑诉中二审发现一审事实错误怎么办 法院判决过了执行期怎么办 刑事犯罪判决后法院未执行怎么办 法院起诉对方不签字怎么办 法院判离婚对方不要签字怎么办 老人不能到法院签字怎么办 法院判决书被告人没签字怎么办? 法院判决离婚一方不签字怎么办 法院判决离婚对方不签字怎么办 法院已判决被告没有钱怎么办 判决还款但是的确没有钱怎么办 强执执行没有钱怎么办 法院判了没钱还怎么办 判决后对方不给钱怎么办 欠钱判决后没钱怎么办 判决了还不给钱怎么办 驾驶证到期本人在监狱服刑怎么办 轻伤过了追诉期怎么办 判决生效书丢了怎么办 高层17楼水压低怎么办 高层六楼水压低怎么办 高层6楼水压低怎么办 高层5楼水压低怎么办 高层五楼水压小怎么办 高层四楼水压低怎么办