Are Android View id supposed to be unique?

前端 未结 3 993
慢半拍i
慢半拍i 2020-12-18 18:17

Ok so something that I am confused with is whether Android ids need to be unique or not. Here is why the confusion arises:
Let\'s just say there is an Activity

3条回答
  •  时光说笑
    2020-12-18 18:30

    You do a convertView.findViewById(..).

    If all the views need to have a unique id, how does changing the content of View in the getView not result in haphazard behavior ?

    This is because you create an instance of the view by inflating it. In this instance the ids need to be unique. Otherwise you will probably get a ClassCastException (If two different type of views share the same Id).

    It would be impossible to maintain a ListView where every row would have to have unique identifiers for all its views.

    So, what this means is I can not inflate two Fragment in an Activity by using the same layout. This will result in an exception. Both are inflated by the same Activity, hence belong to the same instance. The ids will conflic

    No, fragments work in a different way. You have to return an inflated view in the fragments onCreateView() method. So each fragment has to inflate the view, which results in having 2 separate view objects.

提交回复
热议问题