在按钮上隐藏/显示/单击时显示布局,而不影响图像视图缩放

时间:2018-06-28 07:57:50

标签: android android-viewpager

enter image description here 我从 Whatsapp 中显示的这张图片。

我有一个FrameLayout。它包含两个viewGroup,一个是ViewPager,另一个是RelativeLayout。我希望ImageView一样,当用户单击图像时,它会隐藏其他按钮,仅显示图像,反之亦然。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.bogdwellers.pinchtozoom.view.ImageViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black" />

<RelativeLayout
    android:layout_width="match_parent"
    android:id="@+id/rrr"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/share_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Share" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/sudoLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</RelativeLayout>

</FrameLayout>

编码:

RelativeLayout relativeLayout = v.findViewById(R.id.rrr);
    RelativeLayout sudoLayout = v.findViewById(R.id.sudoLayout);
    sudoLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (rrr.getVisibility() == View.VISIBLE){
                rrr.setVisibility(View.GONE);
            }
            else {
                rrr.setVisibility(View.VISIBLE);
            }
        }
    });

我在sudoLayout上尝试了onClick方法,但它会影响ViewPager。我无法向左或向右滑动,ImageView缩放也消失了。

1 个答案:

答案 0 :(得分:1)

我为我的Gallery App实现了相同的功能。作为解决方案,我使ViewPager可点击。为此,您必须先扩展它:

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

private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {

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

然后用XML将支持ViewPager替换为com.yourpackage.ClickableViewPager。然后像这样使用它:

mViewPager.setOnItemClickListener(new ClickableViewPager.OnItemClickListener() {
        @Override
        public void onItemClick() {
            // hide and show
        }
    });