【问题标题】:Android: replace one view with anotherAndroid:用另一个视图替换一个视图
【发布时间】:2016-11-20 13:06:09
【问题描述】:

我一般需要什么:通过单击按钮将一个视图替换(隐藏)另一个视图

让我们深入了解一下: 我使用 ViewPager,它包含 100 张图片。

ViewPager的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"           
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:layout_alignParentLeft="true"       
        android:background="#ffffff" >

    </android.support.v4.view.ViewPager>

    <ImageButton
        android:id="@+id/imagePlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageRight"
        android:layout_alignTop="@+id/imageList"        
        android:src="@drawable/list" />

    </RelativeLayout>

图片布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="1dip"
    android:background="#ffffff">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
     /> 
</RelativeLayout>

它有效 - 没问题。但是我需要在viewpager中添加按钮,并且在viewpager中点击图像后应该将其更改为gif。它看起来像 gif 预览和 onclick 播放后 - gif 开始播放。

GIF 的自定义布局:

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageList"
        android:layout_marginRight="28dp"
        android:layout_toLeftOf="@+id/imageRight" >

            <com.basv.gifmoviewview.widget.GifMovieView
                android:id="@+id/gif1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 />

    </LinearLayout>

应该如何解决?在点击按钮后打开另一个活动对我来说非常简单,但问题是在同一个片段中播放 gif

有什么想法吗?

【问题讨论】:

    标签: android android-layout android-fragments android-imageview animated-gif


    【解决方案1】:

    我只需将您的 com.basv.gifmoviewview.widget.GifMovieView 添加到您的根 RelativeLayout 的某个位置(也许您可能需要将 ViewPagerGifMovieView 都包装在额外的 FrameLayout 中)并在需要时使用普通函数简单地显示/隐藏它setVisibility().

    编辑

    我想这不是手机内存使用的最佳解决方案

    我很快查看了sources,实际上并没有太多代码。因此,如果您打扰内存使用,您始终可以在显示之前动态创建小部件(并在隐藏后销毁),或者,由于我看不到“卸载”GIF 的方法,您始终可以使用“1x1 px”空 Gif 来确保最后一个如果不需要,您刚刚停止显示不再占用宝贵的内存。

    【讨论】:

    • 感谢回复,我想这不是手机内存使用的最佳解决方案,但解决问题听起来不错。
    【解决方案2】:

    这是一个想法。使用视图寻呼机和 gif 进行布局,但 gif 可见性设置为不可见,因此最初它在那里但不可见。但是,当有人单击按钮时,您应该将该 gif 设置为可见,同时将视图寻呼机更改为不可见或消失,以防您甚至不希望其初始边界存在!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 2017-05-04
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      相关资源
      最近更新 更多