Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

android - Click ImageView within a ListView ListItem and get the position?

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 that I can launch my MapActivity instead and pass it the position.

Basically: Click the ImageView launch MapsActivity, otherwise click anywhere else on ListItem launch StopsScheduleActivity. In Both cases I need the position. How can I do this?

Here is my current onItemClick listener.

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

    Intent myIntent = new Intent(this, StopsScheduleActivity.class);
    myIntent.putExtra("stop", stops.get(position));

    startActivity(myIntent);

    Log.i("Winnipeg Bus", "Stop item #" + id + " clicked.");
}

Here is my ListView with a map for an ImageView that I want to listen to for clicks. Thanks!

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...