【问题标题】:Using RecyclerView.OnScrollListener in RecyclerView to implement loading more items got duplicated items在 RecyclerView 中使用 RecyclerView.OnScrollListener 实现加载更多项得到重复项
【发布时间】:2018-11-30 06:11:52
【问题描述】:

我试图使用 Retrofit 2 将 JSON 数据提取到 RecyclerView 中。我还实现了分页。现在进行测试,我更改了 JSON URL,就好像它只会在每页显示 5items。可以通过在 JSON URL 末尾添加“&page=PAGE#”来更改页码(其中 PAGE# 是从 1 开始的整数值,如果没有提及页码,则默认显示第一页)。

问题:我已成功地将每个页面添加到 Recyclerview。但是,它会将最后一页的项目添加两次,有时会添加 3 次。
像这样: 最后一个帖子会加几次

我找不到问题所在。一步一步的调试没有帮助。请帮我。我正在学习这些,如果你能帮助我,我将不胜感激。

WPPOST POJO:

public class WPPost {

  @SerializedName("code")
  @Expose
  private String code;
  @SerializedName("id")
  @Expose
  private Integer id;
  @SerializedName("guid")
  @Expose
  private Guid guid;
  @SerializedName("title")
  @Expose
  private Title title;
  @SerializedName("content")
  @Expose
  private Content content;
  @SerializedName("post_categories")
  @Expose
  private List < String > postCategories = null;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public Guid getGuid() {
    return guid;
  }

  public void setGuid(Guid guid) {
    this.guid = guid;
  }

  public Title getTitle() {
    return title;
  }

  public void setTitle(Title title) {
    this.title = title;
  }

  public Content getContent() {
    return content;
  }

  public void setContent(Content content) {
    this.content = content;
  }

  public List < String > getPostCategories() {
    return postCategories;
  }

  public void setPostCategories(List < String > postCategories) {
    this.postCategories = postCategories;
  }
  public String getCode() {
    return code;
  }

  public void setCode(String code) {
    this.code = code;
  }
}

模型类:

public class Model {

  public int postid;
  public String title;
  public String content;
  public String categoriesnames;
  public Model(int mID, String mTitle, String mContent, String mCategorynames) {
    this.postid = mID;
    this.title = mTitle;
    this.content = mContent;
    this.categoriesnames = mCategorynames;
  }
}

【问题讨论】:

  • 你检查过服务器的回复吗?
  • 是的。我已经检查了回复。没关系。没有重复的响应。

标签: java android android-recyclerview retrofit2


【解决方案1】:

RecyclerViewonScrolled方法可能在短时间内被多次调用,而你的if语句不能阻止getRetrofit()在一次滚动中被多次调用,你可以打印一条日志消息您的请求网址以验证这一点。

因此,您应该通过消除多余的重复请求来解决此问题。例如,使用标志检查 getRetrofit 请求是否正在进行:

public void getRetrofit() {

  isLoadingData = true; // <--- flag

  // ....

  final Call < List < WPPost >> call = service.getPostInfo(url);
  call.enqueue(new Callback < List < WPPost >> () {
    @Override
    public void onResponse(Call < List < WPPost >> call, Response < List < WPPost >> response) {
      isLoadingData = false; // <--- flag
      // ...
    }

    @Override
    public void onFailure(Call < List < WPPost >> call, Throwable t) {
      isLoadingData = false; // <--- flag
    }
  });
}

然后,使用此标志来确定是否应触发下一页请求。

【讨论】:

  • 是的!我认为这是更合适的解决方案。非常感谢@Hong Duan 非常感谢您的帮助。
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
  • 2019-08-30
  • 1970-01-01
  • 2018-01-05
  • 2018-05-25
相关资源
最近更新 更多