2016-06-09 32 views
4

我正在嘗試將網格視圖更改爲使用Recyclerview列出並列入網格。當我第一次運行我的應用我很喜歡第一圖像越來越鑑於電網方式現在StaggaredGridLayoutManager無法正常工作

enter image description here

當我點擊圖標可從網格轉換視圖以列出我得到它以列表的方式這樣

2.

enter image description here

  • 但現在,如果我再次點擊圖標來改變視圖,將給予適當的輸出
  • enter image description here

    現在的問題是,爲什麼我不能夠獲得視圖在網格的方式在1個截圖提到

    oncreateview

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

    使用VO獲取JSON響應lley和設置適配器

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

    的onclick

    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; 
    
    +0

    它的layoutManager你在開始的時間設定是指初始化時間? – Mehta

    +0

    linearlayoutmanager正如問題 –

    +0

    在初始化時所提到的那樣,您正在設置linearLayoutManager而不是StaggeredGridLayoutManager ...因此您第一次沒有得到正確的輸出。 – Mehta

    回答

    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); 
    
    +0

    感謝它的工作它是這樣一個愚蠢的錯誤..保存我的日子 –

    +0

    現在,它首先顯示gridview,如果我想以listview的方式顯示它..你能告訴? –