2011-06-15 65 views
1

我正在開發一個應用程序,我在其中使用ViewFlipper自定義OnTouch實現。在ViewFlipper中,我有大約20張圖片供用戶翻閱。這工作正常,但如果我在系列中的第20張圖像並翻轉屏幕,它會返回到第一張圖像。如何防止ViewFlipper循環

我想阻止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> 
+0

你能詳細說明你翻轉屏幕的意思嗎?你是在談論改變方向還是在「ViewFlipper」上執行某種滑動手勢?你有任何代碼來產生你可以顯示的行爲嗎? – nil 2011-06-15 07:25:14

+0

我可以p [在你可以從上面的問題中檢查它的問題中查看我的代碼。 – 2011-06-15 07:35:25

回答

4

你知道,有20名孩子在viewflipper。因此,請在onclick中檢查if語句,以檢查getDisplayedChild()是否低於20.如果它是20或更高,則不要致電showNext()

1

我通過定製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; 
    } 
0

在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);