1. 程式人生 > >在使用SmartRefreshLayout中,用onResume方法會載入重複資料以及解決

在使用SmartRefreshLayout中,用onResume方法會載入重複資料以及解決

先描述一下問題,就是在使用SmartRefreshLayout本身是沒有問題的,但是我需要在進入這個介面的時候就進行載入,就用到了onResume();在這個方法裡我進行了網路請求資料,但是出現了這麼一個問題,在我點選跳轉回來的資料載入第二頁就會多出來幾條資料,下面標紅的是我的解決方法。

首先是XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srlEnableFooterFollowWhenLoadFinished="true">

        <ListView
            android:id="@+id/list_records"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/dimen_9dp"
            android:divider="@color/dividerColor_view"
            android:dividerHeight="@dimen/dp_8" />

    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

    <com.dyh.library.widget.NoDataView
        android:id="@+id/relat_not"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

</RelativeLayout>

 

Activity:

 

package com.dyhoa.school.uinew.earnmoney;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.dyh.frame.util.JsonUtils;
import com.dyh.library.base.PageHelperNew;
import com.dyh.library.base.TitleFragmentActivity;
import com.dyh.library.base.net.DefaultStringRequestNew;
import com.dyh.library.base.net.ResponseListenerNew;
import com.dyh.library.base.net.VolleyUtil;
import com.dyh.library.util.UserHelper;
import com.dyh.library.widget.NoDataView;
import com.dyhoa.school.R;
import com.dyhoa.school.global.HostStudentNew;
import com.dyhoa.school.ui.director.BusinessMessageActivity;
import com.dyhoa.school.uinew.adapter.RedPacketRecordsAdapter;
import com.dyhoa.school.uinew.earnmoney.bean.RedRecordsBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;

/**
 * Created by ljg on 2018/11/21.
 * 紅包記錄
 */

public class RedPacketRecordsActivity extends TitleFragmentActivity {

    private ListView listrds;
    private SmartRefreshLayout refreshLayout;
    private NoDataView noDateLayout;
    private PageHelperNew pageHelper;
    private RedPacketRecordsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addLayout(R.layout.activity_red_packect_records);
        initView();
        pageHelper = PageHelperNew.getNewInstance();
        pageHelper.setPageSize(10);
        refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout layout) {
                refreshLayout.setNoMoreData(false);
                pageHelper.pullDown();
                reqDatalist();
                refreshLayout.finishRefresh();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout layout) {
                if (pageHelper.pullUp(refreshLayout)) {
                    reqDatalist();
                }
            }
        });
        adapter = new RedPacketRecordsAdapter(mContext, null);
        listrds.setAdapter(adapter);
//        Gourl() 文章連結    Content()  內容      ShareImg()   分享圖片   ShareUrl() 分享連結    ShareSwitch()  判斷分享是連結還是圖片
        listrds.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RedRecordsBean.DataEntity.PageEntity entity = (RedRecordsBean.DataEntity.PageEntity) parent.getAdapter().getItem(position);
                if (entity.getPacketType() == 4) {//轉發紅包
                    if (!TextUtils.isEmpty(entity.getGourl())) {
                        HtmlForwardActivity.toMe(mContext, entity.getArticleId(), entity.getPacketId(), entity.getGourl(), entity.getTitle(), entity.getContent(), entity.getIcon(), entity.getShareUrl(), entity.getShareImg(), entity.getShareSwitch(), true);
                    }
                } else if (entity.getPacketType() == 5) {//閱讀紅包
                    if (!TextUtils.isEmpty(entity.getGourl())) {
                        Intent intent = new Intent(mContext, HtmlReadingActivity.class);
                        intent.putExtra("packetId", entity.getPacketId());
                        intent.putExtra("goUrl", entity.getGourl());
                        mContext.startActivity(intent);
                        overridePendingTransition(R.anim.bottom_in, R.anim.bottom_silent);
                    }
                } else if (entity.getPacketType() == 6) {//商訊紅包
                    Intent intent = new Intent(mContext, BusinessMessageActivity.class);
                    intent.putExtra("flag", 1);
                    intent.putExtra("packetId", entity.getPacketId());
                    intent.putExtra("shareUrl", entity.getShareUrl());
                    intent.putExtra("title", entity.getTitle());
                    intent.putExtra("content", entity.getContent());
                    intent.putExtra("icon", entity.getIcon());
                    intent.putExtra("shareImg", entity.getShareImg());
                    intent.putExtra("shareSwich", entity.getShareSwitch());
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshLayout.setNoMoreData(false);
        pageHelper.pullDown();
reqDatalist(); } private void initView() { listrds = (ListView) findViewById(R.id.list_records); noDateLayout = (NoDataView) findViewById(R.id.relat_not); refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout); } @Override protected void initTitileBar() { titleLayout.showAll(R.string.red_packet, R.string.historical, new View.OnClickListener() { @Override public void onClick(View v) { startActivity(HistoricalRecordsActivity.class); } }); } private void reqDatalist() { HashMap<String, String> map = new HashMap<>(); map.put("mid", UserHelper.getUUUID() + ""); map.put("cityId", UserHelper.getCity() + ""); map.put("pageSize", pageHelper.getPageSize() + ""); map.put("pageIndex", pageHelper.getCurPage() + ""); String path = pageHelper.getUrlNull(HostStudentNew.RED_PACKECT_RECORDS, map); DefaultStringRequestNew req = new DefaultStringRequestNew(mContext, path, new ResponseListenerNew(mContext) { @Override public void onSuccess(String response) { RedRecordsBean detailsBean = JsonUtils.fromJson(response, RedRecordsBean.class); if (detailsBean != null) { if (detailsBean.getResult() == 0) { RedRecordsBean.DataEntity data = detailsBean.getData(); if (data != null) { pageHelper.successSmart(refreshLayout, noDateLayout, data.getPage(), data.getPageCount(), adapter); } } else { showToast(detailsBean.getMessage()); } } if (refreshLayout != null) refreshLayout.finishLoadMore(); } }); VolleyUtil.getInstance().addToRequestQueue(req); } }