2017年1月8日 星期日


Something about item view in Adapter code J

Item view is defined in XML file but there are something in Adapter code should be known so that it can be used efficiently and error free.



1.          View Recycling: For example, a list is scrolled forward and so the first item view is disappeared while new item view is inflated on the bottom. Android does not delete the disappeared item view, it is passed to AdapterView’s getView() method as “convertView”. Therefore in code, it is always found in the beginning of getView() like “if(convertView == null) …” which is refer as View Recycling.

Android provides View Recycling which improves efficiency, so it should be implemented. The only exceptional case that I know is: there are many different item view types. This is because even a convertView is present, it may be a wrong type and new view needs to be inflated. For this situation, you may remove the check condition “if(convertView == null)” and inflate item view directly.

2.          View Holder: View Recycling gives back the whole item view but we normally do not operate directly on it with data, e.g. the item view is a RelativeLayout contains TextViews and CheckBoxes. Therefore in the getView() method, there is a list of codes “convertView.findViewById(…)” to find the view elements and then put data into them. However findViewById() is considered as having long execution time, so to avoid doing this on every data item, we use a View Holder class. View Holder is a brief layout/framework of the item view (convertView). When item view is created for the first time, we create a View Holder and attached it to item view by setTag(). Therefore when the view is recycled, we can use getTag() to get back this brief layout/framework rather than doing a list of findViewById().

For AdapterView, View Holder is optional but Android has further developed the idea and make a new View type, RecyclerView. For this one, it is more flexible with complex layout but View Holder is necessary.

3.          AdapterView’s ChildView: Most of the time, an AdapterView only display part of the dataset on the screen. Those visible item views are referred as child view. For the first or last item view, even it may only partly displayed, is also count as a child. One common mistake on beginner: try to get a “child view” that is not visible on the screen.

4.         notifyDataSetChanged(): This is one of the method that renders all the visual views again in the AdapterView. In the beginning of development, it should be already be used after data changed. This is because the ListView may have some design changes during development. Use this method helps to avoid making mistakes. Only when the app is come to final stage and notifyDataSetChanged() is causing performance issue [since all views are rendered again and it may takes time], use editor to find out the word “notifyDataSetChanged()” and replace it with codes that modify corresponding views directly.

沒有留言:

張貼留言