上拉下拉分页加载

时间:2021-11-06 12:37:11

第三方登录获取授权

Platform platform11 = ShareSDK.getPlatform(QQ.NAME);
// platform11.SSOSetting(true);
platform11.authorize();
platform11.showUser(null);// 必须要加的要不然不行!这个才是授权的!
tm = (TelephonyManager) MainActivity.this
                .getSystemService(TELEPHONY_SERVICE);
imi = tm.getDeviceId();
platform11.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onError(Platform platform11, int arg1,
        Throwable arg2) {
        // 弹出失败窗口
}

public void onComplete(Platform platform11, int arg1,
                    HashMap<String, Object> arg2) {
    System.out.println("登录成功。。。。");
    //得到用户信息
    String userId = platform11.getDb().getUserId();
    String userName = platform11.getDb().getUserName();
    String token = platform11.getDb().getToken();
    String icon = platform11.getDb().getUserIcon();//用户头像
    long expiresTime = platform11.getDb().getExpiresTime();
    SimpleDateFormat sdf3 = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm");


    System.out.println("userId    " + userId);
    System.out.println("userName    " + userName);
    System.out.println("token     " + token);
    System.out.println("icon     " + icon);
    System.out.println("expiresTime  "
                        + sdf3.format(expiresTime));

    //跳转页面传值
    Intent intent = new Intent(getApplicationContext(), ShowActivity.class);
                //传用户名和头像
    intent.putExtra("name", userName);
    intent.putExtra("image", icon);
    startActivity(intent);
        }
            @Override
    public void onCancel(Platform arg0, int arg1) {
            }
        });

请求网络数据上拉刷新下拉加载进行分页操作

private void getinfo(final String str) {
    new AsyncTask<Void, Void, List<nList>>() {

        @Override
        protected List<nList> doInBackground(Void... params) {

            //i代表页数
            String path = "http://api.sina.cn/sinago/list.json?channel=hdpic_funny&adid=4ad30dabe134695c3b7c3a65977d7e72&wm=b207&from=6042095012&chwm=12050_0001&oldchwm=12050_0001&imei=867064013906290&uid=802909da86d9f5fc&p="
                    + i;
            // 得到请求的数据
            String string = MyHttp.getHttp(path);
            Gson gson = new Gson();
            Type type = new TypeToken<News>() {
            }.getType();
            News news = gson.fromJson(string, type);
            Data data = news.data;
            List<nList> list = data.list;
            return list;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(List<nList> result) {
            super.onPostExecute(result);
            //调用方法的时候判断是上拉还是下拉,上拉加载新的数据到集合顶部,下拉加载新的数据到集合中
            if (str.equals("刷新")) {
                listall.addAll(0, result);
            } else {
                listall.addAll(result);
            }
            //添加适配器
            setadapter();
        }

        private void setadapter() {
        //在添加适配器时候,判断是否创建了适配器,如果适配器创建,直接适配器更新,否则创建适配器
            if (adapter == null) {
                adapter = new Myadapter(ShowActivity.this, listall);
                listView.setAdapter(adapter);
            } else {
                adapter.notifyDataSetChanged();
            }
        }
    }.execute();
}

上拉刷新方法

@Override
public void onRefresh() {
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            i = i + 1;
            getinfo("刷新");
            load();
        }
    }, 2000);
}

下拉加载的方法

@Override
public void onLoadMore() {
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            i = i + 1;
            getinfo("加载");
            load();
        }
    }, 2000);
}