I am trying to create a gridView of scrollable images for my android app.Each Grid consist of imageView. When one clicks on the image, then it blurs the image and present Play & share options.I am using the ArrayAdapter with holder pattern to set the view.
Problem Statement:-
I am handling click event on BlurLayout in the Adapter. On click i play the video related to image in youtube player. The problem is that in getView of ArrayAdapter, value of position is coming incorrectly. Thus it plays a wrong video. My observation is that position value is correct for 0-2 and it repeats.
Code Snippet:-
public class TrendingAdapter extends ArrayAdapter<TrendingData> {
private AdapterCallback mAdapterCallback;
final private DbHandler db;
public TrendingAdapter(Context context, int resource, AdapterCallback callback) {
super(context, resource);
this.mAdapterCallback = callback;
this.db = new DbHandler(getContext());
}
//hover image
private BlurLayout mSampleLayout ;
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
if( convertView == null ) {
holder = new ViewHolder();
convertView = LayoutInflater.from( getContext() ).inflate( R.layout.activity_trending_adapter, parent, false );
holder.image = (ImageView) convertView.findViewById( R.id.image );
holder.caption=(TextView) convertView.findViewById(R.id.textView);
convertView.setTag(holder);
BlurLayout.setGlobalDefaultDuration(450);
mSampleLayout = (BlurLayout)convertView.findViewById(R.id.blur_layout);
final View hover = LayoutInflater.from(getContext()).inflate(R.layout.hover_sample, null);
holder.play = (ImageView)hover.findViewById(R.id.heart);
holder.playList = (ImageView)hover.findViewById(R.id.share);
hover.findViewById(R.id.play).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
YoYo.with(Techniques.Tada)
.duration(550)
.playOn(v);
mAdapterCallback.onMethodCallback(getItem(position).getImage(), getItem(position).getCaption(),getItem(position).getBgImage());
}
});
hover.findViewById(R.id.playlist).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
YoYo.with(Techniques.Swing)
.duration(550)
.playOn(v);
String date = new SimpleDateFormat("dd-MM-yy").format(new Date());
db.addContact(new PlayListData(getItem(position).getThumbnail(), getItem(position).getCaption(),date,getItem(position).getImage()));
}
});
mSampleLayout.setHoverView(hover);
mSampleLayout.setBlurDuration(550);
mSampleLayout.addChildAppearAnimator(hover, R.id.heart, Techniques.FlipInX, 550, 0);
mSampleLayout.addChildAppearAnimator(hover, R.id.share, Techniques.FlipInX, 550, 250);
mSampleLayout.addChildAppearAnimator(hover, R.id.more, Techniques.FlipInX, 550, 500);
mSampleLayout.addChildDisappearAnimator(hover, R.id.heart, Techniques.FlipOutX, 550, 500);
mSampleLayout.addChildDisappearAnimator(hover, R.id.share, Techniques.FlipOutX, 550, 250);
mSampleLayout.addChildDisappearAnimator(hover, R.id.more, Techniques.FlipOutX, 550, 0);
mSampleLayout.addChildAppearAnimator(hover, R.id.description, Techniques.FadeInUp);
mSampleLayout.addChildDisappearAnimator(hover, R.id.description, Techniques.FadeOutDown);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ViewHolder tmp = holder;
Picasso.with(getContext()).load(getItem(position).getThumbnail())
.placeholder(getContext().getResources().getDrawable(R.drawable.place)).
error(getContext().getResources().getDrawable(R.drawable.place)).
into(holder.image);
return convertView;
}
private class ViewHolder {
ImageView image;
TextView caption;
ImageView play;
ImageView playList;
}
}
The Value of position is incorrect in:-
mAdapterCallback.onMethodCallback(getItem(position).getImage(), getItem(position).getCaption(),getItem(position).getBgImage());
db.addContact(new PlayListData(getItem(position).getThumbnail(), getItem(position).getCaption(),date,getItem(position).getImage()));
I don't how to get the correct value of position so that i can play the correct video against image (Play button in BlurLayout) on which the click event happens.
Please help to resolve the problem.
Edit 1:- Adding TrendingData Class
public class TrendingData {
private String thumbnail;
private String image;
private String caption;
private String bg_image;
public TrendingData() {
}
public TrendingData( String thumbnail, String image, String caption, String bg_image ) {
//this.thumbnail = thumbnail;
this.thumbnail = bg_image;
this.image = image;
this.caption = caption;
this.bg_image = bg_image;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
public String getImage() {
return image;
}
public String getBgImage() {
return bg_image;
}
public void setImage(String image) {
this.image = image;
}
public void setBgImage(String bg_image) {
this.bg_image = bg_image;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
}
Please help i am stuck for days now and I have tried everything i can find.