将FrameLayout中的片段与另一个片段进行比较

时间:2018-08-26 14:38:29

标签: android android-fragments bottomnavigationview

我有一个带有底部导航的应用程序,该应用程序在三个片段之间切换。当我单击菜单项时,我添加了过渡,并且我希望能够对其进行更多自定义,以便当我从左侧的项( navigation_sync )移至右侧的 navigation_message navigation_settings ),过渡过程从右到左,反之亦然。我想将FrameLayout的内容与要通过switch更改为片段的内容进行比较,但是我对实际操作方法一无所知。

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();
}
}

这不读取任何 fragmentTransaction.setCustomAnimations ,所以我认为我在比较错误的值?请帮忙!

1 个答案:

答案 0 :(得分:0)

我确实确实提出了解决问题的方法。

主要帮助者是Log.d(TAG, "Message");。有了这个,我就可以检查navigation_syncsyncFragment和我的toFragment变量的实际含义,然后就可以了。我发现syncFragment和其他对象拥有片段的ID,而我只需要将其与FrameLayout中当前的片段ID进行比较。可以使用以下方法完成:

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

所以我把它放到了我的交换机中:

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

我收到一条错误消息:

  

需要常量表达

我发现here

可以通过将我所做的switch转换为if-else结构来解决。

这是所有可能遇到此问题的人的完整代码。请注意,我必须将toFragment变量更改为Fragment变量。同样,我在onNavigationItemSelected中在switch中设置语句的顺序也很重要。

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);
}
}