2012-04-05 13 views
0

私はGalleryを使用しており、ImageView,ImageButtonおよびProcessBarをバインドしています。ギャラリーには50以上の画像があり、ViewGroup.getChildCount()を撮ったときの画像はgetView()です。これは、3のint値だけを返します。ビューを取得すべての子要素をバインドしていません

ViewGroup.getChildCount()は、すべての画像がスクロールされても3を返すのはなぜですか?

だから私はこの方法では、エラーを取得しています:

ViewGroup.getChildAt(position).findViewById(R.id.progressbar_Horizontal).setVisibility(View.VISIBLE); 

主な活動

public class SliderActivity extends Activity { 

    private Integer[] mImageIds = { 
       R.drawable.sample_1, 
       R.drawable.sample_2, 
       R.drawable.sample_3, 
       R.drawable.sample_4, 
       R.drawable.sample_5, 
       R.drawable.sample_6, 
       R.drawable.sample_7 
     }; 
    /** Called when the activity is first created. */ 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main);    

     Gallery gallery = (Gallery) findViewById(R.id.gallery); 
     gallery.setAdapter(new ImageAdapter(this,mImageIds));  

    } 


} 

ImageAdapterクラス:

public class ImageAdapter extends BaseAdapter { 

    private int mGalleryItemBackground; 
    private Activity mContext; 
    private Integer[] Images; 
    private int[] ID; 
    private ProgressBar process; 
    private ImageButton imgBtn; 
    private ImageView imageview; 


    public ImageAdapter(Activity c,Integer[] images) { 
     this.mContext = c; 
     this.Images=images; 
     TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGal999lery); 
     mGalleryItemBackground = attr.getResourceId(
       R.styleable.HelloGal999lery_android_galleryItemBackground, 0); 
     attr.recycle(); 

     ID=new int[getCount()]; 
    } 

    public int getCount() { 
     return Images.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return 1; 
    } 



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


     View rowView = convertView; 

     if(rowView==null) 
     { 

     LayoutInflater inflater = mContext.getLayoutInflater(); 
     rowView = inflater.inflate(R.layout.test, null, true); 

     imageview=(ImageView) rowView.findViewById(R.id.ImageViewuser);  
     imgBtn=(ImageButton)rowView.findViewById(R.id.Download); 
     process=(ProgressBar)rowView.findViewById(R.id.progressbar_Horizontal); 

     imageview.setImageResource(Images[position]);    
     imageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
     imageview.setBackgroundResource(mGalleryItemBackground);   


     if(position==0) 
     { 
      if(ID[position]==1) 
      { 
       imgBtn.setVisibility(View.INVISIBLE); 
       process.setVisibility(View.VISIBLE); 
      }   
     } 

     if(ID[position]==position && position!=0) 
     { 
      imgBtn.setVisibility(View.INVISIBLE); 
      process.setVisibility(View.VISIBLE); 
     }    
     }              
     else{ 
      Log.e("Else", "Else case");  
     } 

     imgBtn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) {    
       // TODO Auto-generated method stub 
       if(position==0) 
       { 
        ID[position]=1;      
       } 
       else 
       { 
        ID[position]=position; 
       } 
       parent.getChildCount(); 
       Toast.makeText(mContext, position+"", Toast.LENGTH_SHORT).show(); 
       parent.getChildAt(position).findViewById(R.id.progressbar_Horizontal).setVisibility(View.VISIBLE);   
       BackgroundAsyncTask objDownload=new BackgroundAsyncTask(parent,position); 
       objDownload.execute(); 
      } 

     }); 
     return rowView;      
    } 



    private class BackgroundAsyncTask extends 
     AsyncTask<Void, Integer, Void> { 

     public BackgroundAsyncTask(ViewGroup vg,int position) {  
      this.subGroup=vg; 
      this.pos=position; 
     } 

    int myProgress; 
    private ViewGroup subGroup; 
    private int pos; 

     @Override 
     protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 


      subGroup.getChildAt(pos).findViewById(R.id.Download).setVisibility(View.VISIBLE);   
      subGroup.getChildAt(pos).findViewById(R.id.progressbar_Horizontal).setVisibility(View.INVISIBLE);    
      Toast.makeText(mContext,"Download finished "+pos, Toast.LENGTH_SHORT).show(); 

      ID[1]=0; 

     } 

     @Override 
     protected void onPreExecute() { 
     // TODO Auto-generated method stub   
     myProgress = 0; 
     imgBtn.setVisibility(View.INVISIBLE); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 
     while(myProgress<100){ 
     myProgress++; 
     publishProgress(myProgress); 
      SystemClock.sleep(100);    
     } 
    return null;    
} 

     protected void onProgressUpdate(Integer... values) { 

      process.setProgress(values[0]); 

     } 
    }  
} 
+1

を修正して、我々は、コードなしで立ち往生している:(私はミリアンペアで更新した – waqaslam

+0

を返信code..Please – Arun

答えて

0

あなたgetView

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


    View rowView = convertView; 

    if(rowView==null) 
    { 

    LayoutInflater inflater = mContext.getLayoutInflater(); 
    rowView = inflater.inflate(R.layout.test, null, true); 

    imageview=(ImageView) rowView.findViewById(R.id.ImageViewuser);  
    imgBtn=(ImageButton)rowView.findViewById(R.id.Download); 
    process=(ProgressBar)rowView.findViewById(R.id.progressbar_Horizontal); 
    } 

    imageview.setImageResource(Images[position]);    
    imageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
    imageview.setBackgroundResource(mGalleryItemBackground);   


    if(position==0) 
    { 
     if(ID[position]==1) 
     { 
      imgBtn.setVisibility(View.INVISIBLE); 
      process.setVisibility(View.VISIBLE); 
     }   
    } 

    if(ID[position]==position && position!=0) 
    { 
     imgBtn.setVisibility(View.INVISIBLE); 
     process.setVisibility(View.VISIBLE); 
    }    

    imgBtn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) {    
      // TODO Auto-generated method stub 
      if(position==0) 
      { 
       ID[position]=1;      
      } 
      else 
      { 
       ID[position]=position; 
      } 
      parent.getChildCount(); 
      Toast.makeText(mContext, position+"", Toast.LENGTH_SHORT).show(); 
      parent.getChildAt(position).findViewById(R.id.progressbar_Horizontal).setVisibility(View.VISIBLE);   
      BackgroundAsyncTask objDownload=new BackgroundAsyncTask(parent,position); 
      objDownload.execute(); 
     } 

    }); 
    return rowView;      
} 
0

Androidはこれを最適化します。 ListViewがあり、要素数が無限であるとします。 子カウントを照会すると、たとえば10個の子が戻されます。画面上に表示されているビューのみをアンドロイドが作成し、それらを再利用するために起こります。

詳細については、Adapter.getViewについて詳しくは、convertViewを参照してください。

関連する問題