• 分页加载PullToRefreshLayout+PullableListView


    PullToRefreshLayout+PullableListView实现分页加载功能

    1、修改布局文件(添加下拉刷新和上拉加载的布局)

    <com.abcaaaaa.zyypay.ui.widget.pulltorefresh.PullToRefreshLayout
    android:id="@+id/pulltorefreshlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/rl_mid">

    <include layout="@layout/load_head" />

    <com.abcaaaaa.zyypay.ui.widget.pulltorefresh.PullableListView
    android:id="@+id/pullablelistview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#E0E0E0"
    android:dividerHeight="2px" />

    <include layout="@layout/load_more" />

    </com.abcaaaaa.zyypay.ui.widget.pulltorefresh.PullToRefreshLayout>


    2、设置网络请求的工具类
    /**
    *设置在不同情况下数据刷新的设置
    * @param requestTag
    * @param params
    * @param onDataListener
    * @param showLoad 加载框状态, 0表示弹出收起,1表示只弹出,2表示只收起
    * @param pullToRefreshLayout
    * @param requsetType 当数据位0时表示普通请求,为1时表示刷新请求,为2时表示加载请求
    * @return
    */
    public static RequestHandle doPost(final int requestTag, RequestParams params, final OnDataListener onDataListener, final int showLoad,
    final PullToRefreshLayout pullToRefreshLayout, final int requsetType) {
    // 重连次数,超时时间
    client.setMaxRetriesAndTimeout(0, 5000);
    return client.post(URL, params, new JsonHttpResponseHandler() {
    @Override
    public void onStart() {
    super.onStart();
    onDataListener.onStart(requestTag, showLoad);
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
    super.onSuccess(statusCode, headers, response);
    if (requsetType == 1) {
    pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
    }
    // else if (requsetType == 2){
    // pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
    // }
    onDataListener.onSuccess(requestTag, response, showLoad);
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
    super.onFailure(statusCode, headers, throwable, errorResponse);
    if (requsetType == 1) {
    pullToRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
    } else if (requsetType == 2) {
    pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
    }
    onDataListener.onFailure(requestTag, errorResponse, showLoad);
    }

    @Override
    public void onCancel() {
    super.onCancel();
    onDataListener.onCancel(requestTag, showLoad);
    }
    });
    }

    3、获取网络请求的数据(有添加刷新分页功能)
    //获取结算列表数据---------改为有刷新加载的请求 20170609修改
    public RequestHandle getSettlementList(OnDataListener onDataListener, PullToRefreshLayout pullToRefreshLayout, int requestType,
    int page, int requesTag, int showLoad) {
    String randomCode = SharedPreferencesUtlis.getString(mContext, ConstantUtlis.SP_RANDOMCODE, "");
    HashMap<String, Object> hashMap = new HashMap<>();
    if (page > 0) {
    hashMap.put("页数", page + "");
    } else {
    hashMap.put("页数", 1 + "");
    }
    hashMap.putAll(CommonUtlis.loadMap(ConstantUtlis.SP_INFO_JSON));
    RequestParams params = new RequestParams();
    params.add("func", FUNC_SETTLEMENTLIST);
    params.add("words", randomCode + EnDecryptUtlis.aesEncrypt(JsonUtils.createJSON(hashMap).toString(), randomCode));
    return HttpManager.doPost(requesTag, params, onDataListener, showLoad, pullToRefreshLayout, requestType);
    }

    4、在实现分页刷新加载功能的页面完成如下设置
    4.1声明相关的设置
    private PullToRefreshLayout pullToRefreshLayout;
    private PullableScrollView pullablescrollview;
    private ListViewForScrollView mListView;
    private TextView mTvNoData;
    private TextView tv_settlement_gosettle, tv_settlement_lasttime;
    private List<String[]> list = new ArrayList<>();
    private CommonAdapter mSettlementListAdapter;
    private String lastSettlementTime;
    private int page = 0;

    4.2刷新功能的具体实现
    /**
    * @param requestType 请求类型,初始请求=0,刷新=1,加载=2
    * @param page 页数
    * @param requestTag 请求tag,请求成功后用于区分是哪个请求
    * @param showLoad 是否显示请求加载框 0--显示, -1--不显示
    */
    private void request(int requestType, int page, int requestTag, int showLoad) {
    requestHandleArrayList.add(requestAction.getSettlementList(this, pullToRefreshLayout, requestType, page, requestTag, showLoad));
    }
    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
    if (!list.isEmpty()) {
    mSettlementListAdapter.notifyDataSetChanged();
    }
    list.clear();
    request(1, 0, 2, -1);
    }
    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
    if (page > 0) {
    request(2, page + 1, 3, -1);
    } else {
    MToast.showToast(mContext, "已无数据加载");
    pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
    }
    }
    4.3处理网络请求返回的数据

    /**
    * 处理请求返回数据
    *
    * @param response
    * @param isLoadMore
    */
    private void processResponseData(JSONObject response, boolean isLoadMore) throws JSONException {
    Log.e("结算单列表", response.toString());
    lastSettlementTime = response.getString("上次结算时间");
    tv_settlement_lasttime.setText(lastSettlementTime);
    int num = Integer.parseInt(response.getString("条数"));
    if (num != 0) {//有可展示的数据
    JSONArray jsonArray = response.getJSONArray("列表");
    for (int i = 0; i < jsonArray.length(); i++) {
    String[] a = new String[6];
    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
    a[0] = jsonObject.getString("真实名");
    a[1] = jsonObject.getString("交易金额");
    a[2] = jsonObject.getString("到账金额");
    a[3] = jsonObject.getString("结算单号");
    a[4] = jsonObject.getString("开始时间");
    a[5] = jsonObject.getString("结束时间");
    list.add(a);
    }
    page = num == 10 ? Integer.parseInt(response.getString("页数")) : 0;
    mListView.setVisibility(View.VISIBLE);
    mTvNoData.setVisibility(View.GONE);
    if (isLoadMore) {
    pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
    mSettlementListAdapter.notifyDataSetChanged();
    } else {
    setAdapter();
    }
    } else {//数据条数为0
    if (isLoadMore) {
    MToast.showToast(mContext, "已无数据加载");
    pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
    } else {
    mListView.setVisibility(View.GONE);
    mTvNoData.setVisibility(View.VISIBLE);
    mTvNoData.setText("无数据");
    }
    }
    }
    4.4网络请求成功,根据tag区分请求类别
    @Override
    public void requestSuccess(int requestTag, JSONObject response, int showLoad) throws JSONException {
    super.requestSuccess(requestTag, response, showLoad);
    switch (requestTag) {
    case 1:
    processResponseData(response, false);
    break;
    case 2:
    processResponseData(response, false);
    break;
    case 3:
    processResponseData(response, true);
    break;
    default:
    break;
    }
    }
    4.5设置界面展示的adpter
     private void setAdapter() {
    mSettlementListAdapter = new CommonAdapter<String[]>(SettlementActivity.this, R.layout.item_settlement, list) {
    @Override
    public void convert(final ViewHolder holder, String[] strings) {
    holder.setText(R.id.tv_settlement_name, strings[0]);
    holder.setText(R.id.tv_settlement_oddNum, strings[3]);
    holder.setText(R.id.tv_settlement_starttime, strings[4]);
    holder.setText(R.id.tv_settlement_endtime, strings[5]);
    holder.setText(R.id.tv_jiaoyi, strings[1]);
    holder.setText(R.id.tv_daozhang, strings[2]);
    }
    };//给listview中设置数据
    mListView.setAdapter(mSettlementListAdapter);
    //设置分隔线
    mListView.setDivider(new ColorDrawable(Color.rgb(243, 243, 243)));//#F3F3F3
    mListView.setDividerHeight(16);
        //点击listview中的子控件跳转到对应的子界面
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(SettlementActivity.this, SettlementDetailActivity.class);
    intent.putExtra("name", list.get(position)[0]);
    intent.putExtra("oddNum", list.get(position)[3]);
    intent.putExtra("start", list.get(position)[4]);
    intent.putExtra("end", list.get(position)[5]);
    intent.putExtra("jiaoyi", list.get(position)[1]);
    intent.putExtra("daozhang", list.get(position)[2]);
    startActivity(intent);
    }
    });
    }
    4.6其他相关设置
    在initData(方法中)
    pullablescrollview.smoothScrollTo(0, 0);//默认listview会滑倒顶部位置,这里设置为ScrollView的开始位置
    数据刷新时,集合数据清空,重新进行网络请求
    @Override
    protected void onResume() {
    super.onResume();
    list.clear();
    request(0, 0, 1, 0);
    }
  • 相关阅读:
    小程序动态修改页面标题setNavigationBarTitle
    webapi发布在iis之后报错Http 403.14 error
    vue调用子组件方法时,参数传不过去
    Echarts中X轴坐标太密集,分段显示
    使用echarts时,鼠标首次移入屏幕会闪动,全屏会出现滚动条
    js关于数组的操作(合并数组、添加数组、循环等)
    在vue项目中使用echarts
    npm i安装命令中的-g -D -S的区别
    ArcGIS api for JS 实现三维飞行漫游功能
    Vue通过EventBus实现兄弟组件间通信
  • 原文地址:https://www.cnblogs.com/jingAndroid/p/6992823.html
Copyright © 2020-2023  润新知