Im facing a problem with ViewPagerClass, I have three pages, each of which has scrollView, relative layout and ImageView inside.
I need to set the color background when I click on ImageView. My problem: when I click on ImageViewto get a background image with color, the background color appears on another page ImageViewon another PagerView page ...
For instance: I have four pages, each of which is loaded using ImageView, when I click on the first ImageView on the first page, the ImageView on the second page ViewPagergets the background color.
I need to make my first ImageView(on the first page) so that it is framed.
Here is the code ... This is my pager adapter class
public class ViewPagerAdapter extends PagerAdapter {
private DataSetObserver mObserver;
ImageView image,image2;
Activity activity;
int imageArray[];
Point p;
public ViewPagerAdapter(Activity act, int[] imgArra) {
imageArray = imgArra;
activity = act;
}
@Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
int fff=PageIndicatorActivity.myPager.getCurrentItem();
Toast.makeText(activity, String.valueOf(fff), 5000).show();
ScrollView view = new ScrollView(activity);
RelativeLayout container = new RelativeLayout(activity);
image = new ImageView (activity);
image2=new ImageView(activity);
image.setId(1);
LayoutParams container_params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
LayoutParams content_params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams contents_paramw = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
contents_paramw.addRule(RelativeLayout.BELOW, image.getId());
view.setLayoutParams(container_params);
container.setLayoutParams(container_params);
image.setLayoutParams(content_params);
image2.setLayoutParams(contents_paramw);
image2.setImageResource(R.drawable.aya3);
image.setImageResource(R.drawable.aya2);
container.addView(image);
container.addView(image2);
view.addView(container);
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int location[] = new int[2];
image.setBackgroundColor(Color.RED);
image.setTag("1");
image.getLocationOnScreen(location);
p = new Point();
p.x = location[0]-600;
p.y = location[1]+30;
showPopup(activity, p);
notifyDataSetChanged();
}
});
image2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int location[] = new int[2];
image2.setBackgroundColor(Color.RED);
image.getLocationOnScreen(location);
p = new Point();
p.x = location[0]-600;
p.y = location[1]+100;
showPopup(activity, p);
notifyDataSetChanged();
}
});
((ViewPager) collection).addView(view,0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((ScrollView) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((ScrollView) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
if (this.mObserver != null)
this.mObserver.onDataSetChanged();
}
@Override
public void startUpdate(ViewGroup container) {
super.startUpdate(container);
}
static abstract interface DataSetObserver
{
public abstract void onDataSetChanged();
}
private void showPopup(final Activity context, Point p) {
int popupWidth = 560;
int popupHeight = 80;
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
int OFFSET_X = 30;
int OFFSET_Y = 30;
popup.setBackgroundDrawable(new BitmapDrawable());
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
ImageButton close = (ImageButton) layout.findViewById(R.id.tashgheel);
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
}
});
}
}
, , , .