在gridview中按下onLongClick时会触发OnItemSelectedListener

时间:2016-04-20 05:20:26

标签: android gridview onitemclicklistener onlongclicklistener

我会尽量保持清醒。 我有一个显示图像的Gridview。用户可以在OnLongClick中选择多个图像。 onLongClick位于适配器中。该代码还有一个onItemSelectedListener,用于缩放特定位置的选定图像。 问题是在任何位置点击gridview后都没有触发onItemSelectedListener。当使用onLongClick时,它会触发OnItemSelectedListener和onLongClickListener方法。我尝试使用前面提供的解决方案,它在onLongClickListener中返回true,但这也没有用。我被困在这里。帮助将不胜感激。 提前谢谢。

    gvImages.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            PhotoBeans bean = (PhotoBeans) parent.getItemAtPosition(position);
            j = position;
            photoUrl = bean.getThumbImageUrl();
            Log.e("photoUrl", "" + photoUrl);
            zoomImageFromThumb(view, photoUrl);
        }
    });

//适配器

     public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private ArrayList<PhotoBeans> arrayListPhoto;


    public ImageAdapter(ArrayList<PhotoBeans> arrayListPhoto) {
        this.arrayListPhoto = arrayListPhoto;
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return arrayListPhoto.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayListPhoto.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
          public View getView(final int position, View convertView, ViewGroup parent) {
        View rootView = convertView;
        final ViewHolder holder;

        if (convertView == null) {

            holder = new ViewHolder();

            rootView = mInflater.inflate(R.layout.custom_gallery_item, null);

            holder.imgThumb = (ImageView) rootView.findViewById(R.id.imgThumb);
            holder.chkImage = (ImageView) rootView.findViewById(R.id.chkImage);
            rootView.setTag(holder);
        } else {
            holder = (ViewHolder) rootView.getTag();

        }

        if (arrayListPhoto.get(position).isImageSelected()) {
            holder.chkImage.setVisibility(View.VISIBLE);
            holder.imgThumb.setAlpha(0.5f);
        } else {
            holder.chkImage.setVisibility(View.GONE);
            holder.imgThumb.setAlpha(1f);
        }


        holder.imgThumb.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (arrayListPhoto.get(position).isImageSelected()) {
                    holder.chkImage.setVisibility(View.GONE);
                    holder.imgThumb.setAlpha(1f);
                    arrayListPhoto.get(position).setIsImageSelected(false);
                } else {
                    holder.chkImage.setVisibility(View.VISIBLE);
                    holder.imgThumb.setAlpha(0.5f);
                    arrayListPhoto.get(position).setIsImageSelected(true);
                }
                notifyDataSetChanged();

                return true;
            }
        });



        Picasso.with(Photos.this)
                .load(new File(arrayListPhoto.get(position).getThumbImageUrl()))
                .resize(150, 150)
                .centerCrop()
                .into(holder.imgThumb);

        return rootView;
    }
}

class ViewHolder {
    ImageView imgThumb;
    ImageView chkImage;
    int id;
}

1 个答案:

答案 0 :(得分:0)

在此活动中将代码放入此

   gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {***your code here**
                return false;
            }
        });