samedi 18 avril 2015

custom adapter view, child not being measured correctly

I wrote a GridView that arranges it's children like the image below. enter image description here


Here's the code:


MyGridView.onLayout:



...
while (position < mAdapter.getCount()) {
View newBottomChild = mAdapter.getView(position, null, this);
addAndMeasureChild(newBottomChild);
position++;
}
...


and here's the addAndMeasureChild:



LayoutParams params = child.getLayoutParams();
if (params == null) {
params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
}
addViewInLayout(child, -1, params, true);
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);


and the item_layout.xml



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="60dp"
android:layout_height="60dp" >

<ImageView
android:id="@+id/image_content"
android:layout_width="60dp"
android:layout_height="60dp"
android:contentDescription="@string/content_description"
android:scaleType="centerCrop" >
</ImageView>

</RelativeLayout>



  • The reason why I put the ImageView into a RelativeLayout is that if I don't, the item will be displayed very large,large enough to fill the entire GridView.

  • I measured the child with UNSPECIFIED MeasureSpec because using other measure spec will not display any item.


Anyway,my grid view is now working as I want, but I don't want to put that ImageView in a RelativeLayout.


Any help will be appreciated.


Aucun commentaire:

Enregistrer un commentaire