如何实现一个观点持有者? [英] How to implement a view holder?

查看:105
本文介绍了如何实现一个观点持有者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是viewholder显示从动态arrayadapter.it作品,但数据显示的不规则变化,当我滚动列表。 我希望我的列表视图要填充一次,不是所有的时间当我滚动我的列表中。任何建议? 这是我的code

i am using a viewholder to display from a dynamic arrayadapter.it works but the data displayed changes irregularly when i scroll the List.i want my List View to be populated only once ,Not all the time when i scroll my list. Any suggestion? Here is my Code

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 滚动一些数据后的 http://i.stack.imgur.com/sMbAD.png 它看起来像这样,又如果我滚动到开始的时候看起来 HTTP://i.stack。 imgur.com/0KjMa.png

when the list is loaded it looks like this : it looks like this, and again if i scroll to the beginning it looks http://i.stack.imgur.com/0KjMa.png

PS:我的名单必须是按字母顺序

P.S : my list have to be in alphabetic order

推荐答案

您需要编写您自己版本的ListView 要做到这一点(这是坏的)。如果的ListView 不能正常工作,它可能意味着你正在做的事情是错误的。

You would need to write you own version of ListView to do that (which is bad). If the ListView doesn't work properly, it probably means that you are doing something wrong.

在什么地方 ID 元素从何而来?你得到你的 getView()方法的位置,所以你不必担心超出列表范围。位置链接到你的列表中的元素的位置,这样你就可以得到正确的元素是这样的:

Where does the id element come from? You are getting the position in your getView() method, so you don't need to worry about exceeding list bounds. The position is linked to the element position in your list, so you can get the correct element like this:

myElements.get(position);

当在列表中的数据发生变化,你可以调用这个:

When the data in your list changes, you can call this:

yourAdapter.notifyDataSetChanged()

这将重建与新数据的列表(同时保持你的滚动和东西)。

That will rebuild your list with new data (while keeping your scrolling and stuff).

这篇关于如何实现一个观点持有者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