Android 如何在ViewFlipper中用另一个视图替换视图

Android 如何在ViewFlipper中用另一个视图替换视图,android,views,viewflipper,Android,Views,Viewflipper,我的应用程序包含一个具有更多图像的ViewFlipper。它使用一些动画自动翻转图像。我希望在显示某个图像时,ViewFlipper停止滑动,将该图像替换为另一个图像,然后ViewFlipper再次开始翻转。我花了很多时间在这方面,但它没有得到工作。我试图替换视图并添加新视图,但应用程序无法正常工作。有人使用过ViewFlipper吗?有人知道如何用新视图替换视图吗 这是我的密码: vf = (ViewFlipper) findViewById(R.id.details); vf.setFlip

我的应用程序包含一个具有更多图像的ViewFlipper。它使用一些动画自动翻转图像。我希望在显示某个图像时,ViewFlipper停止滑动,将该图像替换为另一个图像,然后ViewFlipper再次开始翻转。我花了很多时间在这方面,但它没有得到工作。我试图替换视图并添加新视图,但应用程序无法正常工作。有人使用过ViewFlipper吗?有人知道如何用新视图替换视图吗

这是我的密码:

vf = (ViewFlipper) findViewById(R.id.details);
vf.setFlipInterval(Integer.valueOf(3 * 1000));

//populate the ViewFlipper with images

vf.setOutAnimation(animFlipOutNext);
vf.setInAnimation(animFlipInNext);
vf.setAutoStart(true);
我使用的动画:

animFlipInNext = AnimationUtils
                .loadAnimation(this, R.anim.push_left_in);
        animFlipInNext.setDuration(2000);
        animFlipInNext.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                System.out.println("AnimStart- LeftIn" + " Will be displayed "
                        + vf.getDisplayedChild());
                imageName = images.listFiles();

                for (int i = 0; i < imageName.length; i++) {
                    if (vf.getCurrentView().getTag().toString()
                            .equalsIgnoreCase(imageName[i].getName())) {
                        System.out.println("begin  " + imageName[i].getName());
                        vf.stopFlipping();

                    } else
                        System.out.println("NUNUNU");

                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                System.out.println("AnimRepeat-LeftIn");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                System.out.println("Anim end " + vf.getDisplayedChild());
                for (int i = 0; i < imageName.length; i++) {
                    if (vf.getCurrentView().getTag().toString()
                            .equalsIgnoreCase(imageName[i].getName())) {
                        System.out.println("begin  " + imageName[i].getName());
                        videoname = imageName[i].getName().replace("1.jpg", "");
                        System.out.println("VideoName este " + videoname
                                + ".mp4");

                        bitmap = BitmapFactory.decodeFile(Environment
                                .getExternalStorageDirectory()

                        + "/Images" + "/" + "23Video1.jpg");
                        img = new ImageView(getBaseContext());
                        img.setImageBitmap(bitmap);
                        img.setScaleType(ImageView.ScaleType.FIT_XY);
                        int nr = vf.getDisplayedChild();
                        System.out.println("nr view "+nr);

                        vf.removeViewAt(nr);

                        vf.addView(img, nr,new LayoutParams(LayoutParams.FILL_PARENT,
                                LayoutParams.FILL_PARENT));
                        vf.setDisplayedChild(nr);
                        vf.setVisibility(View.VISIBLE);
                        img=null;
                        //vf.setDisplayedChild(nr);

                    }
                }
            }
        });

        animFlipOutNext = AnimationUtils.loadAnimation(this,
                R.anim.push_left_out);
        animFlipOutNext.setDuration(2000);

        animFlipInPrevious = AnimationUtils.loadAnimation(this,
                R.anim.push_right_in);
        animFlipInPrevious.setDuration(2000);

        animFlipOutPrevious = AnimationUtils.loadAnimation(this,
                R.anim.push_right_out);
        animFlipOutPrevious.setDuration(2000);

我尝试替换onAnimationEnd方法上的视图。请帮忙。。欢迎任何想法。

ViewFlipper有showNext和showPrevious方法,通过它们我们可以更改当前显示的子项,若要获取当前显示的子项,请使用getDisplayedChild。它将返回当前显示子项的索引

只需使用以下方法显示第n个元素:

private void display(int n)
{
    int index=viewFlipper.getDisplayedChild();
    while(n<index)
    {
       viewFlipper.showPrevious();
       index--;
    }
    while(n>index)
    {
         viewFlipper.showNext();
         index++;
    }
}

我做的和你一样,没有动画,只替换了viewflipper。可以在动画属性上放置不同的动画。将这种类型的xml放在布局中

 <ViewFlipper android:id="@+id/vf1" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_margin="3dp">
            <RelativeLayout android:layout_width="wrap_content"
                android:id="@+id/RLF1" android:background="@drawable/block_purpal"
                android:layout_height="wrap_content">

            </RelativeLayout>
            <RelativeLayout android:layout_width="wrap_content"
                android:id="@+id/RLB1" android:gravity="center" android:background="@drawable/block_purpal"
                android:layout_height="wrap_content">
                <TextView android:layout_width="wrap_content" android:id="@+id/textView1"
                     android:gravity="right"
                    android:layout_height="wrap_content" />
            </RelativeLayout>
        </ViewFlipper>
打开视图翻转器的clicklistener

            Message msg = new Message();
            msg.what = 1;
            delayHandler.sendMessageDelayed(msg, DELAYTIME);

            vFilpper1.showNext();
最后添加以下内容: 公共处理程序delayHandler=新处理程序{

    @Override
    public void handleMessage(Message message) {
        super.handleMessage(message);

        switch (message.what) {
        case 1:
            try {
                vFilpper1.showPrevious();
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;

    so on other viewflipper..
也去看看吧 如果您认为这不是一个更好的主意,请转到:


我使用getDisplayedChild获取当前显示子对象的索引。问题在于替换视图。新图像未添加到viewflipper
    @Override
    public void handleMessage(Message message) {
        super.handleMessage(message);

        switch (message.what) {
        case 1:
            try {
                vFilpper1.showPrevious();
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;

    so on other viewflipper..