user10287903
user10287903

Reputation:

RecyclerView add extra space on refresh

I have implemented RecyclerView in my project,on my onPostExecute method I have added Decoration to my recyclerview but when refresh the Decoration is increasing. my code :

protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    progressDialog.dismiss();
    FeedsAdapter adapter = new FeedsAdapter(context, feedItems);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.addItemDecoration(new VerticalSpace(20));
    recyclerView.setAdapter(adapter);

}

Upvotes: 0

Views: 960

Answers (2)

Jarda Havelik
Jarda Havelik

Reputation: 540

You probably need to remove decoration before adding new one.

Each time you call the onPostExecute method it will add new ItemDecoration to RecyclerView (thats why space is increasing). Just try to remove it before adding, f.e. via removeItemDecoration(referenceToItemDecoration) or removeItemDecorationAt(0) method.

Upvotes: 2

Joachim Haglund
Joachim Haglund

Reputation: 820

You do not have to create a new adapter every time you run the http call (or whatever the postExecute is for), you simply need to update the feedItems and call NotifyDataSetChanged().

right now you are recreating the adapter and adding vertical space every time you run postExecute, so maybe just add the adapter and the verticalSpace in oncreate, and then only update the dataset in postExecute and then notifyDataSetChanged

Upvotes: 0

Related Questions