代码之家  ›  专栏  ›  技术社区  ›  Ads

如何实现视图保持架?

  •  11
  • Ads  · 技术社区  · 14 年前

    我正在使用一个取景器从动态数组适配器显示。它工作,但当我滚动列表时显示的数据变化不规则。 I want my list view to be populated only one,not all the time when I scroll my list. >any suggestion? 这是我的密码

    public view getview(int position,view convertview,viewgroup parent){
    
    //viewholder保留对子视图的引用,以避免不必要的调用
    //在每行上查找到findViewByID()。
    取景器支架;
    //当convertview不为空时,可以直接重用,不需要
    //重新充气。只有当ConvertView提供时,我们才会对新视图进行充气。
    //by listview为空。
    
    if(convertview==null){
    
    convertview=minflatter.inflat(r.layout.sample,空);
    //创建一个viewholder并存储对两个子视图的引用
    //我们要将数据绑定到。
    holder=新的viewholder();
    holder.name=(textview)convertview.findView.byid(r.id.text);
    holder.icon=(imageview)convertview.findViewByID(r.id.icon);
    convertview.settag(持有人);
    }其他{
    //返回视窗夹以快速访问textview
    //和ImageView。
    
    
    holder=(viewholder)convertView.gettag();
    
    }
    
    
    //将数据有效绑定到保持架上。
    如果(_first==true)
    {
    if(id<myelements.size())
    {
    holder.name.settext(myelements.get(id));
    holder.icon.setImageBitmap(micon1);
    ID++;
    }
    其他的
    {
    _第一个=错误;
    }
    }
    //holder.icon.setImageBitmap(micon2);
    *尝试{
    if(id<myelements.size())
    ID++;
    其他的
    {
    身份证;
    }
    }
    catch(异常E)
    {
    android.util.log.i(“callrestservice”,e.getmessage());
    **
    
    
    
    返回convertview;
    
    }
    静态类视角{
    文本视图名称;
    图像视图图标;
    }
    

    加载列表后,如下所示:http://i.stack.imgur.com/nrghr.png在滚动某些数据后http://i.stack.imgur.com/smbad.png它看起来像这样,如果我滚动到开头,它看起来像http://i.stack.imgur.com/0kjma.png<

    注意:我的名单必须按字母顺序排列它工作,但当我滚动列表时显示的数据不规则地变化。我希望列表视图只填充一次,而不是每次滚动列表时都填充一次。有什么建议吗? 这是我的密码

    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.pngalt text在滚动一些数据之后http://i.stack.imgur.com/sMbAD.pngalt text它看起来像这样,如果我再次滚动到开头,它看起来像http://i.stack.imgur.com/0KjMa.pngalt text

    附言:我的名单必须按字母顺序排列。

    2 回复  |  直到 12 年前
        1
  •  5
  •   istruble kalyan    12 年前

    ListView

    id getView()

    myElements.get(position);
    

    yourAdapter.notifyDataSetChanged()
    

        2
  •  27
  •   nbarraille    14 年前

    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;        
    }