2014-09-11 87 views
0

我目前正在使我的應用程序的一部分,以使用GridView從圖庫中選擇多個圖像。 問題是我需要在第一個項目上放一個按鈕,其餘的將是圖像(0,0將是按鈕,其餘部分會很好,帶有複選框的imageview)該按鈕將使用手機攝像頭。我能做到,但作爲交換,它不能順利滾動,有沒有辦法解決這個問題?以下是我的代碼:Android Gridview平滑滾動按鈕

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

      final ViewHolder holder; 
      View view; 

      System.out.println(position); 




       if (position == 0) { 


        holder = new ViewHolder(); 
        view = getLayoutInflater().inflate(R.layout.year_dropdownlayout, parent, false); 

        holder.mButton = (Button) view.findViewById(R.id.mfreakingBtn); 
        ViewGroup.LayoutParams params = holder.mButton.getLayoutParams(); 
        params.height = 250; 
        params.width = 250; 
        holder.mButton.requestLayout(); 



       } else { 

        view = getLayoutInflater().inflate(R.layout.select_photos_layout, parent, false); 
        holder = new ViewHolder(); 

        holder.imageView = (ImageView) view.findViewById(R.id.imageView1); 
        holder.mCheckBox = (CheckBox) view.findViewById(R.id.checkBox1); 
        holder.mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener); 
        holder.progressBar = (ProgressBar) view.findViewById(R.id.progress); 
        holder.mButton = null; 
        view.setOnClickListener(new View.OnClickListener() { 

         @Override 
         public void onClick(View v) { 
          // TODO Auto-generated method stub 
          Toast.makeText(getApplicationContext(), "You Clicked " + position, Toast.LENGTH_LONG).show(); 
         } 
        }); 

        imageLoader.displayImage("file://" + imageUrls.get(position), holder.imageView, options, new SimpleImageLoadingListener() { 

           @Override 
           public void onLoadingStarted(String imageUri, View view) { 
            holder.progressBar.setProgress(0); 
            holder.progressBar.setVisibility(View.VISIBLE); 
           } 

           @Override 
           public void onLoadingFailed(String imageUri, View view, 
                  FailReason failReason) { 
            holder.progressBar.setVisibility(View.GONE); 
           } 

           @Override 
           public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
            Animation anim = AnimationUtils.loadAnimation(MultiPhotoSelect.this, R.anim.fade_in); 
            holder.imageView.setAnimation(anim); 
            anim.start(); 
            holder.progressBar.setVisibility(View.GONE); 
           } 
          }, new ImageLoadingProgressListener() { 
           @Override 
           public void onProgressUpdate(String imageUri, View view, int current, 
                  int total) { 
            holder.progressBar.setProgress(Math.round(100.0f * current/total)); 
           } 
          } 
        ); 

        holder.mCheckBox.setTag(position); 
        holder.mCheckBox.setChecked(mSparseBooleanArray.get(position)); 


       } 


      return view; 

     } 

不介意佈局的名稱。 :)當挫折感到雅時,會發生什麼。我一直在尋找幾個小時。我發現一個人在gridview的末尾放了一個按鈕:Android - Different items in GridView not showing up

我希望有人能幫助我。提前致謝。 :)

+0

這是因爲你總是膨脹新的視圖實例。檢查convertView實例是否爲null。如果爲空,那麼這是唯一一次應該誇大視圖的時間。 請參閱BaseAdapter.getItemViewType(),BaseAdapate.getItemViewTypeCount()。 看到這個:http://www.learn2crack.com/2014/01/android-custom-gridview.html – 2014-09-11 10:26:09

回答

1
Your adapter...  

private static final int ItemTypeButton = 0; 
private static final int ItemTypeImage = 1; 

@override 
public int getViewTypeCount() 
{ 
    return 2; // 2 because you want two type of view. (One button and the rest is images) 
} 

@override 
public int getItemViewType(int position) 
{ 
/* it's better to use item view type 
    to determine what type of view 
    you want to display. This is used 
    for recycling views for optimization. */ 
    return position == 0 ? ItemTypeButton : ItemTypeImage; 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 
    int type = getItemViewType(position); 

    /* Check whether convertView intance is null. If null, then inflate. 
     Otherwise the view is recycled for optimization. */ 
    if (convertView == null) 
    { 
     if (type == ItemTypeButton) 
     { 
     // inflate button view 
     } 
     else if (type == ItemTypeImage) 
     { 
     // inflate your view for image 
     } 
    } 

    // This is where you should update your view states. 
    if (type == ItemTypeImage) 
    { 
    /* ... */ 
    holder.imageView = (ImageView) view.findViewById(R.id.imageView1); 
    holder.imageView.setImage(..... 
    } 
    else 
    { 
    /* ... */ 
    } 
} 
+0

哇!只是....哇... :)謝謝 – JLONG 2014-09-11 11:03:11

+0

您的歡迎..不要忘記讚揚順便說一句.. :)祝你好運隊友.. – 2014-09-12 01:01:43