The view, which was added with addView do not Flip (using ViewFlipper, Android)
The problem is that all the views, added to main.xml are flipping correctly - after the last view goes first view, after first -goes last, its rounded, but if i add the view using method addView of ViewFlipper class it won't flip "rounded" it will stop on it and do some incorrect animation, it won't go to next view and will go to previous only if there was done only 1 flip to the end. Please, say how to make it works as a round 1->2->3->1. Here's the code 开发者_运维技巧of flipper realization:
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;
}
Please, help. And answer plz if possible to add in main.xml the objects, that i defined in the code like myview is class object of GraphicsView, which extends from View.
Regards, Keem
As per your code that you post, In your code you are adding view dynamically, on Touch Events, so as many time you touch your device screen, your OnTouch() call and every touch call this..
GraphicsView myview=new GraphicsView(this);
and also this.. ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
vf.addView(myview);
so on every touch you adding a view, and may be thats why you not getting right flipping.
For your solution, you should follow correct flow - may be as follows
- in OnCreate() add different view in viewFlipper, out side your OnTouch().
- In OnTouch() you set your animation and call vf.showPrevious() orvf.showNext() for propper navigation.
精彩评论