Reputation: 21
I am doing fragment animation during transaction but I am able to do only left slide in. Right slide animation does not affect anything. Please help: I have tried everything and searched on google and other stackoverflow questions and did all the things they suggested. Still right slide in animation is not doing anything.
Below is the fragment activity :
public class MainActivity extends FragmentActivity {
static FragmentManager manager;
String arr1[] = { "One", "Two", "Three", "Four", "Five", "Six", "Seven" };
String arr2[] = { "One2", "Two2", "Three2", "Four2", "Five2", "Six2",
"Seven2" };
String arr3[] = { "One23", "Two23", "Three23", "Four23", "Five23", "Six23",
"Seven23" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getSupportFragmentManager();
StartTransaction(arr1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public static void StartTransaction(String arr[]) {
FragmentOne one = new FragmentOne();
Bundle b = new Bundle();
b.putStringArray("array", arr);
one.setArguments(b);
FragmentTransaction transaction = manager.beginTransaction();
// PROblem is here
transaction.setCustomAnimations(R.anim.slide_right, R.anim.slide_left);// ,R.anim.slide_right,R.anim.slide_left);
transaction.replace(R.id.container, one);
// transaction.addToBackStack(null);
transaction.commit();
}
private void MoveOneLevelUp() {
FragmentOne one = new FragmentOne();
Bundle b = new Bundle();
b.putInt("moveup", 1);
one.setArguments(b);
FragmentTransaction transaction = manager.beginTransaction();
transaction.setCustomAnimations(R.anim.slide_left, R.anim.slide_right,
R.anim.slide_right, R.anim.slide_left);
transaction.replace(R.id.container, one);
transaction.addToBackStack(null);
transaction.commit();
}
}
Here is the Fragment code :
public class FragmentOne extends Fragment implements OnItemClickListener {
Activity activity;
ListView list;
View v;
String arr1[] = {"One","Two","Three","Four","Five","Six","Seven"};
String arr2[] = { "One2", "Two2", "Three2", "Four2", "Five2", "Six2",
"Seven2" };
String arr3[] = { "One23", "Two23", "Three23", "Four23", "Five23", "Six23",
"Seven23" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
activity = getActivity();
String array[] = getArguments().getStringArray("array");
v = LayoutInflater.from(activity).inflate(R.layout.first_view, null);
list = (ListView)v.findViewById(R.id.listview);
list.setOnItemClickListener(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
list.setAdapter(adapter);
return v;
// return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if(position ==2) {
MainActivity.StartTransaction(arr2);
} else {
MainActivity.StartTransaction(arr3);
}
}
}
Slide left anim xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="700"
android:fromXDelta="-100%"
android:toXDelta="0%" >
</translate>
</set>
Slide right xml :
<?xml version="1.0" encoding="utf-8"?>`enter code here`
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="700"
android:fromXDelta="0%"
android:toXDelta="100%" >
</translate>
</set>
Upvotes: 2
Views: 6565
Reputation: 128
You need a fragment transaction.
Try something like this:
if (mLastMenuWeight > mMenuWeight) {
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
}
else {
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
}
Keep in mind that mLastMenuWeight and mMenuWeight are used by me to track which animation should I use. You will need to replace this condition with something else.
Since android includes only left-to-right animations, you have to define the right-to-left on your own, using the original animation code. Like this:
slide_in_right.xml:
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/res/anim/slide_in_left.xml
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="50%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
slide_out_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/res/anim/slide_out_right.xml
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-50%p"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
Also, you may try to add/attach the new view and detach the old one instead of replace. Perhaps it will result with the same behavior anyway...
Upvotes: 2