I have an app with Bottom navigation which changes between three fragments. I added transition when I click the menu item and I would like to be able to customize them more so that when I go from the left item (navigation_sync) to the right (navigation_message or navigation_settings) the transition goes from right to left and vice versa. I came up with comparing the content of my FrameLayout to the fragment I want to change to via switch but I'm at loss of how to actually do that.
SelectActivity.java
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.FrameLayout;
public class SelectActivity extends AppCompatActivity {
private BottomNavigationView mMainNavigation;
private FrameLayout mMainFrame;
private SyncFragment syncFragment;
private MessageFragment messageFragment;
private SettingsFragment settingsFragment;
int toFragment;
@Override
public void onBackPressed() {
moveTaskToBack(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
mMainFrame = (FrameLayout) findViewById(R.id.mainFrame);
mMainNavigation = (BottomNavigationView) findViewById(R.id.navigation);
syncFragment = new SyncFragment();
messageFragment = new MessageFragment();
settingsFragment = new SettingsFragment();
setFragment(syncFragment);
mMainNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()) {
case R.id.navigation_sync:
setFragment(syncFragment);
toFragment = R.id.navigation_sync;
return true;
case R.id.navigation_message:
setFragment(messageFragment);
toFragment = R.id.navigation_message;
return true;
case R.id.navigation_settings:
setFragment(settingsFragment);
toFragment = R.id.navigation_settings;
return true;
default:
return false;
}
}
});
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
switch(R.id.mainFrame) {
case R.id.navigation_sync:
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
case R.id.navigation_message:
if(toFragment == R.id.navigation_sync) {
fragmentTransaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right);
} else {
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
}
case R.id.navigation_settings:
fragmentTransaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right);
}
//fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
fragmentTransaction.replace (R.id.mainFrame, fragment);
fragmentTransaction.commit();
}
}
This does not read any of the fragmentTransaction.setCustomAnimations so I think I'm comparing bad values? Please help!