简体   繁体   English

在Android的自定义Listview中动态隐藏视图

[英]Dynamically hiding Views in Custom Listview in Android

I have a custom listview in which I have a Textview and Imageview now I want to hide or display the Imageview for some Items in the Listview ony. 我有一个自定义的listview,其中现在有一个Textview和Imageview,我想隐藏或显示Listview ony中某些项目的Imageview。

I have done this using getview method but the problem is that when the Listview is displayed at first time the View does not get hide but when I scroll down and scroll up that time it gets hidden. 我已经使用getview方法完成了此操作,但是问题是,当第一次显示Listview时,View不会被隐藏,但是当我向下滚动并向上滚动时,它会被隐藏。 following is the code snippet. 以下是代码段。 Thanks in advance. 提前致谢。

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

    }

Try below code. 尝试下面的代码。 Hope it helps 希望能帮助到你

            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;

Please remove the conditions ie if else (don't put check for convertView) 请删除条件,即是否其他条件(不要对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;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM