Click ImageView within a ListView ListItem and get the position?

前端 未结 4 1635
时光取名叫无心
时光取名叫无心 2020-12-10 07:19

Currently, when the ListItem is clicked I grab its position and pass it to my StopsScheduleActiviy. I would like to detect when the ImageView of that ListItem is clicked so

4条回答
  •  -上瘾入骨i
    2020-12-10 07:52

    In you Custom Adapter getView() method , when you are init the ImageView. Add position as a tag for image view. So each time when new ImageView will appear it will hold its position in his tag. Then just add the OnClickListener() in getView().

    OnClickListener(View view) contains the view which was clicked by user. So when user will click any image view in the list. Then it will be passed to OnClickListener(View view) as a clicked view. And we know that our ImageView contains the position as a tag. So the tag can tell us that this ImageView is for ? position. :)

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            if(convertView == null){
                convertView = inflater.inflate(R.layout.list_row, parent, false);
            }
    
            ImageView imageView = (ImageView) convertView.findViewById(R.id.videoListImage);
            imageView.setTag(new Integer(position));
            imageView.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View view) {
                    Toast.makeText(mContext, "ImageView clicked for the row = "+view.getTag().toString(), Toast.LENGTH_SHORT).show();
                }
            });
    
            return convertView;
        }
    

    You will get the clicked ImageView position.

提交回复
热议问题