2017-02-14 24 views
0

enter image description here如何在Android中製作這個自定義的「網格」佈局?

enter image description here

我需要9 square細胞則long rectangle細胞然後再9 square細胞。我應該如何設置LinearLayoutManager才能將它傳遞給RecyclerView

我嘗試了兩種方式:

LinearLayoutManager llm2 = new LinearLayoutManager(App.getContext()); 
llm2.setOrientation(LinearLayoutManager.VERTICAL); 

但這裏所有的產品隨附的一個單獨的行。

如果我將它設置爲.HORIZONTAL,那麼所有元素都將位於一個較長的行中。

試圖GridLayoutManager

RecyclerView.LayoutManager glm = new GridLayoutManager(App.getContext(), 3); 

但在這裏不知道如何處理長項。

有什麼想法?

回答

0

您可以創建以3跨度GridLayoutManager並在spanSizeLookup返回3的尺寸爲貴「長素」:

final int spanCount = 3; 

    GridLayoutManager glm = new GridLayoutManager(this, spanCount); 
    recyclerView.setLayoutManager(glm); 

    glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 
     @Override 
     public int getSpanSize(int position) { 
      // You could also use adapter.getItemViewType(position) and check if 
      // the type of the view is "LONG_VIEW" instead of using a modulo 
      if (position % 10 == 9) { 
       return spanCount; 
      } else { 
       return 1; 
      } 
     } 
    }); 
1

您可以在每9個項目後使用setSpanCount,您將得到一行是單個單元格。

所以基本上你必須把你的數組中您傳遞到您的適配器「空項目」將表明一個完整的一行,因爲recyclerview仍然需要知道,以顯示它

例如:

陣列

[data,data,data,data,data,data,data,data,data,"",data,data,etc...]