如何prevent ViewFlipper从循环 [英] How to prevent ViewFlipper from looping

查看:123
本文介绍了如何prevent ViewFlipper从循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发中,我使用的是 ViewFlipper 用自定义的 OnTouch 执行的应用程序。在 ViewFlipper ,我有大约20幅图像,供用户翻阅。这工作得很好,但如果我在该系列中的第20图像和翻转屏幕,它返回到第一张图像。

我想prevent的 ViewFlipper 从循环回第一张图像。相反,它应该简单地停留在最后的影像。

下面是我的code:

 进口android.app.Activity;
进口android.os.Bundle;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.View.OnTouchListener;
进口android.view.animation.AnimationUtils;
进口android.widget.ArrayAdapter;
进口android.widget.LinearLayout;
进口android.widget.Spinner;
进口android.widget.ViewFlipper;

公共类活动1扩展活动实现OnTouchListener {

    浮动downXValue;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        //设置main.xml中作为该活动的布局
        的setContentView(R.layout.main);

        //添加这两条线
        的LinearLayout layMain =(的LinearLayout)findViewById(R.id.layout_main);
        layMain.setOnTouchListener((OnTouchListener)本);

        //添加一些国家的微调
        微调spinnerCountries =(微调)findViewById(R.id.spinner_country);
        ArrayAdapter countryArrayAdapter =新的ArrayAdapter(这一点,
                    android.R.layout.simple_spinner_dropdown_item,
                    新的String [] {加拿大,USA});
        spinnerCountries.setAdapter(countryArrayAdapter);

    }

    公共布尔onTouch(查看为arg0,MotionEvent ARG1){

        //获取通过这个触摸事件执行的操作
        开关(arg1.getAction())
        {
            案例MotionEvent.ACTION_DOWN:
            {
                //存储X值时,用户的手指被pressed向下
                downXValue = arg1.getX();
                打破;
            }

            案例MotionEvent.ACTION_UP:
            {
                //获取X值,当用户发布了他/她的手指
                浮currentX = arg1.getX();

                //退步:推东西的权利
                如果(downXValue< currentX)
                {
                    //获取一个参考ViewFlipper
                     ViewFlipper VF =(ViewFlipper)findViewById(R.id.details);
                     //设置动画
                      vf.setAnimation(AnimationUtils.loadAnimation(这一点,R.anim.push_left_out));
                      //翻转!
                      vf.show previous();
                }

                //将前锋:推动东西向左
                如果(downXValue> currentX)
                {
                    //获取一个参考ViewFlipper
                    ViewFlipper VF =(ViewFlipper)findViewById(R.id.details);
                     //设置动画
                     vf.setInAnimation(AnimationUtils.loadAnimation(这一点,R.anim.push_left_in));
                      //翻转!
                     vf.showNext();
                }
                打破;
            }
        }

        //如果返回false,这些行动将不会被记录
        返回true;
    }

}
 

和XML布局:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:ID =@ + ID / layout_main
    >

    < ViewFlipper机器人:ID =@ + ID /详细信息
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>


      <的LinearLayout
               机器人:方向=垂直
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            >

            < ImageView的机器人:ID =@ + ID / ImageView01机器人:背景=@可绘制/二
            机器人:layout_x =1dip机器人:layout_y =1dip
            机器人:layout_height =FILL_PARENT机器人:layout_width =FILL_PARENT>< / ImageView的>
        < / LinearLayout中>
         <的LinearLayout
               机器人:方向=垂直
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            >

            < ImageView的机器人:ID =@ + ID / ImageView01机器人:背景=@可绘制/三
            机器人:layout_x =1dip机器人:layout_y =1dip
            机器人:layout_height =FILL_PARENT机器人:layout_width =FILL_PARENT>< / ImageView的>
        < / LinearLayout中>
         <的LinearLayout
               机器人:方向=垂直
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            >

            < ImageView的机器人:ID =@ + ID / ImageView01机器人:背景=@可绘制/四
            机器人:layout_x =1dip机器人:layout_y =1dip
            机器人:layout_height =FILL_PARENT机器人:layout_width =FILL_PARENT>< / ImageView的>
        < / LinearLayout中>
         <的LinearLayout
               机器人:方向=垂直
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            >

            < ImageView的机器人:ID =@ + ID / ImageView01机器人:背景=@可绘制/五
            机器人:layout_x =1dip机器人:layout_y =1dip
            机器人:layout_height =FILL_PARENT机器人:layout_width =FILL_PARENT>< / ImageView的>
        < / LinearLayout中>
 <的LinearLayout
               机器人:方向=垂直
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            >

            < ImageView的机器人:ID =@ + ID / ImageView01机器人:背景=@可绘制/六类
            机器人:layout_x =1dip机器人:layout_y =1dip
            机器人:layout_height =FILL_PARENT机器人:layout_width =FILL_PARENT>< / ImageView的>
        < / LinearLayout中>
         <的LinearLayout
               机器人:方向=垂直
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            >

            < ImageView的机器人:ID =@ + ID / ImageView01机器人:背景=@可绘制/七
            机器人:layout_x =1dip机器人:layout_y =1dip
            机器人:layout_height =FILL_PARENT机器人:layout_width =FILL_PARENT>< / ImageView的>
        < / LinearLayout中>
         <的LinearLayout
               机器人:方向=垂直
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            >

            < ImageView的机器人:ID =@ + ID / ImageView01机器人:背景=@可绘制/八
            机器人:layout_x =1dip机器人:layout_y =1dip
            机器人:layout_height =FILL_PARENT机器人:layout_width =FILL_PARENT>< / ImageView的>
        < / LinearLayout中>

    < ViewFlipper>
 < / LinearLayout中>
 

