Reputation: 991
I have a problem with my Pager Adapter. I have an ImageView
and TextView
in layout inflater. My adapter will show some Image and description of the Image. And I want to implement onClick
to the pager, when I click the Image, the TextView
will show/hide.
Below my snippet code :
private class MyAdapter extends PagerAdapter implements ViewPager.OnClickListener {
private ArrayList<Bitmap> images;
private LayoutInflater inflater;
private Context context;
private ViewPager viewPager;
private TextView textViewDescription;
private ImageView imageViewDescription;
public MyAdapter(Context context, ArrayList<Bitmap> images, ViewPager viewPager) {
this.context = context;
this.images = images;
this.viewPager = viewPager;
inflater = LayoutInflater.from(context);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return images.size();
}
@Override
public Object instantiateItem(ViewGroup view, final int position) {
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View page = inflater.inflate(R.layout.item_image_preview, null);
ImageView myImage = (ImageView) page.findViewById(R.id.imageView_preview);
TextView textView= (TextView) page.findViewById(R.id.textView_image_description);
textViewDescription = textView;
imageViewDescription = myImage;
imageDescription = arraydescription.get(position);
textView.setText(arraydescription.get(position));
myImage.setImageBitmap(images.get(position));
page.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if (isDescriptionVisible) {
textViewDescription.setVisibility(View.GONE);
} else {
textViewDescription.setVisibility(View.VISIBLE);
}
isDescriptionVisible = !isDescriptionVisible;
Log.i("TAG", "This page was clicked: " + position);
}
});
((ViewPager) view).addView(page, 0);
return page;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void onClick(View v) {
if (isDescriptionVisible) {
textViewDescription.setVisibility(View.GONE);
} else {
textViewDescription.setVisibility(View.VISIBLE);
}
isDescriptionVisible = !isDescriptionVisible;
}
}
The problem is: if the pager at position 2, and I click the Image, TextView
at position 2 will show/hide. But when I slide into position 1 and I click the Image, the TextView
which show/hide is still in position 2. Its like only one position that I can handle. I read a question about this example but it is still not working.
Upvotes: 0
Views: 567
Reputation: 21063
You need to save the state for each page . Try Code below . I have set the state as Tag.
public Object instantiateItem(ViewGroup view, final int position) {
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View page = inflater.inflate(R.layout.item_image_preview, null);
final ImageView myImage = (ImageView) page.findViewById(R.id.imageView_preview);
final TextView textView= (TextView) page.findViewById(R.id.textView_image_description);
imageDescription = arraydescription.get(position);
textView.setText(arraydescription.get(position));
myImage.setImageBitmap(images.get(position));
page.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
boolean isVisible=textView.getTag()==null?true:(boolean)textView.getTag();
if (isVisible) {
textView.setVisibility(View.GONE);
} else {
textView.setVisibility(View.VISIBLE);
}
textView.setTag(!isVisible);
Log.i("TAG", "This page was clicked: " + position);
}
});
((ViewPager) view).addView(page, 0);
return page;
}
Upvotes: 1