【问题标题】:Show hide layout on imageview在 imageview 上显示隐藏布局
【发布时间】:2018-06-25 20:41:56
【问题描述】:

我有一个浏览器,我在里面显示图像。支持捏缩放和双击缩放。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearLayoutViewpager">


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


</LinearLayout>

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

    <Button
        android:id="@+id/save_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Save" />



   </RelativeLayout>
// Sudo layout, working as button
  <RelativeLayout
android:id="@+id/relativelayoutforvisibility"
android:layout_width="match_parent"
android:layout_height="match_parent">

我希望当用户打开 viewpager 时,它会显示完整的图像,如果用户单击图像,则应该从另一个(RelativeLayout)布局中出现一些按钮。 我创建了一个额外的布局并在其上设置了 OnClickListener。

  viewPager = (ImageViewPager) v.findViewById(R.id.viewpager);
    final RelativeLayout relativeLayout = (RelativeLayout) 
  v.findViewById(R.id.relativelayoutforbuttons);
    relativeLayout.setVisibility(View.GONE);
    RelativeLayout relativelayoutforvisibility =  (RelativeLayout) 
v.findViewById(R.id.relativelayoutforvisibility);
  relativelayoutforvisibility.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(relativeLayout.getVisibility() == View.VISIBLE)
            {
                relativeLayout.setVisibility(View.GONE);
            }
            // Show layouts if they're not VISIBLE
            else
            {
                relativeLayout.setVisibility(View.VISIBLE);
            }
        }
    });
   myViewPagerAdapter = new MyViewPagerAdapter();
    viewPager.setAdapter(myViewPagerAdapter);
    viewPager.addOnPageChangeListener(viewPagerPageChangeListener);

    setCurrentItem(selectedPosition);

现在我的问题是这段代码运行良好并显示/隐藏按钮包含布局。但它也会影响viewpager。我无法向左或向右滑动,也无法缩放图像。 我想要whatsapp之类的完整图像预览,触摸时显示前进按钮。

【问题讨论】:

  • 等等,你不能左右滑动和缩放?你至少应该能够做一件事。对吗?
  • @Dennis 不,我无能为力。既不滑动也不缩放。
  • 您能否发布所有代码(什么是 relativelayoutforvisibility 和 relativeLayout 变量?)我想您在 ViewPager 前面显示了一个布局,并且此布局接受了触摸事件并且不将其发送到布局后面的 ViewPager。
  • 是的,问题不完整,在有人投反对票之前赶快行动
  • @Bubu 更新了代码。

标签: android android-viewpager


【解决方案1】:

简单修复:将RelativeLayout 的高度设置为wrap_content

一旦您使RelativeLayout 可见,您的触摸手势不起作用的原因是因为您使用的是FrameLayout,并且“RelativeLayout”现在位于您的ImageView 上方,它将invalidate Touch gestures/events 或者换句话说,它将“consume”那个触摸手势,并且不允许你的ViewPager 使用它。

修复? 使用 ConstraintLayout 并将单个视图(存在于您的 RelativeLayout 中)与您的图像视图一起放置。

这是一个随机示例,您将在触摸 ImageView 时隐藏两个按钮。我不会发布显示onClickListeners 的java 代码。

<android.support.constraint.ConstraintLayout
    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"
    android:orientation="vertical">

  <ImageView android:layout_width="0dp" android:layout_height="0dp"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintRight_toRightOf="parent"/>

  <Button
      android:id="@+id/shareButton"
      android:text="Share Button"
      android:layout_width="wrap_content" android:layout_height="wrap_content"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      tools:ignore="HardcodedText"/>

  <Button
      android:id="@+id/sendButton"
      android:text="Send Button Button"
      android:layout_width="wrap_content" android:layout_height="wrap_content"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      tools:ignore="HardcodedText"/>

  <android.support.constraint.Group
      android:id="@+id/group"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:constraint_referenced_ids="sendButton,shareButton"/>

</android.support.constraint.ConstraintLayout>

然后在你的 onClickListener 里面

yourGroup.setVisiblility(View.GONE); 等等..

更新(用户要求)

GroupsConstraintLayout 的一部分

 <android.support.constraint.Group
              android:id="@+id/group"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:visibility="visible"
              app:constraint_referenced_ids="button4,button9" />

在您的 java 代码中

import android.support.constraint.Group; 

然后为了初始化它们

Group group = findViewById(R.id.yourGroupId);

然后设置可见性

group.setVisibility(View.VISIBLE);

【讨论】:

  • 等等什么?可以发图吗?
  • 使用 ConstrantLayout 后出现同样的问题。我只是用 ConstriantL 改变了框架布局,什么都没有。
  • 仔细阅读答案,我要求您从您的RelativeLayout 中取出子视图,并将它们与您的ImageView 一起放置在屏幕上
  • 我不知道你的布局是什么样的,我什至不知道你想在你的图像视图上显示的子视图。但我可以和你分享我的代码。等等。
  • 你使用 Whatsapp 吗?当我们单击图像时,它会显示完整视图。如果我们点击屏幕,分享和转发按钮显示和隐藏。像这样
【解决方案2】:

这是因为您的 relativelayoutforvisibility 占据了所有位置(宽度和高度 = match_parent)并且它在您的 ViewPager 前面,所以这个布局会接受所有触摸事件并且不会将它们发送到您的 ViewPager(即在布局)。 您可以将android:clickable="false"android:focusable="false" 添加到您的relativelayoutforvisibility

【讨论】:

  • 正确的解决方案是重新创建您的 UI,而不是添加额外的布局。 (它也可以让手机轻松渲染它)@Babu 检查我的答案,让我知道你对它的想法。
  • @Dennis 那么我应该在哪个布局上设置 OnClickListener ?
  • 请阅读我的回答。他应该做的是从他的RelativeLayout 中取出所有视图并将它们与他的ImageView 一起定位。这是一个很好的做法。 "不应在其布局文件中添加不必要的父视图层`
  • @Bubu 从whatsapp 看到图片。这是我想要的。
猜你喜欢
  • 2012-08-03
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多