片段过渡动画在API级别22中导致ANR

时间:2018-11-14 06:45:14

标签: android android-fragments android-transitions

我有一个顶部的标签布局,当选择一个标签时,它会以左右滑动动画切换片段。

对于API级别22,以下代码会导致ANR:它不会给出任何错误消息,但是注释掉以下代码即可使其正常工作。

if (position > curSelectTab) {
        mFragments[curSelectTab].setExitTransition(inflater.inflateTransition(R.transition.slide_out_to_left));
        mFragments[position].setEnterTransition(inflater.inflateTransition(R.transition.slide_in_from_right));
    } else if (position < curSelectTab){
        mFragments[curSelectTab].setExitTransition(inflater.inflateTransition(R.transition.slide_out_to_right));
        mFragments[position].setEnterTransition(inflater.inflateTransition(R.transition.slide_in_from_left));
    }

这是我切换片段的方式:

private void onTabSelected(int position) {
    //switch tab
    int curSelectTab = mSelectTab;
    SearchResultFragment fragment = mFragments[position];
    TransitionInflater inflater = TransitionInflater.from(this);
    if (fragment == null) {
        switch (position){
            case 0:
                mFragments[0] = SearchResultFragment.newInstance(SearchResultFragment.TYPE_ALL_NOTE, mQuery);
                break;
            case 1:
                mFragments[1] = SearchResultFragment.newInstance(SearchResultFragment.TYPE_NOTE_NAME, mQuery);
                break;
            case 2:
                mFragments[2] = SearchResultFragment.newInstance(SearchResultFragment.TYPE_NOTE_CONTENT, mQuery);
                break;
            default:
                break;
        }
        fragment = mFragments[position];
    }

    //add Fragment
    if (!fragment.isAdded()) {
        getFragmentManager().beginTransaction().add(R.id.fl_container, fragment).commit();
    }

    if (position > curSelectTab) {
        mFragments[curSelectTab].setExitTransition(inflater.inflateTransition(R.transition.slide_out_to_left));
        mFragments[position].setEnterTransition(inflater.inflateTransition(R.transition.slide_in_from_right));
    } else if (position < curSelectTab){
        mFragments[curSelectTab].setExitTransition(inflater.inflateTransition(R.transition.slide_out_to_right));
        mFragments[position].setEnterTransition(inflater.inflateTransition(R.transition.slide_in_from_left));
    }
    getFragmentManager().beginTransaction().hide(mFragments[curSelectTab]).commit();
    getFragmentManager().beginTransaction().show(mFragments[position]).commit();
    mSelectTab = position;
}

这是我的过渡动画之一slide_in_from_right

<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <slide android:slideEdge="end" />
    <fade android:fadingMode="fade_in" android:duration="100"/>
</transitionSet>

ANR仅在API级别22发生,在API级别23很好。

有人知道为什么吗?在23以下的API级别中不支持过渡动画?我认为它在21岁以上受到支持

0 个答案:

没有答案