如何实现视图持有者? [英] How to implement a view holder?

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

问题描述

我正在使用查看器从动态数组适配器中显示.它可以工作,但是当我滚动列表时显示的数据会不规则变化.我希望我的列表视图只填充一次,而不是所有时间当我滚动列表时. 有什么建议吗?这是我的代码

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/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

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

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天全站免登陆