How can I do a animation that pushes the current fragment by the next fragment
Here is the Animation that I want:
My current animation code just overlaps the first fragment by the second fragment it didnt push it just like in the picture
Here is the code:
result_list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> av, final View view,
final int i, long i2) {
result_list.setEnabled(false);
view.animate().setDuration(300).translationX(widthListView).alpha(0).
withEndAction(new Runnable() {
@Override
public void run() {
//setResult(Activity.RESULT_OK,new Intent().putExtra("bussStopCode", data.get(i).getStopCode()).putExtra("bussStopName", data.get(i).getStopName()));
////int get 1
//data.remove(i);
int temporaryInteger = i;
listLastPostion = temporaryInteger;
//customAdapter.notifyDataSetChanged();
//view.setTranslationX(0);
Log.d("data",conreq.getCollectedData().getBusRouteSetData().get(temporaryInteger - 1).getRouteHeading());
Bundle bundle = new Bundle();
bundle.putString("busdestination", conreq.getCollectedData().getBusRouteSetData().get(temporaryInteger-1).getRouteHeading());
bundle.putString("busnumber", conreq.getCollectedData().getBusRouteSetData().get(temporaryInteger-1).getRouteNo());
Fragment fragment = new FragmentNextTripForStop();
fragment.setArguments(bundle);
FragmentTransaction fragmentManager = getFragmentManager().beginTransaction();
fragmentManager.setCustomAnimations(R.anim.right_left_anim_x_left,R.anim.right_left_anim_x_right,R.anim.left_right_anim_x_left,R.anim.left_right_anim_x_right);
fragmentManager.add(R.id.fragment_searched_data_xml, fragment).addToBackStack(null).commit();
// finish();
//overridePendingTransition(R.anim.right_left_anim_x_left,R.anim.right_left_anim_x_right);
}
});
}});
Although this question is a duplicate, the answer that was provided on that question does not work for many viewers and requires them to make a few assumptions on their end.
This is an excellent question considering this functionality exists in many applications, however it requires a very elaborate answer. I will try to break the answer down into a series of compartmentalized steps to ensure that it is repeatable!
The Problem
The problem inhibiting most of us from doing this easily is one of a compound nature. To fully understand this problem, let me list the issues:
FragmentManager
andFragmentTransaction
classes provide a convenience method,setCustomAnimations(int, int, int int)
, that requireObjectAnimators
ObjectAnimators
(the traditional approach)The Solution
In order to tackle this problem and provide the immersive experience desired, we must tackle the it from many angles. These next few steps explain exactly how to acquire this functionality!
We first need to define a custom layout for each
Fragment
as we must have runtime access to the width of the screen, and a self-contained method to manipulate the x-position of theView
(the layout) based on this width.FractionTranslateLinearLayout.java
Now, since we have a special layout that will allow us to translate based on the physical width of the screen, we can use it in the associated Fragment XML files.
fragment_1.xml
fragment_2.xml
Then, we must create the
Fragment
classes that will contain the logic to implement the transitions.Fragment1.java
Fragment2.java
Let's now create the
objectAnimator
.xml files that we will use to translate theView
s across the screen. Note that we will need four of these files because we need one for each process (out and in), and one for each side (left and right).slide_left_out.xml
slide_right_out.xml
slide_left_in.xml
slide_right_in.xml
Note that these folders must be placed in the 'res/animator' directory in your project structure.
Create the container layout that will hold each
Fragment
as we transition between them.main.xml
Now, we must create the
Activity
that will wrap everything together!Main.java
In this code example, this class is used heavily. Its methods are crucial to the execution of the process!
Some Considerations
Note that I have made a few assumptions here, and that this code is quite unique:
Fragment
class, and thus will not work with the support package unless the necessary modifications are made.Android API 13.0
or above.Hopefully there is sufficient detail here to answer your question. Please let me know if you require any more.
At this point, there should be enough code to implement your own twist on the solution. I did type this up without the crutch of the Eclipse editor, so if there are any errors, please accept my apologies in advance!