GridView get view by position, first child view different

前端 未结 3 698
失恋的感觉
失恋的感觉 2020-12-19 05:51

Android GridView is quite interesting, it reuses the child views. The ones scrolled up comes back from bottom. So there is no method from GridView

3条回答
  •  情歌与酒
    2020-12-19 06:35

    You can't reach the views directly because of the recycling. The view at position 0 may be re-used for the position 10, so you can't be sure of the data present in a specific view.

    The way to go is to use the underlying data. If you need to modify data at position 10, then do it in the List or array under your adapter and call notifyDataSetChanged() on the adapter.

    if you need to have different views for different data subtypes, you can override the two following method in your adapter: getItemViewType() and getViewTypeCount()

    Then, in getView() you can 1) decide which layout to inflate 2) know the type of view recycled using getItemViewType()

    You can find an example here: https://stackoverflow.com/a/5301093/990616

提交回复
热议问题