zoukankan      html  css  js  c++  java
  • Android开发之PullToRefreshGridView下拉刷新上拉加载

    首先把PullToRefresh的第三方依赖库导入到工程中,并依赖它:(依赖库如下图)

    布局文件:

    <?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>

    下面是Activity的核心代码:

    package com.bwie.main;
    
    import java.util.List;
    
    import com.bwie.adapter.JsonAdapter;
    import com.bwie.bean.JsonResult;
    import com.bwie.bean.Jsonarray;
    import com.google.gson.Gson;
    import com.handmark.pulltorefresh.library.PullToRefreshBase;
    import com.handmark.pulltorefresh.library.PullToRefreshGridView;
    import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
    import com.lidroid.xutils.HttpUtils;
    import com.lidroid.xutils.exception.HttpException;
    import com.lidroid.xutils.http.ResponseInfo;
    import com.lidroid.xutils.http.callback.RequestCallBack;
    import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.GridView;
    
    public class ItemActivitytwo extends Activity {
        private int index = 0;
        private String xmlid;
        private List<Jsonarray> list;
        private GridView gridView;
        private JsonAdapter adapter;
        private PullToRefreshGridView mPullRefreshGridView;
    
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.itemactivity);
    
            Intent intent = getIntent();
            xmlid = intent.getStringExtra("lsid");
            // 获得PullToRefreshGridView
            mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
            gridView = mPullRefreshGridView.getRefreshableView();
            gethttp();
            // 实现上拉刷新与下拉加载
            mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2() {
    
                @SuppressWarnings("rawtypes")
                @Override
                public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                    gethttp();
                }
    
                @SuppressWarnings("rawtypes")
                @Override
                public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                    gethttp();
                }
    
            });
    
        }
    
        private void gethttp() {
            // TODO Auto-generated method stub
            String url = "http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id="
                    + xmlid + "&pn=5&rn=" + index + "";
            HttpUtils httpUtils = new HttpUtils();
            httpUtils.send(HttpMethod.POST, url, new RequestCallBack<String>() {
    
                @Override
                public void onFailure(HttpException arg0, String arg1) {
                    // TODO Auto-generated method stub
                    Log.i("TAG", "请求失败啦");
                }
    
                @Override
                public void onSuccess(ResponseInfo<String> arg0) {
                    // TODO Auto-generated method stub
                    // gson解析
                    Gson gson = new Gson();
                    JsonResult fromJson = gson.fromJson(arg0.result,
                            JsonResult.class);
                    list = fromJson.getResult().getData();
                    Log.i("TAG", list.toString());
                    adapter = new JsonAdapter(list, getApplicationContext());
                    gridView.setAdapter(adapter);
                    index = index++;
                    // 适配器刷新
                    adapter.notifyDataSetChanged();
                    mPullRefreshGridView.onRefreshComplete();
                }
            });
        }
    
    }
  • 相关阅读:
    Jquery里live事件移除原因
    js委托事件-addEventListeners(冒泡方向)
    后端同同不肯给我算好的时间差给我,只好自己写了:
    js滚动到顶部底部代码
    浏览器地址栏运行HTML代码(谷歌)
    黑马vue---56-58、vue组件创建的三种方式
    黑马vue---46、vue使用过渡类名实现动画
    黑马vue---28、vue中全局过滤器的基本使用
    vue devtools无法使用
    红心点赞效果的实现
  • 原文地址:https://www.cnblogs.com/bokeyuan007/p/5459762.html
Copyright © 2011-2022 走看看