2011-06-01 44 views
0

问题是,添加到main.xml中的所有视图都正确翻转 - 在最后一个视图变为第一个视图之后,在第一个 - 最后一个视图之后,它的四舍五入,但如果我添加视图使用ViewFlipper类的方法addView它不会翻转“四舍五入”它会停下来,并做一些不正确的动画,它不会去下一个视图,并会去往前只有如果最后只做了一次翻转。 请说出如何使它作为一个1-> 2-> 3-> 1。 这里的脚蹼实现代码:添加了addView的视图不会翻转(使用ViewFlipper,Android)

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); 
    setContentView(R.layout.main); 
    // Set main.XML as the layout for this Activity 


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


} 

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();    
      View view = new View(this); 
     //HERE IS DECLARATION OF VIEW WHICH I NEED TO ADD 
      GraphicsView myview=new GraphicsView(this); 
      // going backwards: pushing stuff to the right 
      if (downXValue < currentX) 
      { 
       // Get a reference to the ViewFlipper 
       ViewFlipper vf = (ViewFlipper) findViewById(R.id.details); 
         vf.addView(myview);     

       // Set the animation 

       vf.setInAnimation(view.getContext(), R.anim.push_right_in); 
       vf.setOutAnimation(view.getContext(), R.anim.push_right_out); 
       // Flip! 

       vf.showNext(); 

      } 

      // going forwards: pushing stuff to the left 
      if (downXValue > currentX) 
      { 
       // Get a reference to the ViewFlipper 
       ViewFlipper vf = (ViewFlipper) findViewById(R.id.details); 
       //HERE I'M ADDING IT     
      vf.addView(myview); 
       // Set the animation 
       vf.setInAnimation(view.getContext(), R.anim.push_left_in); 
       vf.setOutAnimation(view.getContext(), R.anim.push_left_out); 
        // Flip! 
            vf.showPrevious(); 
      } 
      break; 
     } 
    } 

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

请帮忙。如果可能的话,回答plz在main.xml中添加对象,我在myview这样的代码中定义的是GraphicsView的类对象,该对象从View扩展而来。

问候, Keem

回答

-1

根据你的代码,您张贴,在你的代码动态添加视图中,触摸事件,让尽可能多的触摸设备屏幕时,您OnTouch()调用,每touch this this ..

GraphicsView myview = new GraphicsView(this); (ViewFlipper)findViewById(R.id.details); vf.addView(myview);
所以每次触摸你添加一个视图,并可能是这就是为什么你不能正确翻转。

您的解决方案,应该按照正确的流程 - 可能是在OnCreate中如下

  1. ()在viewFlipper添加不同的看法,从侧面您OnTouch()。
  2. 在OnTouch()中,您设置了您的动画并调用vf.showPrevious()或vf.showNext()进行propper导航。