5

我遇到了ViewPagerClass 的问题,我有三个页面,每个页面都有一个scrollView,Relative Layout 和 ImageView 里面。

单击 ImageView 时,我需要设置彩色背景。我的问题是:当我单击一个ImageView以使其以颜色为背景时,背景颜色出现ImageView在另一个 PagerView 页面中的另一个上...

例如:我有四个页面,每个页面都加载了一个ImageView,当我单击第一页中的第一个 ImageView 时,第二ViewPager页中的 ImageView 将获得背景颜色..

我需要让我ImageView的第一页(在第一页)被背景着色..

这是代码...这是我的寻呼机适配器类

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

希望大家帮我解决这个问题。

4

2 回答 2

2

试试这个代码,它工作正常,

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

在里面instantiateItem(View collection, int position),因为您在视图寻呼机上使用的每个页面都有相同的图像,所以结果集中在最后启动的页面上。

于 2013-04-11T07:13:52.653 回答
1

@Sino Raj 即使可行,这也不是正确的方法。

试试这个 Declare ImageViewimage,image2 local insideinstantiateItem方法并在里面setOnClickListener将行更改image.setBackgroundColor(Color.RED)v.setBackgroundColor(Color.RED)

于 2013-04-11T08:04:55.527 回答