How often do updates to ViewPager?

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) {
            // TODO Auto-generated method stub
            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);


            // Parameters
            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);
            // i.setim(imageArray[position]);



        /*  ImageView vv=(ImageView)collection.findViewById(R.id.imageView1);
        vv.setImageResource(R.drawable.aya2);
            TextView view = new TextView(activity);
            view.setTextIsSelectable(true);
            view.setTextSize(30);
            view.setTextColor(Color.BLUE);

            view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
            //view.setScaleType(ScaleType.FIT_XY);
            view.setText(imageArray[position]);
            ((ViewPager) collection).addView(view, 0);
            */
            container.addView(image);

            container.addView(image2);
            view.addView(container);

            image.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int location[] = new int[2];
//Here is the problem
                   image.setBackgroundColor(Color.RED);
//=======================================================
                   image.setTag("1");
                   image.getLocationOnScreen(location);
                   p = new Point();
                   p.x = location[0]-600;
                   p.y = location[1]+30;
                  // Toast.makeText(activity, String.valueOf(location[0]), 5000).show();
                  // Toast.makeText(activity, String.valueOf(location[1]), 5000).show();
                  showPopup(activity, p);

               notifyDataSetChanged();
                }
            });
            image2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int location[] = new int[2];

    //Here is the problem
                   image2.setBackgroundColor(Color.RED);
//=======================================================
                   image.getLocationOnScreen(location);
                   p = new Point();
                   p.x = location[0]-600;
                   p.y = location[1]+100;
                 //  Toast.makeText(activity, String.valueOf(location[0]), 5000).show();
                   //Toast.makeText(activity, String.valueOf(location[1]), 5000).show();
                  showPopup(activity, p);


                   notifyDataSetChanged();
                }
            });
            //container.addView(text);
            //container.addView(image);
            ((ViewPager) collection).addView(view,0);


            return view;
        }


        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            //Toast.makeText(activity.getApplicationContext(), String.valueOf(arg1), 5000).show();
            ((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() {
            // TODO Auto-generated method stub
            super.notifyDataSetChanged();
             if (this.mObserver != null)
                  this.mObserver.onDataSetChanged();
              }

        @Override
        public void startUpdate(ViewGroup container) {
            // TODO Auto-generated method stub
            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;

               // Inflate the popup_layout.xml
               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);

               // Creating the PopupWindow
               final PopupWindow popup = new PopupWindow(context);
               popup.setContentView(layout);
               popup.setWidth(popupWidth);
               popup.setHeight(popupHeight);
               popup.setFocusable(true);

               // Some offset to align the popup a bit to the right, and a bit down, relative to button position.
               int OFFSET_X = 30;
               int OFFSET_Y = 30;

               // Clear the default translucent background
               popup.setBackgroundDrawable(new BitmapDrawable());

               // Displaying the popup at the specified location, + offsets.
               popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

               // Getting a reference to Close button, and close the popup when clicked.
               ImageButton close = (ImageButton) layout.findViewById(R.id.tashgheel);
               close.setOnClickListener(new OnClickListener() {

                 @Override
                 public void onClick(View v) {
                   popup.dismiss();
                 }
               });
            }
    }

, , , .

+5
2

, ,

final ImageView image = new ImageView (activity);
final ImageView image2=new ImageView(activity);

instantiateItem(View collection, int position), , , .

+2

@Sino Raj , .

ImageView image, image2 instantiateItem setOnClickListener image.setBackgroundColor(Color.RED) v.setBackgroundColor(Color.RED)

+1

All Articles