Android ViewFlipper with 2 views - android

I'm looking for help with creating ViewFlipper which will work like in android calendar when switching between months by swipe.
I want to have only two views inside my viewFlipper, and when i swipe right i want to do
mViewFlipper.setDisplayedChild( 1 );
and when i swipe left :
mViewFlipper.setDisplayedChild( 0 );
and when i swipe twice left, i want to get every time full in/out animation and the same for right swipping. Full animation works only when i swipe to next and to previous, never in direction of next -> next.
Thanks for help!

If you're only using 2 views, and at present and you're setting 0 and 1 manually as such, just call
mViewFlipper.showNext();
instead regardless of the swipe direction and apply the correct animation depending on if you want it to be sliding left or right. This will mean even if you're on view 1 and you slide right again (like you defined) then view 0 will come back in, like a never ending flipper between the 2 views.
http://developer.android.com/reference/android/view/animation/AnimationUtils.html
I may be totally misunderstanding your question however, it is rather vague.

See this link it may help.....
http://www.1mobile.com/effective-navigation-863044.html
http://developer.android.com/training/implementing-navigation/index.html

**folder in res = animator.......
translation animation xml file = s_in_fleft in animator folder in res folder.....
might help**
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
if (lastX < currentX) {
if (viewFlipper.getDisplayedChild() == 0)
break;
viewFlipper.setInAnimation(this, R.animator.s_in_fleft);
viewFlipper.setOutAnimation(this, R.animator.s_out_right);
viewFlipper.showNext();
}
if (lastX > currentX) {
if (viewFlipper.getDisplayedChild() == 1)
break;
viewFlipper.setInAnimation(this, R.animator.s_in_fright);
viewFlipper.setOutAnimation(this, R.animator.s_out_left);
viewFlipper.showPrevious();
}
break;
}
}
return false;
}

Related

How to swipe list view item left in Android?

I have a list view and I want to make it so I can swipe each item to reveal a delete button:
I've figured out how to recognise the swipe event, I am using this code (listItem is of type View):
listItem.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
_xSwipe1 = event.getX();
break;
case MotionEvent.ACTION_UP:
_xSwipe2 = event.getX();
float deltaX = _xSwipe2 - _xSwipe1;
if (deltaX < 0)
{
Log.e("SWIPE", "Right to Left swipe");
}
else if (deltaX >0)
{
Log.e("SWIPE", "Left to right swipe");
}
break;
}
return false;
}
});
And when I swipe, I can see in the logs that the swipe event is being recognised.
However, I'm not sure how to physically make the list item start disappearing to the left?
Any help would be greatly appreciated.
If you know when the swipe is happening and how much the user has swiped you could call View.setTranslationX() depending on how much the user has swiped. Alternatively, you could forego the swipe detection logic altogether and use a ViewPager with only 2 pages. You would then override PagerAdapter.getPageWidth(...) in your PagerAdapter so that the delete button only takes up a limited amount of space.

Disable last page on ViewPager

i'm using viewpagerIndicator in my app, i have 200 pages. is possible disable the Swipe 1 page before the last page?
Example my currentpage is the 199 here the swipe is disble and never change to the next page, only the previous pages.
EDIT: i have one calendar in the Tabindicator, the pages show info for day, i need show the next day but not swipe because not exist info for this reason i need disable this page.
Exam:
19/ago/2014 | 20/ago/2014 | 21/ago/2014
before |Current |Last Page
in before i can swipe left and right but in Current i need only swipe left and disable right is possible?
You can return 199 from getCount() from adapter that way viewpager will not know there are 200 pages.
EDIT:
Well In that case, you have to override viewpager and manually disable the motion when viewpager is showing second last page.
It would look something like this
First extend : class CustomViewPager extends ViewPager {...}
float lastX;
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if(getCurrentItem() == getAdapter().getCount()-1){
if(ev.getAction() == MotionEvent.ACTION_DOWN){
lastX = ev.getX();
// Just touch down let viewpager also handle touch event
return super.onInterceptTouchEvent(event);
}
else if(ev.getAction() == MotionEvent.ACTION_MOVE){
float xDiff = ev.getX() - lastX;
if(xDiff > 0){
// Attempt to move right on second last page
// Return false, we dont want to handle this movement
return false;
}
else{
// Left you can move
return super.onInterceptTouchEvent(event);
}
}
// Notice that we are not updating lastX on motion towards left
// This is to enable user can swipe towards left a bit and comeback, but dont dare go right
}
else{
// Not on second last page let viewpager handle page event
return super.onInterceptTouchEvent(event);
}
}

