2012-08-14 139 views
0

我有,我有一個TextView和ImageView的 現在我想隱藏或在ListView ONY一些項目顯示ImageView的自定義列表視圖。在自定義列表視圖動態隱藏查看Android中

我已經完成了這個使用getview方法,但問題是,當第一次顯示列表視圖時,視圖不會隱藏,但當我向下滾動並向上滾動時,它會被隱藏。以下是代碼片段。提前致謝。

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

    } 
+0

不使用此行.. 視圖V = super.getView(位置,convertView,親本); 使用View v = convertView;如果 – SilentKiller 2012-08-14 10:13:03

+0

應用程序崩潰我刪除行 – kendrelaxman 2012-08-14 10:33:55

+0

嘗試通過Braj給出的代碼......也不錯的多數民衆贊成代碼.. – SilentKiller 2012-08-14 10:35:41

回答

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; 
+0

這也並沒有爲我工作。事實上,在向下滾動和向上滾動後,視圖不會隱藏。視圖保持不變。任何解決方案? – kendrelaxman 2012-08-14 10:45:16

+0

我做了一些小的改動。試一次 – Braj 2012-08-14 10:53:23

+0

偉大這工作for.I已接受的解決方案,請告訴我什麼是滾動時的問題?在4位的圖像位置後此溶液 – kendrelaxman 2012-08-21 04:38:33

0

請刪除的條件,即如果其他人(不把檢查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; 
    } 
+0

沒有工作。事實上,在向下滾動和向上滾動後,視圖不會隱藏。 – kendrelaxman 2012-08-14 10:39:51