ViewSwitcher在以编程方式更新视图和切换时未显示初始视图

时间:2014-09-16 23:53:08

标签: android

以下是我正在尝试的概念的测试项目。目的是动态创建两个视图并从一个视图滑动到另一个视图。

但是,ViewSwitcher 从不显示初始视图!

活动

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity {

    private ViewSwitcher mViewSwitcher;
    private Button mButton;
    private Animation mAnimSlideInRight;
    private Animation mAnimSlideOutLeft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher1);
        mButton = (Button) findViewById(R.id.button1);

        mAnimSlideInRight = AnimationUtils.loadAnimation(this, R.anim.slide_in_right);
        mAnimSlideOutLeft = AnimationUtils.loadAnimation(this, R.anim.slide_out_left);

        // Fish image
        final ImageView imageView1 = new ImageView(MainActivity.this);
        imageView1.setImageResource(R.drawable.fish_one);
        imageView1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        // Fire image
        final ImageView imageView2 = new ImageView(MainActivity.this);
        imageView2.setImageResource(R.drawable.fire);
        imageView2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));


        mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                mViewSwitcher.removeAllViews();

                mViewSwitcher.addView(imageView1, 0);
                mViewSwitcher.addView(imageView2, 1);

                mViewSwitcher.setInAnimation(mAnimSlideInRight);
                mViewSwitcher.setOutAnimation(mAnimSlideOutLeft);

                mViewSwitcher.showNext();
            }
        });

    }
}

布局XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Flip" />

    <ViewSwitcher
        android:id="@+id/viewSwitcher1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </ViewSwitcher>

</RelativeLayout>

动画:slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%" android:toXDelta="0%"
            android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

动画:slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0%" android:toXDelta="-100%"
            android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

对于图像,我只使用了google'd鱼图像和火图像。

问题:鱼图像永远不可见:-( 相反,视图是白色的,火图像图像会滑入。(鱼永远不会滑出)

如何让鱼滑出来?

1 个答案:

答案 0 :(得分:0)

我已经弄明白了 - 解决方案是给观看时间!不要急于求成,哈哈! 以下是更新后的mButton.setOnClickListener()

的代码
mButton.setOnClickListener(new OnClickListener()
{

    // Button callback
    @Override
    public void onClick(View v)
    {
        mViewSwitcher.post(new Runnable()
        {
            // Runnable that removes all views and places the images in the view
            @Override
            public void run()
            {
                mViewSwitcher.removeAllViews();

                mViewSwitcher.addView(imageView1, 0);
                mViewSwitcher.addView(imageView2, 1);

                // Nested Runnable that triggers the sliding animation
                mViewSwitcher.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        mViewSwitcher.setInAnimation(mAnimSlideInRight);
                        mViewSwitcher.setOutAnimation(mAnimSlideOutLeft);
                        mViewSwitcher.showNext();
                    }
                });// end of nested Runnable

            }
        });// end of Runnable

    }
});// end of OnClickListener