Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何防止ViewFlipper循环_Android_Viewflipper - Fatal编程技术网

Android 如何防止ViewFlipper循环

Android 如何防止ViewFlipper循环,android,viewflipper,Android,Viewflipper,我正在开发一个应用程序,其中我使用了一个带有自定义OnTouch实现的ViewFlipper。在ViewFlipper中,我有大约20幅图像供用户浏览。这很好,但是如果我在系列中的第20张图片上翻转屏幕,它会返回到第一张图片 我想防止视图翻转器循环回到第一个图像。相反,它应该在最后一个图像处停止 这是我的密码: import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; impor

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

我想防止
视图翻转器
循环回到第一个图像。相反,它应该在最后一个图像处停止

这是我的密码:

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

}
导入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;
公共类Activity1将活动实现扩展到TouchListener{
浮动下降值;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//将main.XML设置为此活动的布局
setContentView(R.layout.main);
//把这两行加起来
LinearLayout layMain=(LinearLayout)findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener)this);
//向微调器添加几个国家/地区
Spinner Spinner Countries=(Spinner)findViewById(R.id.Spinner\u Countries);
ArrayAdapter countryArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple\u微调器\u下拉菜单\u项,
新字符串[]{“加拿大”、“美国”});
喷丝头国家/地区。setAdapter(countryArrayAdapter);
}
公共布尔onTouch(视图arg0、运动事件arg1){
//获取在此触摸事件上执行的操作
开关(arg1.getAction())
{
case MotionEvent.ACTION\u DOWN:
{
//当按下用户的手指时,存储X值
downXValue=arg1.getX();
打破
}
case MotionEvent.ACTION\u UP:
{
//获取用户释放手指时的X值
float currentX=arg1.getX();
//倒退:把东西往右边推
如果(向下X值<当前X)
{
//获取对ViewFlipper的引用
ViewFlipper vf=(ViewFlipper)findViewById(R.id.details);
//设置动画
setAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_out));
//翻转!
vf.showPrevious();
}
//向前:把东西推到左边
如果(向下X值>当前X)
{
//获取对ViewFlipper的引用
ViewFlipper vf=(ViewFlipper)findViewById(R.id.details);
//设置动画
设置动画(AnimationUtils.loadAnimation(这个,R.anim.push_left_in));
//翻转!
vf.showNext();
}
打破
}
}
//如果返回false,则不会记录这些操作
返回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>


您知道viewflipper中有20个子对象。因此,在onclick中做一个if语句,检查
getDisplayedChild()
是否低于20。如果是20或更高,则不要调用
showNext()

我通过自定义ViewFlipper检测子对象的结束

public class ExViewFlipper extends ViewFlipper {

  private OnChangeViewListener mOnChangeViewListener;

  public ExViewFlipper(Context context) {
    super(context);
  }

  public ExViewFlipper(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public interface OnChangeViewListener {

    /**
     * call on Change view
     * 
     * @param index next index
     * @param hasNext true if next view is exist
     */
    void onChange(int index, boolean hasNext);
  }

  public void setOnChangeViewListener(OnChangeViewListener listener) {
    mOnChangeViewListener = listener;
  }

  @Override
  public void showNext() {

    super.showNext();

    if (mOnChangeViewListener != null) {
      mOnChangeViewListener.onChange(getDisplayedChild(), true);
    }
  }

  @Override
  public void showPrevious() {

    super.showPrevious();

    if (mOnChangeViewListener != null) {
      mOnChangeViewListener.onChange(getDisplayedChild(), false);
    }
  }

  public boolean isFirstItem() {

    return getDisplayedChild() == 0;
  }

  public boolean isLastItem() {

    return getDisplayedChild() == getChildCount() - 1;
  }
}

在Viewflipper上设置AnimationListener,然后在到达最后一张幻灯片时停止动画

    viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipperMain);              
    viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.slide_in_right));       
    viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_left));   

    AnimationListener mAnimationListener = new Animation.AnimationListener() {
        public void onAnimationStart(Animation animation) {
            //animation started event
        }

        public void onAnimationRepeat(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {
            //TODO animation stopped event
            if (viewFlipper.getDisplayedChild()==intLastChild){
                viewFlipper.stopFlipping();
            }
        }
    };

   //Get a reference to one of the animations set on the ViewFlipper
   //In this example, I used the "In Animation"
   viewFlipper.getInAnimation().setAnimationListener(mAnimationListener);

你能详细解释一下你翻转屏幕的意思吗?您是在谈论方向更改还是在
浏览页面上执行某种滑动手势?你有任何代码可以产生这种行为吗?我可以在问题中使用我的代码,你可以从上面的qestion检查它。