美文网首页
展示多个fragment的Activiy,支持fragment之

展示多个fragment的Activiy,支持fragment之

作者: Yang仔 | 来源:发表于2021-03-16 14:14 被阅读0次

    传入需要展示的Fragment的class对象,通过反射方式创建Fragment对象展示,再次调用start(Context, Class<?>)方法启动,可以在原来的fragment上层再显示一个,返回键可以返回上个fragment。且有切换动画

    package com.android.jsbcmasterapp.base;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Build;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.text.TextUtils;
    import android.util.Log;
    
    import com.android.jsbcmasterapp.R;
    import com.android.jsbcmasterapp.Res;
    
    /**
     * <pre>
     * Description: 展示activity用的宿主;
     * 调用{@link #start(Context, Class)}方法,传入需要展示的Fragment的class,
     * singleInstance启动模式,支持启动多次展示多个fragment支持返回
     *
     * Author: xucunyang
     * Time: 2021/3/4 15:45
     * </pre>
     */
    public class FragmentDisplayActivity extends BaseCompatActivity {
        private static final String TAG = "FragmentDisplayActivity";
    
        private static final String KEY_FRAGMENT_NAME = "fragment_name";
    
        private FragmentManager fragmentManager;
    
        private String mFragmentPath;
        private FragmentTransaction mFragmentTransaction;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mFragmentPath = getIntent().getStringExtra(KEY_FRAGMENT_NAME);
                fromNewIntent = false;
                super.onCreate(savedInstanceState);
            }
            Log.i(TAG, "onCreate: ");
        }
    
        @Override
        protected int getContentViews() {
            return R.layout.activity_display_fragment;
        }
    
        @Override
        protected void initViews() {}
    
        @Override
        protected void initListener() {}
    
        @Override
        protected void initData() {
            Log.i(TAG, "initData: ");
            if (TextUtils.isEmpty(mFragmentPath)) {
                Log.w(TAG, "initData: fragmentPath is empty");
                finish();
                return;
            }
    
            Log.i(TAG, "initData: show fragment:" + mFragmentPath);
    
            try {
                Class<?> clazz = Class.forName(mFragmentPath);
                fragment = (BaseFragment) clazz.newInstance();
                fragmentManager = getSupportFragmentManager();
                mFragmentTransaction = fragmentManager.beginTransaction();
                if (fromNewIntent) {
                    mFragmentTransaction.setCustomAnimations(
                            R.anim.slide_enter,
                            R.anim.slide_exit,
                            R.anim.pop_left_enter,
                            R.anim.pop_left_exit
                    );
                }
                mFragmentTransaction.replace(Res.getWidgetID("frame_layout"), fragment);
                mFragmentTransaction.addToBackStack(mFragmentPath);
                mFragmentTransaction.commit();
            } catch (ClassNotFoundException e) {
                Log.w(TAG, "initData: ClassNotFoundException:" + e.getMessage());
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                Log.w(TAG, "initData: IllegalAccessException:" + e.getMessage());
                e.printStackTrace();
            } catch (InstantiationException e) {
                Log.w(TAG, "initData: InstantiationException:" + e.getMessage());
                e.printStackTrace();
            }
        }
    
        private boolean fromNewIntent = false;
    
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
    
            Log.i(TAG, "onNewIntent: " + intent);
    
            fromNewIntent = true;
            if (intent != null) {
                mFragmentPath = intent.getStringExtra(KEY_FRAGMENT_NAME);
                initData();
            } else {
                Log.i(TAG, "onNewIntent: intent is null");
            }
        }
    
        @Override
        public void onBackPressed() {
            Log.w(TAG, "onBackPressed: " + fragmentManager.getBackStackEntryCount());
            if (fragmentManager != null && fragmentManager.getBackStackEntryCount() == 1) {
                finish();
            } else {
                super.onBackPressed();
            }
        }
    
        @Override
        public void finish() {
            FragmentManager fragmentManager = getSupportFragmentManager();
            mFragmentTransaction = fragmentManager.beginTransaction();
            mFragmentTransaction.setCustomAnimations(0, 0);
            mFragmentTransaction.commit();
    
            super.finish();
    
            overridePendingTransition(R.anim.pop_left_enter, R.anim.pop_left_exit);
        }
    
        @Override
        public void startActivity(Intent intent) {
            super.startActivity(intent);
            overridePendingTransition(R.anim.slide_enter, R.anim.activity_start_outgoing);
        }
    
        /**
         * 展示fragment
         *
         * @param context     上下文
         * @param fragmentCls fragment
         */
        public static void start(Context context, Class<? extends Fragment> fragmentCls) {
            if (fragmentCls != null) {
                Log.i(TAG, "start: " + fragmentCls.getName());
                Intent intent = new Intent(context, FragmentDisplayActivity.class);
                intent.putExtra(KEY_FRAGMENT_NAME, fragmentCls.getName());
                context.startActivity(intent);
    
                if (context instanceof Activity) {
                    ((Activity) context).overridePendingTransition(R.anim.slide_enter, R.anim.activity_start_outgoing);
                } else {
                    Log.w(TAG, "start: context is not instance of Activity, " + context);
                }
            } else {
                Log.w(TAG, "start: fragment is null");
            }
        }
    }
    

    使用的切换动画文件
    activity_start_outgoing.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <translate
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromXDelta="0%"
            android:toXDelta="-100%" />
    
        <alpha
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromAlpha="1.0"
            android:toAlpha="0.3" />
    
    </set>
    

    pop_left_enter.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <translate
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromXDelta="-100%p"
            android:toXDelta="0%p" />
    
        <alpha
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromAlpha="0.3"
            android:toAlpha="1.0" />
    
    </set>
    

    pop_left_exit.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <translate
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromXDelta="0%p"
            android:toXDelta="100%p" />
    
        <alpha
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromAlpha="1.0"
            android:toAlpha="0.3" />
    
    </set>
    

    slide_enter.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <translate
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromXDelta="100%p"
            android:toXDelta="0%p" />
    
        <alpha
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromAlpha="0.3"
            android:toAlpha="1.0" />
    
    </set>
    

    slide_exit.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <translate
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromXDelta="0%"
            android:toXDelta="-100%" />
    
        <alpha
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromAlpha="1.0"
            android:toAlpha="0.3" />
    
    </set>
    

    相关文章

      网友评论

          本文标题:展示多个fragment的Activiy,支持fragment之

          本文链接:https://www.haomeiwen.com/subject/zpzrcltx.html