gpt4 book ai didi

android - Item Decorator 每次调用都会增加额外的空间,如何处理?

转载 作者:行者123 更新时间:2023-11-29 14:45:03 27 4
gpt4 key购买 nike

我遇到了与以下问题中所述几乎相同的问题,预计我正在使用改造。我希望将数据传递到 spaceDecorator 方法中,因此每次我的设置更改或刷新时,都会添加额外的空间。

RecyclerView decorator adding extra padding on refresh

我尝试了上面建议的所有解决方案,但没有奏效。请提出解决此问题的方法。

我的代码:

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();
}
}

最佳答案

考虑您的 onResponse() 方法,您将 spaceDecorator 添加为项目装饰。此方法是累加的,因此对于每次更新,您都会在项目之间添加空格。相反,您必须先删除旧的 spaceDecorator,然后再添加新的。

@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();
}

关于android - Item Decorator 每次调用都会增加额外的空间,如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42052518/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com