Item Decorator添加额外的空间,每次调用时,如何处理这个问题?

时间:2017-02-05 13:19:44

标签: android android-recyclerview retrofit

我遇到的问题与下面的问题几乎相同,我希望我正在使用改造。我希望将数据传递给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();
    }
}

2 个答案:

答案 0 :(得分:5)

考虑您的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(); }

… = ~(cat_enemy1 | cat_playerShip);

答案 1 :(得分:1)

请在添加之前尝试删除itemDecoration,如下所示:

myParent.mymethod()

每次调用addItemDecoration方法时,都会调用itemDecoration类中的onDraw。在该方法中,如果您使用每个单元格的布局参数来添加空间,那么在第一次它是正确的。在接下来的关于获取单元布局参数的调用中,早期增加的空间就在那里。首先删除空格,然后添加新空格。

希望这会有所帮助。