解决方案

您知道,有20名儿童在viewflipper。因此,要if语句在onclick检查被 getDisplayedChild()低则20。如果是20或更高,然后就是不来电 showNext ()

I am developing an application in which I am using a ViewFlipper with a custom OnTouch implementation. In the ViewFlipper, I have about 20 images for the user to flip through. This works fine, but if I'm at the 20th image in the series and flip the screen, it returns to the first image.

I want to prevent the ViewFlipper from looping back to the first image. Instead, it should simply stop at the last image.

Here is my code:

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.ViewFlipper;

public class Activity1 extends Activity implements OnTouchListener{

    float downXValue;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set main.XML as the layout for this Activity
        setContentView(R.layout.main);

        // Add these two lines
        LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
        layMain.setOnTouchListener((OnTouchListener) this); 

        // Add a few countries to the spinner
        Spinner spinnerCountries = (Spinner) findViewById(R.id.spinner_country);
        ArrayAdapter countryArrayAdapter = new ArrayAdapter(this,
                    android.R.layout.simple_spinner_dropdown_item,
                    new String[] { "Canada", "USA" });
        spinnerCountries.setAdapter(countryArrayAdapter);

    }

    public boolean onTouch(View arg0, MotionEvent arg1) {

        // Get the action that was done on this touch event
        switch (arg1.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                // store the X value when the user's finger was pressed down
                downXValue = arg1.getX();
                break;
            }

            case MotionEvent.ACTION_UP:
            {
                // Get the X value when the user released his/her finger
                float currentX = arg1.getX();            

                // going backwards: pushing stuff to the right
                if (downXValue < currentX)
                {
                    // Get a reference to the ViewFlipper
                     ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                     // Set the animation
                      vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
                      // Flip!
                      vf.showPrevious();
                }

                // going forwards: pushing stuff to the left
                if (downXValue > currentX)
                {
                    // Get a reference to the ViewFlipper
                    ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                     // Set the animation
                     vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
                      // Flip!
                     vf.showNext();
                }
                break;
            }
        }

        // if you return false, these actions will not be recorded
        return true;
    }

}

And the XML layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout_main"
    >

    <ViewFlipper android:id="@+id/details"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">  


      <LinearLayout
               android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >

            <ImageView android:id="@+id/ImageView01" android:background="@drawable/two" 
            android:layout_x="1dip" android:layout_y="1dip" 
            android:layout_height="fill_parent" android:layout_width="fill_parent"></ImageView>
        </LinearLayout> 
         <LinearLayout
               android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >

            <ImageView android:id="@+id/ImageView01" android:background="@drawable/three" 
            android:layout_x="1dip" android:layout_y="1dip" 
            android:layout_height="fill_parent" android:layout_width="fill_parent"></ImageView>
        </LinearLayout> 
         <LinearLayout
               android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >

            <ImageView android:id="@+id/ImageView01" android:background="@drawable/four" 
            android:layout_x="1dip" android:layout_y="1dip" 
            android:layout_height="fill_parent" android:layout_width="fill_parent"></ImageView>
        </LinearLayout> 
         <LinearLayout
               android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >

            <ImageView android:id="@+id/ImageView01" android:background="@drawable/five" 
            android:layout_x="1dip" android:layout_y="1dip" 
            android:layout_height="fill_parent" android:layout_width="fill_parent"></ImageView>
        </LinearLayout> 
 <LinearLayout
               android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >

            <ImageView android:id="@+id/ImageView01" android:background="@drawable/six" 
            android:layout_x="1dip" android:layout_y="1dip" 
            android:layout_height="fill_parent" android:layout_width="fill_parent"></ImageView>
        </LinearLayout> 
         <LinearLayout
               android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >

            <ImageView android:id="@+id/ImageView01" android:background="@drawable/seven" 
            android:layout_x="1dip" android:layout_y="1dip" 
            android:layout_height="fill_parent" android:layout_width="fill_parent"></ImageView>
        </LinearLayout> 
         <LinearLayout
               android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >

            <ImageView android:id="@+id/ImageView01" android:background="@drawable/eight" 
            android:layout_x="1dip" android:layout_y="1dip" 
            android:layout_height="fill_parent" android:layout_width="fill_parent"></ImageView>
        </LinearLayout> 

    <ViewFlipper>
 </LinearLayout>

解决方案

You know that there are 20 children in the viewflipper. Therefore make an if statement in the onclick that checks if the getDisplayedChild() is lower then 20. If it is 20 or higher then just dont call showNext().

这篇关于如何prevent ViewFlipper从循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