4

我正在尝试将视图从 Grid 更改为 list 并使用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;
4

1 回答 1

2

改变:

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);
于 2016-06-09T11:35:57.793 回答