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>
阅读全文
0 0
- PullToRefreshGridView
- PullToRefreshGridView
- PullToRefreshGridView
- PullToRefreshGridView
- PullToRefreshGridView下拉刷新
- PullToRefreshGridView添加头部
- PullToRefreshGridView的使用
- pullToRefreshGridView里面不显示数据
- 上拉分页加载PullToRefreshGridView
- PullToRefreshGridView中xml的配置
- PullToRefreshGridView上拉刷新,下拉加载
- 开源插件 PullToRefresh: PullToRefreshListView / PullToRefreshGridView 实例详解
- PullToRefreshGridView添加头部, GridView添加头部head的PullToRefresh
- PullToRefreshGridView 带下拉刷新,上拉加载的GridView控件
- PullrefreshLibrary(PullToRefreshGridView)上拉刷新,下拉加载+异步AsyncTask(二)
- 抽屉布局+Fragment+TabLayout上导航栏+PullToRefreshListView+轮播图+PullToRefreshGridView
- 开源项目PullToRefresh详解(二)——PullToRefreshGridView
- PullToRefreshGridView改变下拉的文字(2015.8.19第一次使用csdn博客)
- js 原型的问题 Object 和 Function 到底是什么关系?
- Java基础之数组
- jq 移除属性 removeAttr();
- spring生命周期
- Java容器有哪些?
- PullToRefreshGridView
- Android JNI编程
- XRecyclerView的下拉刷新上拉加载功能
- angular实现投票
- RGB颜色查询对照表
- IE兼容问题解决
- noip题目复习
- 操作系统面试题汇总(持续更新)
- 购物车