如何在 ViewPager 中检测单击 TouchImageView?

How to detect single tap on TouchImageView in ViewPager?

我制作了一个应用程序,其中我在 viewpager 中显示图像。我正在使用 Universal 图像加载器。所有图像都加载正常,我可以放大和缩小,因为我添加了 TouchImagevIew 作为 imageview。现在,当我在屏幕上单击时,我想显示一些按钮,例如 sharedelete。我无法检测到单点触摸手势。为什么?

ViewPager 代码

public class ImageViewPager extends Activity {
        // Declare Variable
        int position;
        Button delete, gimmy, done;
        ViewPager viewpager;
        ImagePagerAdapter pageradapter;
        TouchImageView imageView;
        TextView text;
        Runnable r;
        Handler mHandler;
        RelativeLayout rel;
        GestureDetector gestureDetector;

        @SuppressLint("NewApi")
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Set title for the ViewPager
            setContentView(R.layout.view_pager);
            mHandler=new Handler();
            delete=(Button) findViewById(R.id.del);
            done=(Button) findViewById(R.id.done);
            gimmy=(Button) findViewById(R.id.gimmy);
            rel=(RelativeLayout)findViewById(R.id.relative);
            ActionBar bar = getActionBar();
            bar.setDisplayHomeAsUpEnabled(true);
            bar.setHomeButtonEnabled(true);
            setTitle("ViewPager");
            Intent p = getIntent();
            position = p.getExtras().getInt("id");
            pageradapter = new ImagePagerAdapter(ImageViewPager.this);
            viewpager = (ViewPager) findViewById(R.id.pager);
            viewpager.setAdapter(pageradapter);
            viewpager.setCurrentItem(position);
              r=new Runnable() {
                  @Override
                  public void run() {
                     // TODO Auto-generated method stub
                     delete.setVisibility(View.GONE);
                     done.setVisibility(View.GONE);
                     gimmy.setVisibility(View.GONE);
                  }    
                };
              mHandler.postDelayed(r,5*1000);
              done.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                         Intent p = new Intent(getApplicationContext(), MainActivity.class);
                         startActivity(p);
                    }
                });

             gimmy.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                    }
                });

             delete.setOnClickListener(new OnClickListener() {


                    @Override
                    public void onClick(View v) {

                        }
                    });

             viewpager.setOnPageChangeListener(new OnPageChangeListener() {


                    @Override
                    public void onPageSelected (int page)
                    {
                         delete.setVisibility(View.GONE);
                         done.setVisibility(View.GONE);
                         gimmy.setVisibility(View.GONE);
                    }

                    @Override
                    public void onPageScrolled (int arg0, float arg1, int arg2)
                    {

                    }

                    @Override
                    public void onPageScrollStateChanged (int arg0)
                    {
                    }

                });


        }


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // TODO Auto-generated method stub

            switch (item.getItemId()) {
            case android.R.id.home:

                finish();
                overridePendingTransition(R.anim.slide_enter, R.anim.slide_exit);
                return true;

            default:
                return super.onOptionsItemSelected(item);

            }

        }


        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                Intent p = new Intent(getApplicationContext(), SlideImages.class);
                // p.putExtra("id", pos+1);
                startActivity(p);
            }

            return super.onKeyDown(keyCode, event);
        }
}

寻呼机适配器代码

public class ImagePagerAdapter extends PagerAdapter {
    LayoutInflater inflater;
    PhotoViewAttacher attacher;
    private DisplayImageOptions options;
    private  List<Wallpaper>  IMAGES_LIST=AppController.getInstance().getPrefManger().getAllImages();


        public ImagePagerAdapter(Context context) 
        {
                inflater = LayoutInflater.from(context);

                options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.ic_empty)
                .showImageOnFail(R.drawable.ic_error)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new FadeInBitmapDisplayer(300))
                .build();
        }

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        View imageLayout = inflater.inflate(R.layout.item_pager_image, container, false);
        assert imageLayout != null;
        TouchImageView imageView = (TouchImageView) imageLayout.findViewById(R.id.image);
        final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);


        ImageLoader.getInstance().displayImage(IMAGES_LIST.get(position).getUrl(), imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                spinner.setVisibility(View.VISIBLE);
                 view.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                String message = null;
                switch (failReason.getType()) {
                    case IO_ERROR:
                        message = "Input/Output error";
                        break;
                    case DECODING_ERROR:
                        message = "Image can't be decoded";
                        break;
                    case NETWORK_DENIED:
                        message = "Downloads are denied";
                        break;
                    case OUT_OF_MEMORY:
                        message = "Out Of Memory error";
                        break;
                    case UNKNOWN:
                        message = "Unknown error";
                        break;
                }
                Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();

                spinner.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                spinner.setVisibility(View.GONE);
                view.setVisibility(View.VISIBLE);
                Log.i("RAE", "Loading Completed");


            }
        });

        container.addView(imageLayout, 0);
        return imageLayout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return IMAGES_LIST.size();
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }
}

item_pager_image.xml

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="1dip">

    <info.androidhive.slidingmenu.util.TouchImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:contentDescription="@string/app_name" />

    <ProgressBar
        android:id="@+id/loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone" />

</FrameLayout>

我遇到了同样的问题 problem.Just 调用单击确认方法 & 隐藏或显示你对此的看法 & 还将寻呼机适配器添加到同一个文件中。

