【已解决】android recyclerview下拉刷新时为什么会清空上一次的列表然后再显示新的数据?而不是直接插入新数据?
威尼斯人娱乐城
威尼斯人娱乐城
当前位置 : 威尼斯人娱乐城 > 威尼斯人线上娱乐官网

【已解决】android recyclerview下拉刷新时为什么会清空上一次的列表然后再显示新的数据?而不是直接插入新数据?

Adapter:

List<Posts> mPosts;
Context mContext;
public MyRecycerAdapterContext context, List<Posts> posts {
    mPosts = posts;
    mContext = context;
}

public void addItemint positionToAdd {
    mPosts.addpositionToAdd, new Posts;
    notifyItemInsertedpositionToAdd;
}

Volley Request:

StringRequest stringRequest = new StringRequestRequest.Method.GET, jsonUrl,
            new Response.Listener<String> {
                @Override
                public void onResponseString response {
                    mPostsJsonModel = new Gson.fromJsonresponse,
                            PostsJsonModel.class;
                    mPosts = mPostsJsonModel.getPosts;

                }

Activity下拉刷新:

mMyRecycerAdapter.addItem0;

为什么刷新时会清空第一页,然后显示第二页?而不是直接把新请求的数据插入第一页。。。

增加 mMyRecycerAdapter.notifyDataSetChanged;后刷新直接就崩溃了。

崩溃信息:

AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.**..Adapter.MyRecycerAdapter.onBindViewHolderMyRecycerAdapter.java:61

String imageUrl = mPosts.getposition.getAttachments.get0.getImages.getThumbnail.getUrl;**是61行**

public void onBindViewHolderfinal NormalViewHolder holder, int position {
        String titleItem = mPosts.getposition.getTitle;
        holder.mTextView.setTexttitleItem;
        String imageUrl = mPosts.getposition.getAttachments.get0.getImages.getThumbnail.getUrl;}

Posts Model:

public class Posts {

public List<Attachments> attachments;

public List<Attachments> getAttachments {
    return attachments;
}

public void setAttachmentsList<Attachments> attachments {
    this.attachments = attachments;
}
}

Attachments Model:

public class Attachments {
private Images images;

public Images getImages {
    return images;
}

public void setImagesImages images {
    this.images = images;
}
}

Images Model:

public class Images {
private Thumbnail thumbnail;

public Thumbnail getThumbnail {
    return thumbnail;
}

public void setThumbnailThumbnail thumbnail {
    this.thumbnail = thumbnail;
}
}

Thumbnail Model:

public class Thumbnail {

private String url;


public void setUrlString url {
    this.url = url;
}

public String getUrl {
    return url;
}
}

这是onCreate

protected void onCreateBundle savedInstanceState {
        super.onCreatesavedInstanceState;
        setContentViewR.layout.activity_main;
        mRecyclerView = RecyclerView findViewByIdR.id.recyclerview;
        mRecyclerView.setLayoutManagernew LinearLayoutManagerthis;
        mRecyclerView.setItemAnimatornew DefaultItemAnimator;
        mRecyclerView.setHasFixedSizetrue;
        mMyRecycerAdapter = new MyRecycerAdapterMainActivity.this,
                mPosts;
        mRecyclerView.setAdaptermMyRecycerAdapter;

把setAdapter放到onCreate中然后就不显示recyclerview了,是空白

因为你每次onResponse都重新new了adapter并setAdapter,之前的数据当然没有了,只有最新的数据。

广告位

威尼斯人娱乐城