如何检测Gridview中点击位置的imageview?

我在这里得到了GridViewAdapter的View方法。

我在这里想要的是,无论何时点击gridview中的项目,我点击的位置的图像应该缩小以便将其添加到购物车中。

但是,不要在单击位置缩小图像,而是缩小单元的其他图像(在滚动视图时从Viewholder创建的最后一个图像)。 任何帮助是极大的赞赏。 谢谢!!!

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        holder = null;

        Product productItem = (Product) productList.get(position);

        if (row == null) {


                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(R.layout.products_grid_item_layout, parent, false);
                holder = new ViewHolder();
                holder.productImage = (ImageView) row.findViewById(R.id.productImage);
                holder.detailsIcon = (ImageView) row.findViewById(R.id.detailsIcon);
                holder.productTitle = (TextView) row.findViewById(R.id.productTitle);
                holder.productSubTitle = (TextView) row.findViewById(R.id.productSubTitle);
                holder.productQuantity = (TextView) row.findViewById(R.id.productQuantity);
                holder.priceDollar = (TextView) row.findViewById(R.id.priceDollar);
                holder.priceCent = (TextView) row.findViewById(R.id.priceCent);
                holder.productCount = (TextView) row.findViewById(R.id.productCount);
                holder.productGridLayout = (RelativeLayout) row.findViewById(R.id.productGridLayout);

                row.setTag(holder);

        } else {
            holder = (ViewHolder) row.getTag();
        }



        String price = productItem.getPrice().toString();

        String[] pricearray;
        pricearray =  price.split(".");

        holder.productTitle.setText(productItem.getTitle().toString());
        holder.productSubTitle.setText(productItem.getSubtitle().toString());
        holder.productQuantity.setText(productItem.getVolume().toString());
        holder.priceDollar.setText(pricearray[0]+".");
        holder.priceCent.setText(pricearray[1]);


        if(productItem.getInCart()) {
            holder.productCount.setVisibility(View.VISIBLE);
            holder.productCount.setText(productItem.getVolume());
        }

        Picasso.with(context)
                .load(productItem.getImageUrl())
                .placeholder(R.drawable.favourites)
                .error(R.drawable.favourites)
                .into(holder.productImage);

        urlSlug = productItem.getUrlSlug();

        holder.detailsIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final int position = currentGridView.getPositionForView(v);
                Log.e("grid position", position+"");

                Intent productDetailIntent = new Intent(context,ProductDetailActivity.class);
                productDetailIntent.putExtra("url_slug", productList.get(position).getUrlSlug());

                context.startActivity(productDetailIntent);
            }

        });

    holder.productGridLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final int position = currentGridView.getPositionForView(v);
            Log.e("grid position", position+"");


            shrinkImage();
        }

    });


        return row;
    }



public void shrinkImage(){
        // first 0f, 1f mean scaling from X-axis to X-axis, meaning scaling from 0-100%
        // first 0f, 1f mean scaling from Y-axis to Y-axis, meaning scaling from 0-100%
        // The two 0.5f mean animation will start from 50% of X-axis & 50% of Y-axis, i.e. from center


        ScaleAnimation fade_in =  new ScaleAnimation(1f, 0f, 1f, 0f, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 1f);
        fade_in.setDuration(1000);     // animation duration in milliseconds
        fade_in.setFillAfter(true);    // If fillAfter is true, the transformation that this animation performed will persist when it is finished.
        holder.productImage.startAnimation(fade_in);

        Log.e("fade","fade out");
    }

您可以实现自己的自定义OnClickListener,并且可以将每个gridview项的每个onclick的id / tag / position关联起来。

item.setOnclickListener(new GridItemClick(position));

下面的自定义点击事件类:

Class GridItemClick implements View.OnClickListener
{
     int position;
     Public GridItemClick(int position)
     {
         this.position = position;
     }
     @Override
     public onClick(View v)
     { 
         // You can add logic here for each item clicked using position of each item you passed in constructor
     }

}

我在开始时遇到了同样的问题,为了解决这个问题,我如上所述为视图创建了自定义clickListener。


