动态隐藏Android中的自定义列表视图中的视图

时间:2012-08-14 09:53:34

标签: android listview dynamic view hide

我有一个自定义列表视图,其中我有Textview和Imageview 现在我想隐藏或显示Listview中的某些项目的Imageview。

我使用getview方法完成了这个,但问题是当第一次显示Listview时,View不会隐藏,但是当我向下滚动并向上滚动时,它会被隐藏。以下是代码段。提前谢谢。

 public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        ViewHolder holder;
        if (v != convertView && v != null) {
             holder = new ViewHolder();            
             convertView = mInflater.inflate(R.layout.jazzartist, null);
             holder.albumsView = (TextView)convertView.findViewById(R.id.artist_albums_textview);
             v.setTag(holder);


          }
        else{     
       holder = (ViewHolder) convertView.getTag();     }        

        ViewHolder holder1 = (ViewHolder) v.getTag();
        holder1.objimg =  (ImageView)convertView.findViewById(R.id.drag); 
        if(position == 4){  

            (holder1.objimg).setVisibility(View.INVISIBLE); // here I am hiding Imageview for position 4

            } 
        else
        {

            (holder1.objimg).setVisibility(View.VISIBLE); // here I am showing Imageview for rest of items
        }
                 String albums = getItem(position).albums;

        holder1.albumsView.setText(albums);

        return v;
      }
    }

    }

2 个答案:

答案 0 :(得分:1)

尝试以下代码。希望它有所帮助

            ViewHolder holder;
            if(convertView == null) 
            {
                 holder = new ViewHolder();            
                 convertView = mInflater.inflate(R.layout.jazzartist, null);
                 holder.albumsView = (TextView)convertView.findViewById(R.id.artist_albums_textview);
                 holder.objimg =  (ImageView)convertView.findViewById(R.id.drag); 
                 convertView.setTag(holder);
            }
            else
            {     
                holder = (ViewHolder) convertView.getTag();     
            } 

            if(position == 4)
            {
                holder.objimg.setVisibility(View.INVISIBLE); // here I am hiding Imageview for position 4
            } 
            else
            {
                holder.objimg.setVisibility(View.VISIBLE); // here I am showing Imageview for rest of items
            }

            String albums = getItem(position).albums;
            holder.albumsView.setText(albums);

            return convertView;

答案 1 :(得分:0)

请删除条件,即if else(不要检查convertView)

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

    ViewHolder holder;
           holder = new ViewHolder();            
         convertView = mInflater.inflate(R.layout.jazzartist, null);
         holder.albumsView = (TextView)convertView.findViewById(R.id.artist_albums_textview);


    holder1.objimg =  (ImageView)convertView.findViewById(R.id.drag); 
    if(position == 4){  

        (holder1.objimg).setVisibility(View.INVISIBLE); // here I am hiding Imageview for position 4

        } 
    else
    {

        (holder1.objimg).setVisibility(View.VISIBLE); // here I am showing Imageview for rest of items
    }
             String albums = getItem(position).albums;

    holder1.albumsView.setText(albums);

    return convertView;
  }