2016-09-24 139 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