如何实现视图持有者?

时间:2010-11-10 14:41:37

标签: android listview view

我正在使用一个视图来显示动态arrayadapter.it但是当我滚动列表时显示的数据会不规则地变化。 我希望我的列表视图只能填充一次,而不是所有时间当我滚动我的列表时。 有什么建议吗? 这是我的代码

public View getView(int position, View convertView, ViewGroup parent) {            

   // A ViewHolder keeps references to children views to avoid unneccessary calls            
   // to findViewById() on each row.            
   ViewHolder holder;            
   // When convertView is not null, we can reuse it directly, there is no need            
   // to reinflate it. We only inflate a new View when the convertView supplied            
   // by ListView is null.            

   if (convertView == null) {                

    convertView = mInflater.inflate(R.layout.sample, null);   
    // Creates a ViewHolder and store references to the two children views                
    // we want to bind data to.               
    holder = new ViewHolder();                
    holder.name = (TextView) convertView.findViewById(R.id.text);               
    holder.icon = (ImageView) convertView.findViewById(R.id.icon);                
    convertView.setTag(holder);            
   } else {                
    // Get the ViewHolder back to get fast access to the TextView                
    // and the ImageView.


    holder = (ViewHolder) convertView.getTag();

   }            


   // Bind the data efficiently with the holder. 
   if(_first==true)
   {
   if(id<myElements.size())
   {
      holder.name.setText(myElements.get(id)); 
   holder.icon.setImageBitmap( mIcon1 );
   id++;
   }
   else
   {
    _first=false;
   }
   }
    //holder.icon.setImageBitmap(mIcon2);
   /*try{
    if(id<myElements.size())
     id++;
     else
     {
      id--;
     } 
   }
   catch(Exception e)
   {
    android.util.Log.i("callRestService",e.getMessage());
   }*/



   return convertView;

  }        
static class ViewHolder {            
 TextView name;            
 ImageView icon;        
}  

加载列表时,它看起来像这样:http://i.stack.imgur.com/NrGhR.png alt text滚动一些数据http://i.stack.imgur.com/sMbAD.png alt text后看起来像这样,如果我滚动到开头看起来http://i.stack.imgur.com/0KjMa.png alt text

P.S:我的清单必须按字母顺序排列

2 个答案:

答案 0 :(得分:27)

你试过这个吗?

public View getView(int position, View convertView, ViewGroup parent) {            

   // A ViewHolder keeps references to children views to avoid unneccessary calls            
   // to findViewById() on each row.            
   ViewHolder holder;            
   // When convertView is not null, we can reuse it directly, there is no need            
   // to reinflate it. We only inflate a new View when the convertView supplied            
   // by ListView is null.            

   if (convertView == null) {                

    convertView = mInflater.inflate(R.layout.sample, null);   
    // Creates a ViewHolder and store references to the two children views                
    // we want to bind data to.               
    holder = new ViewHolder();                
    holder.name = (TextView) convertView.findViewById(R.id.text);               
    holder.icon = (ImageView) convertView.findViewById(R.id.icon);                
    convertView.setTag(holder);            
   } else {                
    // Get the ViewHolder back to get fast access to the TextView                
    // and the ImageView.


    holder = (ViewHolder) convertView.getTag();

   }            


   // Bind the data efficiently with the holder. 
   holder.name.setText(myElements.get(id)); 
   holder.icon.setImageBitmap( mIcon1 );

   return convertView;
  }  

static class ViewHolder {            
    TextView name;            
    ImageView icon;        
} 

如果是,那它有什么问题?

我不认为一次加载所有行是个好主意。你最终会在内存中拥有大量无用的视图,这些视图会使应用程序无效。 视图上的视图和操作(如inflate,findViewById,getChild ..)都很昂贵,您应该尝试尽可能多地重用它们。这就是我们使用ViewHolders的原因。

答案 1 :(得分:5)

您需要编写自己的ListView版本来执行此操作(这很糟糕)。如果ListView无法正常工作,则可能意味着您做错了什么。

id元素来自哪里?您正在使用getView()方法获得该位置,因此您无需担心超出列表范围。该位置链接到列表中的元素位置,因此您可以获得正确的元素:

myElements.get(position);

当列表中的数据发生变化时,您可以将其命名为:

yourAdapter.notifyDataSetChanged()

这将使用新数据重建您的列表(同时保留您的滚动和内容)。

相关问题