代码:

  public class ImageViewPager extends Activity {
    // Declare Variable
    int position;
    Button delete, gimmy, done;
    ViewPager viewpager;
    ImagePagerAdapter pageradapter;
    TouchImageView imageView;
    TextView text;
    Runnable r;
    Handler mHandler;
    RelativeLayout rel;
    GestureDetector gestureDetector;

    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set title for the ViewPager
        setContentView(R.layout.view_pager);
        mHandler=new Handler();
        delete=(Button) findViewById(R.id.del);
        done=(Button) findViewById(R.id.done);
        gimmy=(Button) findViewById(R.id.gimmy);
        rel=(RelativeLayout)findViewById(R.id.relative);
        ActionBar bar = getActionBar();
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setHomeButtonEnabled(true);
        setTitle("ViewPager");
        Intent p = getIntent();
        position = p.getExtras().getInt("id");
        pageradapter = new ImagePagerAdapter(ImageViewPager.this);
        viewpager = (ViewPager) findViewById(R.id.pager);
        viewpager.setAdapter(pageradapter);
        viewpager.setCurrentItem(position);
          r=new Runnable() {

              @Override
              public void run() {
                 // TODO Auto-generated method stub
                 delete.setVisibility(View.GONE);
                 done.setVisibility(View.GONE);
                 gimmy.setVisibility(View.GONE);
             }

            };
            mHandler.postDelayed(r,5*1000);
          done.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                     Intent p = new Intent(getApplicationContext(), MainActivity.class);
                     startActivity(p);
                }
            });

         gimmy.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                }
            });

         delete.setOnClickListener(new OnClickListener() {


                @Override
                public void onClick(View v) {

                    }
                });

         viewpager.setOnPageChangeListener(new OnPageChangeListener() {


                @Override
                public void onPageSelected (int page)
                {
                     delete.setVisibility(View.GONE);
                     done.setVisibility(View.GONE);
                     gimmy.setVisibility(View.GONE);
                }

                @Override
                public void onPageScrolled (int arg0, float arg1, int arg2)
                {

                }

                @Override
                public void onPageScrollStateChanged (int arg0)
                {
                }

            });


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {
        case android.R.id.home:

            finish();
            overridePendingTransition(R.anim.slide_enter, R.anim.slide_exit);
            return true;

        default:
            return super.onOptionsItemSelected(item);

        }

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent p = new Intent(getApplicationContext(), SlideImages.class);
            // p.putExtra("id", pos+1);
            startActivity(p);
        }

        return super.onKeyDown(keyCode, event);
    }

/**
 * This is adapter class for the viuew pager.we have used this in same file baecause reference is lost in another file.
 * **/
    public class ImagePagerAdapter extends PagerAdapter {
        LayoutInflater inflater;
        PhotoViewAttacher attacher;
        private DisplayImageOptions options;
        private  List<Wallpaper>  IMAGES_LIST=AppController.getInstance().getPrefManger().getAllImages();


            public ImagePagerAdapter(Context context) 
            {
                    inflater = LayoutInflater.from(context);

                    options = new DisplayImageOptions.Builder()
                    .showImageForEmptyUri(R.drawable.ic_empty)
                    .showImageOnFail(R.drawable.ic_error)
                    .resetViewBeforeLoading(true)
                    .cacheOnDisk(true)
                    .imageScaleType(ImageScaleType.EXACTLY)
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .considerExifParams(true)
                    .displayer(new FadeInBitmapDisplayer(300))
                    .build();
            }

        @Override
        public Object instantiateItem(ViewGroup container, int position)
        {
            View imageLayout = inflater.inflate(R.layout.item_pager_image, container, false);
            assert imageLayout != null;
            TouchImageView imageView = (TouchImageView) imageLayout.findViewById(R.id.image);
            final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
            imageView.setOnDoubleTapListener(new OnDoubleTapListener() {

                @Override
                public boolean onSingleTapConfirmed(MotionEvent e) {
                    // TODO Auto-generated method stub
                    delete.setVisibility(View.VISIBLE);
                    done.setVisibility(View.VISIBLE);
                    gimmy.setVisibility(View.VISIBLE);
                    return false;
                }

                @Override
                public boolean onDoubleTapEvent(MotionEvent e) {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public boolean onDoubleTap(MotionEvent e) {
                    // TODO Auto-generated method stub
                    return false;
                }
            });

            ImageLoader.getInstance().displayImage(IMAGES_LIST.get(position).getUrl(), imageView, options, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    spinner.setVisibility(View.VISIBLE);
                     view.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    String message = null;
                    switch (failReason.getType()) {
                        case IO_ERROR:
                            message = "Input/Output error";
                            break;
                        case DECODING_ERROR:
                            message = "Image can't be decoded";
                            break;
                        case NETWORK_DENIED:
                            message = "Downloads are denied";
                            break;
                        case OUT_OF_MEMORY:
                            message = "Out Of Memory error";
                            break;
                        case UNKNOWN:
                            message = "Unknown error";
                            break;
                    }
                    Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();

                    spinner.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    spinner.setVisibility(View.GONE);
                    view.setVisibility(View.VISIBLE);
                    Log.i("RAE", "Loading Completed");


                }
            });

            container.addView(imageLayout, 0);
            return imageLayout;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getCount() {
            return IMAGES_LIST.size();
        }


        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }
    }


}