我有一个FrameLayout
。它包含两个viewGroup,一个是ViewPager
,另一个是RelativeLayout。我希望whatsapp像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
缩放也消失了。
答案 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
}
});