这是因为默认行为重新使用虚拟视图而发生的。 采取临时arraylist存储点击的位置,并验证相同的arraylist是否包含该特定位置或内部getView方法

ArrayList<String> selectedPosition = new ArrayList<String>();

现在按照下面的方法更新你的getView()方法:

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        holder = null;

        Product productItem = (Product) productList.get(position);

        if (row == null) {


                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(R.layout.products_grid_item_layout, parent, false);
                holder = new ViewHolder();
                holder.productImage = (ImageView) row.findViewById(R.id.productImage);
                holder.detailsIcon = (ImageView) row.findViewById(R.id.detailsIcon);
                holder.productTitle = (TextView) row.findViewById(R.id.productTitle);
                holder.productSubTitle = (TextView) row.findViewById(R.id.productSubTitle);
                holder.productQuantity = (TextView) row.findViewById(R.id.productQuantity);
                holder.priceDollar = (TextView) row.findViewById(R.id.priceDollar);
                holder.priceCent = (TextView) row.findViewById(R.id.priceCent);
                holder.productCount = (TextView) row.findViewById(R.id.productCount);
                holder.productGridLayout = (RelativeLayout) row.findViewById(R.id.productGridLayout);

                row.setTag(holder);

        } else {
            holder = (ViewHolder) row.getTag();
        }



        String price = productItem.getPrice().toString();

        String[] pricearray;
        pricearray =  price.split(".");

        holder.productTitle.setText(productItem.getTitle().toString());
        holder.productSubTitle.setText(productItem.getSubtitle().toString());
        holder.productQuantity.setText(productItem.getVolume().toString());
        holder.priceDollar.setText(pricearray[0]+".");
        holder.priceCent.setText(pricearray[1]);


        if(productItem.getInCart()) {
            holder.productCount.setVisibility(View.VISIBLE);
            holder.productCount.setText(productItem.getVolume());
        }

        Picasso.with(context)
                .load(productItem.getImageUrl())
                .placeholder(R.drawable.favourites)
                .error(R.drawable.favourites)
                .into(holder.productImage);

        urlSlug = productItem.getUrlSlug();


       //Added Change here...Check if arraylist contains selectedposition or not?

        if(selectedPosition.contains(String.valueOf(position)))
              shrinkImage(holder.productImage);
        else
           //Nothing



        holder.detailsIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final int position = currentGridView.getPositionForView(v);
                Log.e("grid position", position+"");

                Intent productDetailIntent = new Intent(context,ProductDetailActivity.class);
                productDetailIntent.putExtra("url_slug", productList.get(position).getUrlSlug());

                context.startActivity(productDetailIntent);
            }

        });

    holder.productGridLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //final int position = currentGridView.getPositionForView(v);
            //Log.e("grid position", position+"");

            //shrinkImage();


           //Simply store and check selectedPosition

           if(selectedPosition.contains(String.valueOf(position)))
               selectedPosition.remove(String.valueOf(position));
           else
               selectedPosition.add(String.valueOf(position));

           //And then update adapter
           notifyDataSetChanged();
        }

    });


        return row;
    }

更新你的shirnkImage方法并使用通过参数传递的持有者图像的引用。

public void shrinkImage(ImageView productImage){
        // first 0f, 1f mean scaling from X-axis to X-axis, meaning scaling from 0-100%
        // first 0f, 1f mean scaling from Y-axis to Y-axis, meaning scaling from 0-100%
        // The two 0.5f mean animation will start from 50% of X-axis & 50% of Y-axis, i.e. from center


        ScaleAnimation fade_in =  new ScaleAnimation(1f, 0f, 1f, 0f, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 1f);
        fade_in.setDuration(1000);     // animation duration in milliseconds
        fade_in.setFillAfter(true);    // If fillAfter is true, the transformation that this animation performed will persist when it is finished.
        productImage.startAnimation(fade_in);

        Log.e("fade","fade out");
    }
链接地址: http://www.djcxy.com/p/91313.html

上一篇: How to detect the imageview of clicked position in Gridview?

下一篇: How to get the GridView position while its checkbox item is checked?