GridView getChildAt() returns null

前端 未结 1 616
名媛妹妹
名媛妹妹 2021-01-14 13:59

I\'m trying to get a View from GridView. Unfortunately, it returns null.

onCreate():

GridView gridview = (GridView) fin         


        
相关标签:
1条回答
  • 2021-01-14 14:20

    GridView will populate itself from the adapter during the first layout pass. This means it won't have children in onCreate(). The easiest way to wait for the first layout pass is to use a ViewTreeObserver (see View.getViewTreeObserver()) to register a global layout listener. The first time the listener is invoked, the grid should contain children.

    Note that if you want to change the background of one of the children of a GridView you really should do it from the adapter. Since views are recycled the background may appear to "move" to another view later on.

    0 讨论(0)
提交回复
热议问题