How to swipe with finger in a View flipper?

I have a view flipper containing two layouts with button. Now the issue is i am not able to swipe along with finger. Basically the view should swipe along with the finger. I hope you are getting what i am trying to say. Here is the onTouchevent code :-
bottomFormatFlipper = (ViewFlipper)findViewById(R.id.bottomFormatFlipper);
public boolean onTouchEvent(MotionEvent touchevent)
{
switch (touchevent.getAction())
{
// when user first touches the screen to swap
case MotionEvent.ACTION_DOWN:
{
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP:
{
float currentX = touchevent.getX();
// if left to right swipe on screen
if (lastX < currentX)
{
// If no more View/Child to flip
if (bottomFormatFlipper.getDisplayedChild() == 0)
break;
// set the required Animation type to ViewFlipper
// The Next screen will come in form Left and current Screen will go OUT from Right
bottomFormatFlipper.setInAnimation(this, R.anim.in_from_left);
bottomFormatFlipper.setOutAnimation(this, R.anim.out_to_right);
// Show the next Screen
bottomFormatFlipper.showNext();
}
// if right to left swipe on screen
if (lastX > currentX)
{
if (bottomFormatFlipper.getDisplayedChild() == 1)
break;
// set the required Animation type to ViewFlipper
// The Next screen will come in form Right and current Screen will go OUT from Left
bottomFormatFlipper.setInAnimation(this, R.anim.in_from_right);
bottomFormatFlipper.setOutAnimation(this, R.anim.out_to_left);
// Show The Previous Screen
bottomFormatFlipper.showPrevious();
}
break;
}
}
return false;
}
From this code, when i swipe, then after a second or so, the swipe animation occurs. I want that the view should swipe with my finger

Flip images using viewflipper in android without buttons

I am using the following method of flipping images: How to set dymanic images to ViewFlipper in android?
I do not want to flip using buttons. When the user touches the images or swipes the image I want to flip to the next image. Is there a way I can achieve this?
You can use ViewPager. See these links:
Implementing Horizontal View Swiping Using ViewPager and FragmentPagerAdapter in Android
Horizontal View Swiping with ViewPager, Updated
Android Viewpager as Image Slide Gallery (Swipe Gallery)
and this Library
Support Library
this might Help..... animator is a folder in res and the s_out_* is an xml file with a translate
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
if (lastX < currentX) {
if (viewFlipper.getDisplayedChild() == 0)
break;
viewFlipper.setInAnimation(this, R.animator.s_in_fleft);
viewFlipper.setOutAnimation(this, R.animator.s_out_right);
viewFlipper.showNext();
}
if (lastX > currentX) {
if (viewFlipper.getDisplayedChild() == 1)
break;
viewFlipper.setInAnimation(this, R.animator.s_in_fright);
viewFlipper.setOutAnimation(this, R.animator.s_out_left);
viewFlipper.showPrevious();
}
break;
}
}
return false;
}

Android ViewFlipper back to first view

My viewFlipper contains 15 LinearLayout.
After it reaches, I have a button "Back to menu".
I've used
showNext()
all the way to the 15th LinearLayout.
And now I want it to go back to 1st LinearLayout.
Anyone have any idea? How to bring it back to 1st Linearlayout?
Thanks.
Call showNext(). Or, call setDisplayedChild(0).
I used this code.
private ViewFlipper vf;
private float lastX;
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
if (lastX < currentX) {
vf.setInAnimation(this, R.anim.in_from_left);
vf.setOutAnimation(this, R.anim.out_to_right);
vf.showNext();
}
if (lastX > currentX) {
vf.setInAnimation(this, R.anim.in_from_right);
vf.setOutAnimation(this, R.anim.out_to_left);
vf.showPrevious();
}
break;
}

Categories

Resources