Click ImageView within a ListView ListItem and get the position?

ε祈祈猫儿з 提交于 2019-11-28 10:18:40

For ImageView's Click You have to make a custom adapter for your listview.

And in adpater's getView() method just write a imageView's onClick(), And start your new activity from that imageview's onClick().

For custom list just look at this tutorial Android Series: Custom ListView items and adapters ..

Arslan Anwar

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.

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            System.out.println("getView " + position + " " + convertView);
            ViewHolder holder = null;

            if (convertView == null) {

                convertView = LayoutInflater.from(conText).inflate(
                        R.layout.gridgoggle, parent, false);
                holder = new ViewHolder();

                convertView.setTag(holder);

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


            holder.imageView1.setImageResource(Integer.parseInt((mData
                    .get(position).get("image"))));

            holder.imageView1.setId(position);

            holder.imageView1.setOnClickListener(new OnClickListener() {
                public void onClick(View v)
 {

                                   int id = v.getId();
//here id is position.

  Intent myIntent = new Intent(this, StopsScheduleActivity.class);
  myIntent.putExtra("stop", id);

 startActivity(myIntent);


                }
            });
            return convertView;
        }

In three simple steps:

1 In your adapter method getView(int position, View convertView, ViewGroup parent):

ImageView imageview =(ImageView) convertView.findViewById(R.id.imageView);
imageview.setTag(new Integer(position));

2 In layout.xml add the method onClick() on the ImageView

3 implements onClick() method and retrieves the index by view.getTag()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!