2011-06-15 7 views
1

사용자 정의 OnTouch 구현을 사용하여 ViewFlipper을 사용하는 응용 프로그램을 개발 중입니다. 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 레이아웃 : 당신은 viewflipper 20 아이들이 있다는 것을 알고

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

+0

위의 qestion에서 확인할 수있는 질문에 내 코드를 추가 할 수 있습니다. –

답변

4

여기 내 코드입니다. 그러므로 getDisplayedChild()이 20보다 낮은지 확인하는 onclick의 if 문을 작성하십시오. 20 이상이면 showNext()을 호출하지 마십시오.

1

은 내가 Viewflipper상의 AnimationListener 설정 사용자 지정 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 = (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); 
관련 문제