Reputation: 93
I have a custom ListView
which has a custom layout with a couple of views.
Can I check in the method onItemClick()
which of these views was clicked?
I searched and found one explanation but it couldn't help me a lot.
Here is the custom view:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mHolder;
if(convertView != null){
mHolder = (ViewHolder)convertView.getTag();
}else{
mHolder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.view_video_item,null);
mHolder.mVideoThumbnail = (ImageView)convertView.findViewById(R.id.video_thumbnail);
mHolder.mVideoTitle = (TextView)convertView.findViewById(R.id.video_title);
mHolder.mVideoFavorite = (ImageView)convertView.findViewById(R.id.video_favorite);
convertView.setTag(mHolder);
}
//Setting the data
SearchResult result = mVideoList.get(position);
mHolder.mVideoTitle.setText(result.getSnippet().getTitle());
//Loading the image
Picasso.with(mActivity).load(result.getSnippet().getThumbnails().getMedium().getUrl()).into(mHolder.mVideoThumbnail);
return convertView;
}
private class ViewHolder{
private TextView mVideoTitle = null;
private ImageView mVideoThumbnail = null;
//Testing
private ImageView mVideoFavorite = null;
}
I need to know if the mVideoFavorite
was clicked in this method:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SearchResult result = (SearchResult)parent.getItemAtPosition(position);
VIDEO_ID = result.getId().getVideoId();
Intent videoIntent = YouTubeStandalonePlayer.createVideoIntent(this, AppConstants.KEY, VIDEO_ID);
startActivity(videoIntent);
}
Any ideas how to do this?
Upvotes: 0
Views: 80
Reputation: 713
Basically, what you have to do is, make that imageview to capture onClick events by set its xml value :
android:clickable="true"
After you set that in your custom layout for the imageView called "mVideoFavorite", you must implement its onClick method as follow inside your getView method for custom adapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mHolder;
if(convertView != null){
mHolder = (ViewHolder)convertView.getTag();
}else{
mHolder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.view_video_item,null);
mHolder.mVideoThumbnail = (ImageView)convertView.findViewById(R.id.video_thumbnail);
mHolder.mVideoTitle = (TextView)convertView.findViewById(R.id.video_title);
mHolder.mVideoFavorite = (ImageView)convertView.findViewById(R.id.video_favorite);
mHolder.mVideoFavorite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// do your stuff
}
});
convertView.setTag(mHolder);
}
//Setting the data
SearchResult result = mVideoList.get(position);
mHolder.mVideoTitle.setText(result.getSnippet().getTitle());
//Loading the image
Picasso.with(mActivity).load(result.getSnippet().getThumbnails().getMedium().getUrl()).into(mHolder.mVideoThumbnail);
return convertView;
}
Basically this is what you have to at all. Regards,
Upvotes: 2