BaseAdapter에서 getView () 방법에 대한 자세한 설명
3403 단어 최적화androidBaseAdapterListViewapi
공식 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 대상을 직접 꺼내서 새로운 데이터를 추가할 수 있습니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
깨끗한 것을 보고 싶기 때문에 최적화 함수의 벤치마크에 이용되는 함수의 가시화를 해 보았다결정되지 않음 (자기 만족) 「헤이 이런 거 있어」라고 생각하는 사람 최적화 함수란? 거친 이미지로 1) x + 10 = 25 2) x + 60 = 15 3) x + 45 = 60 의 x를 기계에 구할 때 정확하게 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.