potter13 potter13 - 4 years ago 128
Android Question

Item Decorator adding extra space, each time it is called, how to handle this issue?

I have almost same problem as stated in the below question, expect that I am using retrofit. I want the data to be passed into spaceDecorator method, so each time my settings changed or refreshed, extra space being added.

RecyclerView decorator adding extra padding on refresh

I tried all the solutions suggested above, but didn't work. Please suggested a way to handle this issue.

My code :

public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

public static final String top_rated = "http://api.themoviedb.org/3/movie/top_rated?api_key=1401a8cfe672a592a1a72b023d9febd7";

public static final String popular = "http://api.themoviedb.org/3/movie/popular?api_key=1401a8cfe672a592a1a72b023d9febd7";

public static final String baseUrl = "http://api.themoviedb.org/3/";

public static final String apiKEy = "1401a8cfe672a592a1a72b023d9febd7";

public static final String preUrl = "http://image.tmdb.org/t/p/w342";

public static List<MovieData> movieDataList;

private static SpaceDecorator spaceDecorator;

private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ProgressDialog mProgressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(myToolbar);

recyclerView = (RecyclerView)findViewById(R.id.main_recyclerview);
recyclerView.setHasFixedSize(true);
layoutManager = new GridLayoutManager(MainActivity.this,2);
recyclerView.setLayoutManager(layoutManager);

//use a layout manager, a grid layout manager with 2 columns in our case

updateJson();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_settings){
startActivity(new Intent(this, SettingsActivity.class));
return true;
}

return super.onOptionsItemSelected(item);
}


private void updateJson(){
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setMessage("Loading...");
mProgressDialog.show();

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
final RequestInterface requestInterface = retrofit.create(RequestInterface.class);

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String sort_by = sharedPreferences.getString("sort_by","popular");

Call<JsonMovieResponse> call = requestInterface.getTopRatedMovies(sort_by, apiKEy);
call.enqueue(new Callback<JsonMovieResponse>() {

@Override
public void onResponse(Call<JsonMovieResponse> call, Response<JsonMovieResponse> response) {

movieDataList = response.body().getResults();
adapter = new MainRecyclerViewAdapter(MainActivity.this, movieDataList);
recyclerView.setAdapter(adapter);
spaceDecorator = new SpaceDecorator(4,adapter.getItemCount());
recyclerView.addItemDecoration(spaceDecorator);

if(mProgressDialog.isShowing())
mProgressDialog.dismiss();
}

@Override
public void onFailure(Call<JsonMovieResponse> call, Throwable t) {
Log.e("retrofit error", t.toString());
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
PreferenceManager.getDefaultSharedPreferences(this).
unregisterOnSharedPreferenceChangeListener(this);
Log.i("main","destroy");
}

@Override
protected void onResume() {
super.onResume();
PreferenceManager.getDefaultSharedPreferences(this).
registerOnSharedPreferenceChangeListener(this);
Log.i("activity","main resume");
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
Log.i("main settings", "changed");
updateJson();
}
}

Answer Source

Consider your onResponse() method wehre you add your spaceDecorator as an item decoration. This method is additive, so for every update you're adding up spaces between item. Instead you have to remove the old spaceDecorator before adding a new one.

@Override
public void onResponse(Call<JsonMovieResponse> call, Response<JsonMovieResponse> response) {

    movieDataList = response.body().getResults();
    adapter = new MainRecyclerViewAdapter(MainActivity.this, movieDataList);
    recyclerView.setAdapter(adapter);

    if (spaceDecorator != null)
        recyclerView.removeItemDecoration(spaceDecorator);

    spaceDecorator = new SpaceDecorator(4,adapter.getItemCount());
    recyclerView.addItemDecoration(spaceDecorator);

    if(mProgressDialog.isShowing())
        mProgressDialog.dismiss();
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download