0

我有一个使用 ViewPager 的 Activity,在这个 ViewPager 中是一个 TouchImageView(一个检测手势并相应地缩放/移动的 ImageView)。

现在我想在单击图像时更改 UI 中的某些内容,但是因为在 PagerAdapter 中声明了 TouchImageView,所以我无法访问它,如果我要在 PagerAdapter 的一侧设置一些东西,我就无法访问 UI我想更改的组件(当我开始工作时,我还想在我的应用程序的其他地方使用相同的适配器)。我已经尝试在 ViewPager 本身上设置一个 onClickListener ,但这不会触发。现在我真的不知道该怎么办。

这是适配器:

public class FullScreenImageAdapter extends PagerAdapter {

    private ArrayList<MediaFile> images;

    public FullScreenImageAdapter( ArrayList<MediaFile> images) {
        this.images = images;
    }

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

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        TouchImageView img = new TouchImageView(container.getContext());
        Bitmap bitmap = BitmapFactory.decodeFile(images.get(position).getPath());
        img.setImageBitmap(bitmap);
        container.addView(img, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT);
        return img;
    }

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

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

这是与之交互的方法(在 ProjectOverviewActivity 类中):

private void viewPicture(final int position) {
    View decorView = getWindow().getDecorView();
    statusBarVisibility = decorView.getSystemUiVisibility();
    // Hide the status bar.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
    } else {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    FullScreenImageAdapter imageAdapter = new FullScreenImageAdapter(mediaFiles);
    final ViewPager viewPager = (ViewPager) findViewById(R.id.image_pager);
    viewPager.setAdapter(imageAdapter);
    viewPager.setCurrentItem(position);
    View fancyBox = findViewById(R.id.fancyBox);
    fancyBox.setVisibility(View.VISIBLE);
    viewPager.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View btnClose = findViewById(R.id.fancyBoxBtnClose);
            View btnPrev = findViewById(R.id.fancyBoxBtnPrev);
            View btnNext = findViewById(R.id.fancyBoxBtnNext);
            if (overlayButtonsVisible) {
                btnClose.setVisibility(View.GONE);
                btnPrev.setVisibility(View.GONE);
                btnNext.setVisibility(View.GONE);
            } else {
                btnClose.setVisibility(View.VISIBLE);
                if (viewPager.getCurrentItem() > 0) {
                    btnPrev.setVisibility(View.VISIBLE);
                }
                if (viewPager.getCurrentItem() < (mediaFiles.size() - 1)) {
                    btnNext.setVisibility(View.VISIBLE);
                }
            }
            overlayButtonsVisible = !overlayButtonsVisible;

        }
    });

    View fancyBoxBtnClose = findViewById(R.id.fancyBoxBtnClose);
    final View fancyBoxBtnPrev = findViewById(R.id.fancyBoxBtnPrev);
    final View fancyBoxBtnNext = findViewById(R.id.fancyBoxBtnNext);
    fancyBoxBtnClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View decorView = getWindow().getDecorView();
            // Show the status bar.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                decorView.setSystemUiVisibility(statusBarVisibility);
            } else {
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, statusBarVisibility);
            }
            findViewById(R.id.fancyBox).setVisibility(View.GONE);
            setViewAndChildrenEnabled(findViewById(R.id.layoutProjectOverview), true);
            overlayButtonsVisible = false;
        }
    });
    fancyBoxBtnPrev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
            if (viewPager.getCurrentItem() == 0) {
                fancyBoxBtnPrev.setVisibility(View.GONE);
            }
        }
    });
    fancyBoxBtnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
            if (viewPager.getCurrentItem() == (mediaFiles.size() - 1)) {
                fancyBoxBtnNext.setVisibility(View.GONE);
            }
        }
    });
}
4

1 回答 1

0

你可以试试这个

更新:

我认为你应该创建你的自ViewPager定义GestureDetector

例子:

public class ClickableViewPager extends ViewPager {

    private OnItemClickListener mOnItemClickListener;

    public ClickableViewPager(Context context) {
        super(context);
        setup();
    }

    public ClickableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setup();
    }

    private void setup() {
        final GestureDetector tapGestureDetector = new    GestureDetector(getContext(), new TapGestureListener());

        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                tapGestureDetector.onTouchEvent(event);
                return false;
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            if(mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getCurrentItem());
            }
            return true;
    }
    }
}

然后在你的活动中

ClickableViewPager viewPager = (ClickableViewPager) findViewById(R.id.viewPager);
viewPager.setOnItemClickListener(new ClickableViewPager.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                // HADNLE YOUR CLICKS
            }
        });

资源

于 2016-05-25T12:35:02.237 回答