StaggaredGridLayoutManager 无法正常工作

StaggaredGridLayoutManager not working properly

我正在尝试使用 Recyclerview 将视图从网格更改为列表,再将列表更改为网格。当我 运行 我的应用程序第一次出现时,我会像第一张图片一样以网格方式查看

现在,当我单击图标将视图从网格转换为列表时,我得到的是这样的列表方式

2.

  1. 但现在如果我再次单击图标以更改视图,它将提供正确的输出

现在的问题是为什么我无法像 1 个屏幕截图中提到的那样以网格方式获得视图

oncreateview

 gridimg.setOnClickListener(this);
   llm = new LinearLayoutManager(ProductListActivity.this);
         rv = (RecyclerView)findViewById(R.id.recycler_viewproduts);

        rv.setLayoutManager(llm);

使用 volley 和设置适配器获取 Json 响应

 private void makeJsonArrayRequest() {



        showpDialog();

        JsonArrayRequest req = new JsonArrayRequest( url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d("ress", response.toString());
                        productlist = new ArrayList<ProductListModel>();
                        // Parsing json

                        ch_list = new ArrayList<String>();
                        color_list=new ArrayList<String>();

                        try {....................

                                   rvAdapter = new RecyclerViewAdapter(ProductListActivity.this,productlist,ch_list);
                            rv.setAdapter(rvAdapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                        hidepDialog();

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        //adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("ErrorVolley", "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                hidepDialog();

            }
        });


        MyApplication.getInstance().addToReqQueue(req, "jreq");
    }

点击

 case R.id.product_grid:

                Toast.makeText(ProductListActivity.this, "Refresh App", Toast.LENGTH_LONG).show();
                isViewWithCatalog = !isViewWithCatalog;
               supportInvalidateOptionsMenu();
                //loading = false;
                rv.setLayoutManager(isViewWithCatalog ? new LinearLayoutManager(this) : new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
                rv.setAdapter(rvAdapter);
                break;

变化:

gridimg.setOnClickListener(this);
   llm = new LinearLayoutManager(ProductListActivity.this);
         rv = (RecyclerView)findViewById(R.id.recycler_viewproduts);

        rv.setLayoutManager(llm);

gridimg.setOnClickListener(this);
   llm =  new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
         rv = (RecyclerView)findViewById(R.id.recycler_viewproduts);

    rv.setLayoutManager(llm);