0

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!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

I did actually come up with a solution to my problem.

The main helper was Log.d(TAG, "Message");. With this I was able to check what navigation_sync, syncFragment and my toFragment variable actually holds and then it was straight forward. I found out that syncFragment and other objects hold the id of the fragment and I just had to compare that to the fragment id that was currently in FrameLayout. That can be done with this method:

public Fragment getFragmentId(){
    return getSupportFragmentManager().findFragmentById(R.id.mainFrame);
}

So I put this into my switch:

...
switch(getFragmentId()) {
    case syncFragment:
...

And I got an error saying:

Constant expression required

which I found out here can be solved by converting the switch to if-else structure which I did and it works.

Here is a full code for anybody who might run into this. Please note I had to change toFragment variable to Fragment variable. Also the order in which I set the statements in switch within onNavigationItemSelected is important.

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.util.Log;
import android.view.MenuItem;
import android.widget.FrameLayout;

public class SelectActivity extends AppCompatActivity {

private static final String TAG = "SelectActivity";
private BottomNavigationView mMainNavigation;
private FrameLayout mMainFrame;
private SyncFragment syncFragment;
private MessageFragment messageFragment;
private SettingsFragment settingsFragment;
private Fragment 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);
    toFragment = syncFragment;

    mMainNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch(item.getItemId()) {
                case R.id.navigation_sync:
                    toFragment = syncFragment;
                    setFragment(syncFragment);
                    return true;
                case R.id.navigation_message:
                    setFragment(messageFragment);
                    return true;
                case R.id.navigation_settings:
                    toFragment = settingsFragment;
                    setFragment(settingsFragment);
                    return true;
                default:
                    return false;
            }
        }
    });
}
private void setFragment(Fragment fragment) {
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    if (getFragmentId() == syncFragment) {
        fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
    }
    if (getFragmentId()==messageFragment) {
        if(toFragment == syncFragment) {
            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);
        }
    }
    if (getFragmentId() == settingsFragment) {
        fragmentTransaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right);
    }
    fragmentTransaction.replace (R.id.mainFrame, fragment);
    fragmentTransaction.commit();
}

public Fragment getFragmentId(){
    return getSupportFragmentManager().findFragmentById(R.id.mainFrame);
}
}