Reputation: 1430
I have an activity (A) with a listview that opens a second activity (B) displaying details related to the clicked row. The activity (B) is opened with the following code :
Intent ident = new Intent(getApplicationContext(), DetailedActivity.class);
ident.putExtra("id", id);
startActivity(ident);
What I want is when the activity (B) is opened, to be able to swipe left or right to display detail to the previous/next ListView row. I can't find a way to use the ViewFlipper to reload the same View for a different id...
any ideas ?
Upvotes: 1
Views: 6523
Reputation: 1430
I just find out all I needed here: ViewPager It doesn't take too much effort to implement a nice effect.
Upvotes: 2
Reputation: 46846
I don't think a ViewFlipper is what you'll want to use. You can use a GestureDector to get callbacks for left and right swipe motion. Then in your callback you can do something like this:
OnRightSwipe(){
Intent ident = new Intent(YourActivity.this, DetailedActivity.class);
ident.putExtra("id", this.getIntent().getExtra("id") + 1);
startActivity(ident);
}
OnLeftSwipe(){
Intent ident = new Intent(YourActivity.this, DetailedActivity.class);
ident.putExtra("id", this.getIntent().getExtra("id") - 1);
startActivity(ident);
}
Notes:
OnRightSwipe and OnLeftSwipe are not the actual method names you'll have to lookup what the callback is I don't have an example handy.
This only works if the IDs that you are passing are numeric in some way that you can increment and decrement. If that is not the case, you'll have to post some more of your code in order for me to help. Also if your IDs are stored as a string extra then you'll have to parse to int before you can increment/decrement.
and lastly note the use of YourActivity.this instead of getApplicationContext(). Since the activity you are in is a Context there is no need to use this method for this purpose. It is somewhat over my head, but I am led to believe that the methods getApplicationContext() and getBaseContext() can lead to problems.
Edit:
If you mean something similar to how the home screen scrolls then I think there is no built in widget that will give you that effect. The best way to get it seems to be looking at how they did it in the homescreen source code and replicate what the did then try to fit it into your own layout situation.
If you aren't looking for it to "follow along" with the swiping finger:
If the two things you are trying to flip between are separate activities then a ViewFlipper won't work for that. A ViewFlipper is used to flip between two different Layout objects within the same activity.
I think you could probably get close to achieving a similar effect between two activities but there are a few things you should consider before deciding on doing it this way.
To achieve a slide in slide out type animation between two activities what you could do is just in your xml set your root layout to android:visibility="gone"
then inside the onCreate() after you've called setContentView(R.id.yourLayout);
set up your slideInAnimation and get a reference to your root layout and start the slide in on it. Then right before you are calling startActivity() in your swipe listener(You'll have to set an animationListener to get a callback for after the animation completes inside that callback is where you'll need to move the startActivity() call to) you can set up an start the out animation on your root layout reference.
However I would probably suggest in no matter which of the above your looking for you should consider a model where your detail activity doesn't simply show 1 static record and you start a new activity to show the previous or next. Instead it is able to use a ViewFlipper to cycle through them all within that one instance of the detail activity. If you use this way then you'll be using a ViewFlipper and use yes you can use it in conjunction with the fling listener. You can mash them up yourself or check out the ViewSwiper that @CommonsGuy made It is built for just that and comes ready to drop in toy our project.
Upvotes: 5