android - 如何防止ViewFlipper循环

标签 android viewflipper

我正在开发一个应用程序,我在其中使用带有自定义 OnTouch 实现的 ViewFlipper。在 ViewFlipper 中,我有大约 20 张图片供用户浏览。这工作正常,但如果我在系列中的第 20 张图片并翻转屏幕,它会返回到第一张图片。

我想阻止 ViewFlipper 循环回到第一张图片。相反,它应该简单地停在最后一张图片上。

这是我的代码:

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;
    }

}

和 XML 布局:

<?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>

最佳答案

您知道取景器中有 20 个 child 。因此,在 onclick 中创建一个 if 语句来检查 getDisplayedChild() 是否低于 20。如果它是 20 或更高,那么就不要调用 showNext() .

关于android - 如何防止ViewFlipper循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6354180/

相关文章:

android - 如何在android中设置Layout的最大高度?

java - Android - Firebase - 获取子数据的子数据

java - 在另一个 Activity 中实例化 Google map fragment

java - SimpleAdapter 不在 GUI 上显示数据

android - 强制关闭导致 HTTP 实体可能不为空

android - 将 ViewFlipper 的子项设置在中心

android - 使用 ViewFlipper 或 ViewPager 制作轮播

android - RecyclerView ViewHolder 中的 ViewFlipper

Android 光标错误 - "make sure cursor is initialized correctly before accessing data from it..."

android - 如何为 ViewFlipper 捕获翻转事件