2015-02-07 2 views
0

목록에서 성능을 위해 행을 재활용하는 방법을 알고 있습니다. 내가 주로 참조하는 것은 예를 들어 static 클래스와 태그 (viewHolder)안드로이드에서리스트 행 뷰를 재활용하는 두 가지 기술의 차이점

를 사용하는 기술의 종류 :

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    ViewHolder holder = null; 

    if (convertView == null) 
    { 
     convertView = mInflater.inflate(R.id.row, null); 
     holder = new ViewHolder(); 
     holder.txt1 = (TextView) convertView.findViewById(R.id.txt1); 
     holder.txt2 = (TextView) convertView.findViewById(R.id.txt2); 
     holder.txt3 = (TextView) convertView.findViewById(R.id.txt3); 
// setting more images,images 
     convertView.setTag (holder); 
    } 
    else 
    { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.txt1.setText(data.get(position).txt1); 
    holder.txt2.setText(data.get(position).txt2); 
    holder.txt3.setText(data.get(position).txt3); 

    return convertView; 
} 

static class ViewHolder{ 
    TextView txt1; 
    TextView txt2; 
    TextView txt3; 
} 

하지만 일부 코드에서 내가 static 클래스와 태그를 사용하지 않는 더 간단한 방법을 보았다

,보기를 재활용했는지 확인합니다. 예이면보기를 재활용하고 그렇지 않으면보기 만 사용합니다.

public View getView(int position, View convertView, ViewGroup parent) { 
       if(convertView == null){ 
        convertView = LayoutInflater.from(getActivity()).inflate(R.layout.history_row, null); 
       } 
     LinearLayout row = (LinearLayout) convertView.findViewById(R.id.row); 

     TextView txt1 = (TextView) convertView.findViewById(R.id.txt1); 
        txt1.setText(data.getTxt1()); 
    TextView txt2 = (TextView) convertView.findViewById(R.id.txt1); 
        txt2.setText(data.getTxt2()); 
    TextView txt3 = (TextView) convertView.findViewById(R.id.txt2); 
        txt3.setText(data.getTxt3()); 

    } 

무엇이 다른가요? 사용하는 것이 더 낫지 않으십니까?

답변

0

findViewById()은 값 비싼 전화입니다. 하나는 그것을 사용하지 말아야한다. 첫 번째 접근 방식에서는 새로 생성 된 모든 뷰에 대해 findViewById()이 호출되고 convertView는 호출되지 않습니다. 두 번째 접근 방식에서는 모든 뷰에 대해 findViewById()이 호출됩니다. 첫 번째 접근법은 의심의 여지없이 사용하는 것이 좋습니다.

관련 문제