Question

I'm trying to get an adapterview to draw its child views properly, but am having trouble getting the child views of the views returned by the adapter to draw. Specifically, my adapter should be spitting out simple views like this:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF">

        <TextView
            android:id="@+id/photo_cell_contents"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10px" 
            android:text="@string/photo_cell_default_text" 
            android:textColor="#000000" 
            android:textSize="80px"/>

</LinearLayout>

The getView() method of adapter does get invoked and does indeed return instances of this view to the adapterview. I've also put breakpoints in my adapterview and can see that there is a proper view hierarchy there - the root linear layout with a child textview object. However, when the adapter view lays out and draws the child views returned by the adapter, the inner textviews are not being drawn. I see the actual colored boxes corresponding to each cell, but not the text that should appear inside. Does anyone have an idea of why this could be happening? Below is the code in the onLayout() method of the adapter view that lays out the child views (which get added when I setAdapter() and will get updated as I scroll through the adapterview):

    for (int idx = 0; idx < this.getChildCount(); idx++) {
        if (idx != dragged)
        {
            Point xy = getCoorFromIndex(idx);
            View child = getChildAt(idx);
            child.layout(xy.x, xy.y, xy.x + childSize, xy.y + childSize); //view group will layout the children based on the sizes determined for available columns
            int left = child.getLeft();
            int right = child.getRight();
            int top = child.getTop();
            View tv = ((ViewGroup)child).getChildAt(0);
            int tvleft = tv.getLeft();
            int tvright = tv.getRight();
            int tvtop = tv.getTop();

            Log.i("dgv", "Here is info about main view ... left =  " + left + "; right = " + right + "; top = " + top);
            Log.i("dgv", "Here is info about text view ... left =  " + tvleft + "; right = " + tvright + "; top = " + tvtop);


        }
    }
Was it helpful?

Solution

The issue was that I needed to both measure and layout the child views in my layout method for the adapter view. After adding in code to measure the child view and then calling child.layout(), the views were showing up correctly. Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top