Cannot draw child views in a view group when using an adapter

I am trying to get the adapter to correctly draw its child views, but it is difficult for me to get the child views of the views returned by the adapter for drawing. In particular, my adapter should spit out simple views, for example:

<?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 the adapter calls and actually returns instances of this view to the adapter. I also set breakpoints in my adapter and see that there is the correct hierarchy of views - the root linear layout with a child textview. However, when the adapter view exits and draws the child views returned by the adapter, internal text images are not output. I see the actual colored rectangles corresponding to each cell, but not the text that should appear inside. Does anyone have an idea why this might happen? Below is the code in the onLayout () method in the adapter view, which displays the child views (which are added when I install Adapter () and are updated when scrolling through the adapter):

  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); } } 
+4
source share
1 answer

The problem was that I needed to both measure and place child views in my layout method to represent the adapter. After adding code to measure the child view, and then calling child.layout (), the views displayed correctly. Hope this helps.

+2
source

Source: https://habr.com/ru/post/1399232/


All Articles