BaseAdapter에서 getView () 방법에 대한 자세한 설명

이전 글에서 ListView 최적화 (BaseAdapter 사용) 의 예에서 getView () 는 잘 설명되지 않았다.이번에 단독으로 한 편을 써서 getView 방법을 상세하게 설명해 주십시오.
공식 API에서 getView에 대한 설명
public abstract View getView (int position, View convertView, ViewGroup parent)    Added in API level 1
Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use  inflate(int, android.view.ViewGroup, boolean)
 to specify a root view and to prevent attachment to the root.
Parameters
position The position of the item within the adapter's data set of the item whose view we want.
convertView The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists can specify their number of view types, so that this View is always of the right type (see getViewTypeCount()  and  getItemViewType(int) ).
parent The parent that this view will eventually be attached to
Returns
A View corresponding to the data at the specified position.
BaseAdapter를 최적화하지 않으면 List의 Item이 많을 때 리소스 낭비가 발생할 수 있습니다.
         public View getView(final int position, View convertView, ViewGroup parent) {  
             ViewHolder holder;  

            if (convertView == null) {  
                    convertView = mInflater.inflate(R.layout.item, null);  
                    holder = new ViewHolder();  
                    holder.title = (TextView) convertView.findViewById(R.id.ItemTitle);  
                    holder.text = (TextView) convertView.findViewById(R.id.ItemText);  
                    holder.bt = (Button) convertView.findViewById(R.id.ItemButton);  
                    convertView.setTag(holder);//  ViewHolder    
                   }  
    <span style="white-space:pre">	</span>    else{  
                    holder = (ViewHolder)convertView.getTag();//  ViewHolder    
                  }  
            //  TextView     ,              , <span style="font-family: Roboto, sans-serif;">getDate()        </span>
            holder.title.setText(getDate().get(position).get("ItemTitle").toString());  
            holder.text.setText(getDate().get(position).get("ItemText").toString());  

            return convertView;  
        }  

다음은 getView 최적화의 예입니다.
1. convertView는 재사용을 위한 것입니다.
시작
Activity
첫 번째 화면 표시
ListView
때,
convertView
0입니다.사용자가 아래로 스크롤
ListView
위 항목이 표시되지 않고 새 항목이 나타납니다.이때
convertView
더 이상 비어 있지 않고 일련의
convertView
의 값입니다.또 아래로 굴러갔을 때
11
행 컨테이너
22
가다
12
행 컨테이너
23
그래.즉
convertView
캐시와 같습니다.
0
항목이 보이지 않을 때, 항목은 데이터를 캐시하고, 다음에 나오는 항목은 데이터만 업데이트하면 된다. 이렇게 하면 시스템 자료의 비용을 크게 절약할 수 있다.
우리가 해야 할 일은 회수된view에 표시할 item을 채우는 것입니다.
2. setTag(Object), getTag(Object)
뷰의 setTag과 getTag는 뷰에 추가 데이터를 추가하기 위해 사용할 때 꺼낼 수 있습니다.
convertview가 첫 번째 전시라면 new의 ViewHolder와 연결되며, convertview가 이미 만들어졌으면holder 대상을 직접 꺼내서 새로운 데이터를 추가할 수 있습니다.

좋은 웹페이지 즐겨찾기