Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make ViewPager updates frequently?

Im facing a trouble with ViewPager Class, I have three pages each one has a scrollView, Relative Layout and ImageView inside.

I need to set a coloured background when i click on an ImageView. My Problem is: When I click on an ImageView to get it backgrounded with a colour, the background colour appears on another ImageView in another PagerView page...

For Instance: I have four pages each loaded with an ImageView, when i click on the first ImageView in the first page, the ImageView in the second ViewPager page is got the background colour..

I need to make my first ImageView(in the first page) to be backcoloured..

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's 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();
                 }
               });
            }
    }

Hope you guys help me solve this problem.

like image 732
blueware Avatar asked Oct 21 '22 12:10

blueware


2 Answers

Try this code,it works fine,

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

on inside of instantiateItem(View collection, int position), because you use each page on view pager has same images, so result has focus on last initiated page.

like image 98
Sino Raj Avatar answered Nov 01 '22 11:11

Sino Raj


@Sino Raj this is not the right way to do it even it works.

Try this Declare ImageView image,image2 locally inside instantiateItem method and inside setOnClickListener change the line image.setBackgroundColor(Color.RED) to v.setBackgroundColor(Color.RED)

like image 39
flexdroid Avatar answered Nov 01 '22 13:11

flexdroid