如何实现视图持有者?

Ads*_*Ads 11 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;        
}  
Run Code Online (Sandbox Code Playgroud)

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

PS:我的清单必须按字母顺序排列

nba*_*lle 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;        
} 
Run Code Online (Sandbox Code Playgroud)

如果是的话,它有什么问题?

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


Ale*_*lov 5

你需要写自己的版本ListView才能做到这一点(这很糟糕).如果ListView不能正常工作,可能意味着你做错了什么.

id元素来自哪里?您正在获取getView()方法中的位置,因此您不必担心超出列表范围.该位置链接到列表中的元素位置,因此您可以获得正确的元素,如下所示:

myElements.get(position);
Run Code Online (Sandbox Code Playgroud)

当列表中的数据发生变化时,您可以调用:

yourAdapter.notifyDataSetChanged()
Run Code Online (Sandbox Code Playgroud)

这将使用新数据重建您的列表(同时保持您的滚动和东西).