Android Gridview使用按钮平滑滚动

时间:2014-09-11 09:57:42

标签: java android button gridview

我目前正在制作应用程序的一部分,使用Gridview从图库中选择多个图像。 问题是我需要在第一个项目上放置一个按钮,其余的将是图像(0,0将是按钮,其余的很好,带有复选框的imageview)该按钮将使用手机相机。我能够做到,但作为交换,它不能顺利滚动,有没有办法解决这个问题?下面是我的代码:

 @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

我希望有人可以帮助我。提前致谢。 :)

1 个答案:

答案 0 :(得分: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
   {
     /* ... */
   }
}