Android: 将一个视图替换为另一个视图

Android: replace one view with another

我一般需要的是:通过单击按钮用另一个视图替换(隐藏)一个视图

让我们深入了解一下: 我用的是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 预览和点击播放后 - 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>

应该怎么解决?点击按钮后打开另一个 activity 对我来说非常简单,但问题是在同一个 Fragment

中播放 gif

有什么想法吗?

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

我只是将你的 com.basv.gifmoviewview.widget.GifMovieView 添加到你的根 RelativeLayout 的某处(也许你可能需要将 ViewPagerGifMovieView 包装在额外的 FrameLayout) 并在需要时使用普通 setVisibility().

简单地 show/hide

编辑

I suppose it's not the best solution for phones' memory usage

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