是否有一种简单的方法可以在Android Studio预览中的ViewSwitcher中切换显示的视图,或者是一次一个地替换子视图的XML的唯一方法?
我一直在使用ViewAnimator/ViewSwitcher.
我最常见的用例是从加载阶段切换到内容阶段,或者在向导阶段之间切换,甚至有时会出现错误阶段.
当我建议在"android-ktx"存储库(这里)添加一个很好的扩展函数时,我被告知:
ViewAnimator不是我们积极推荐用于动画视图的API.它基于旧的动画系统,我们不想在这个库中推广它的使用.
我查看了ViewAnimator和ViewSwitcher的文章,包括文档.它没有说它被替换/弃用,或者建议使用别的东西.
什么取代了ViewAnimator?他在谈论过渡吗?
与ViewAnimator相比有哪些优缺点?
给定具有一些视图的ViewAnimator,如何将其转换为更新的解决方案,包括状态之间的切换?
android android-animation viewswitcher android-view viewanimator
我正在尝试使用ViewSwitcher在两个视图之间执行切换.一个是ImageView,另一个是MapView.我有一个按钮,用户单击该按钮执行此切换但我的代码导致错误.你们有什么想法吗?
<?xml version="1.0" encoding="utf-8"?>
<linearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<relativelayout
android:id="@+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<imageview
android:id="@+id/banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignparenttop="true"
android:background="@drawable/banner_location"
android:clickable="false">
</imageview>
<linearlayout
android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/banner"
android:layout_centerhorizontal="true"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:paddingbottom="5px">
<imagebutton
android:id="@+id/btnBusSvcs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginright="5px"
android:layout_margintop="5px"
android:background="@drawable/btn_bus_svcs">
</imagebutton>
<imagebutton
android:id="@+id/btnDrvRoute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginleft="5px"
android:layout_margintop="5px"
android:background="@drawable/btn_drv_route">
</imagebutton>
</linearlayout>
<viewswitcher
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imgViewBusSvcs"
android:layout_below="@+id/LinearLayout01">
<imageview
android:id="@+id/imgViewBusSvcs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/LinearLayout01"
android:layout_centerinparent="true"
android:layout_gravity="center_horizontal"
android:background="@drawable/bus_svcs"
android:clickable="false">
</imageview>
<com.google.android.maps.mapview
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="300px"
android:apikey="REMOVE MY API KEY"
android:clickable="true">
</com.google.android.maps.mapview>
</viewswitcher>
<imagebutton
android:id="@+id/btnBackHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignparentbottom="true"
android:layout_centerhorizontal="true" …Run Code Online (Sandbox Code Playgroud) 我需要向viewSwitcher添加两个以上的视图.我试过,但我得到以下错误日志:
Can't add more than 2 views to a viewSwitcher
我想知道我是否可以通过ViewSwitcher的任何属性解决这个问题,即使我手动从我的Java类添加更多视图.
谢谢!
应该很容易,对吗?所以 - 这是我ViewSwitcher在XML中定义的方式(为了简洁省略了Id和布局)
<ViewSwitcher android:layout_height="wrap_content" android:layout_width="fill_parent" >
<!-- First view, comes up OK -->
<LinearLayout android:id="@+id/header1">
<!-- some more controls: Progress bar, test view and the button -->
</LinearLayout>
<!-- second view. changing to the actual (not include) layout has no effect -->
<include android:id="@+id/header2" />
</ViewSwitcher>
Run Code Online (Sandbox Code Playgroud)
然后在我的Java代码中的某处我有这个代码
ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
// more code that basically executes background search
// when call comes back - switch
switcher.bringToFront(); // does nothing
// switcher.bringChildToFront(findViewById(R.id.header2)); // no effect ether …Run Code Online (Sandbox Code Playgroud) 我在布局中设置动画如下:
<ViewSwitcher
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right" >
Run Code Online (Sandbox Code Playgroud)
我怎么能以编程方式做同样的事情?
是否有可能在ViewSwitcher中获取当前显示图像的名称/引用/ id?
switcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
[...]
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d(TAG, "tell me please, how to get the name/reference/id/etc of the currently displayed image, so i can start a specific action relating to the current image");
return false;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
托比亚斯
我是Android的新手.我有一个关于如何实现`listView 这里的教程:
我实现了它,它工作正常.我需要实现一个ViewSwitcher相同的.就像我刷这首歌一样,我需要得到一些选项,如播放,添加到队列等,
请完成该教程并寻求帮助.我差不多3个星期都在挣扎.
希望有人能帮助我.
编辑1:以下代码是主要活动:
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.ViewSwitcher;
public class CustomizedListView extends Activity implements OnGestureListener{
// All static variables
static final String URL = "http://api.androidhive.info/music/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = …Run Code Online (Sandbox Code Playgroud) android swipe android-listview viewswitcher android-2.1-eclair
我像这样使用ViewSwitcher:
<ViewSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/content_popup"
android:visibility="invisible"
android:layout_centerInParent="true">
<include layout="@layout/content_popup_stub" android:id="@+id/content_general"/>
<include layout="@layout/video_select_popup_stub" android:id="@+id/content_select_video"/>
</ViewSwitcher>
Run Code Online (Sandbox Code Playgroud)
并计划在该ViewSwitcher中再使用4个子级。现在,如何使用.showNext()或.showPrevious()在 view1和view4或view1和view3之间切换?有没有办法设置下一个或上一个视图?