2016-09-24 84 views
0

我有一個recyclerViewgridManagerLayout這樣的:我可以在Android的GridManagerLayout中設置動態列寬度嗎?

GridLayoutManager manager = new GridLayoutManager(Main.this, 10); 
manager.setOrientation(LinearLayoutManager.VERTICAL); 
recyclerView.setLayoutManager(manager); 

有沒有一種辦法不同的寬度設置爲每列?我試圖實現這樣的事情: enter image description here

正如你可以看到列0,2,4,6和8需要比其他列小。

我試過manager.setSpanSizeLookup(spanSizeLookup),但我不想更改layout managerspanCount

這就是我現在所擁有的enter image description here

我設置在構造函數中的每個支架的寬度,通過調用 itemView.getLayoutParams()寬= THE_WIDTH。

它看起來像黃線是在自己的位置,不知道爲什麼其他人的偏移

這是圖像

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 

<ImageView 
    android:id="@+id/image" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:adjustViewBounds="true" 
    android:layout_centerHorizontal="true" 
    android:layout_above="@+id/text" 
    android:scaleType="centerCrop"/> 

<TextView 
    android:id="@+id/text" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Category name" 
    android:layout_alignParentBottom="true" 
    android:gravity="center" 
    android:background="#7f007f"/> 

</RelativeLayout> 

的XML這是對的XML它們之間的空間

<?xml version="1.0" encoding="utf-8"?> 
<View xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="25dp" 
android:layout_height="match_parent" 
android:background="#ffff00"/> 

回答

0

在您的適配器中,使用layout.params動態設置項目的寬度,例如。

tv.setLayoutParams(new ViewGroup.LayoutParams(
10, 
ViewGroup.LayoutParams.WRAP_CONTENT)); 

檢查,

if(position%2==0) { 
your_item_parent_layout.setLayoutParams(new ViewGroup.LayoutParams(
10, 
ViewGroup.LayoutParams.WRAP_CONTENT)); 
} 
else{ 
your_item_parent_layout.setLayoutParams(new ViewGroup.LayoutParams(
100, 
ViewGroup.LayoutParams.WRAP_CONTENT)); 
} 

在您的適配器。

希望它有幫助。

+0

上傳您既XML文件太 –

0

對於您需要使用您的AdaptergetItemViewType方法和你要發送類型的佈局,並在onCreateViewHolder,你需要根據你的類型inflatelayouts。正如截圖所示

+0

我已經做了,在每個支架的構造,根據其類型可設置widthheight你的兩個不同的layouts。 – kimv

+0

@kimv那麼問題在哪裏?它應該運作良好 – Nikhil

相關問題