码迷,mamicode.com
首页 > 其他好文 > 详细

2、fragment的使用封装(下)

时间:2016-05-12 22:49:47      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

转载请注明出处:http://blog.csdn.net/q649381130/article/details/51346551

1、写在前面的话

上一篇已经介绍了fragment的使用封装,读者可以点击链接查看fragment的使用封装(上):http://blog.csdn.net/q649381130/article/details/51340544

本文主要采用其他方式,实现对fragment的添加切换操作。效果图就不上了,跟上篇一样。

2、传统方式

在Activity中我们添加一下常量,表示对于fragment的tag标示,用枚举一样的效果。

 public static final String FRAM_SET_HOME = "设置主页";
    public static final String FRAM_SET_ALBUM = "相册";
    public static final String FRAM_SET_COLLECT = "收藏";
    public static final String FRAM_SET_WALLET = "钱包";
    public static final String FRAM_SET_COUPON = "卡劵";
    public static final String FRAM_SET_FACE = "表情";
    public static final String FRAM_SET_PERSON = "个人";
    public static final String FRAM_SET_SYS = "系统设置";

提供一个工厂方法,根据传入的tag进行fragment对象的创建

 private Fragment fragmentFactory(String tag)
    {
        Fragment fragment = null;

        switch (tag)
        {
        case FRAM_SET_HOME://主页

            fragment = new SettingHomeFragment();

            break;
        case FRAM_SET_ALBUM://相册

            fragment = new SettingAlbumFragment();

            break;
        case FRAM_SET_COLLECT://收藏

            fragment = new SettingCollectionFragment();

            break;
        case FRAM_SET_COUPON://卡劵

            fragment = new SettingCouponFragment();

            break;
        case FRAM_SET_FACE://表情

            fragment = new SettingFaceFragment();

            break;
        case FRAM_SET_PERSON://个人

            fragment = new SettingPersonFragment();

            break;
        case FRAM_SET_WALLET://钱包

            fragment = new SettingWalletFragment();

            break;
        case FRAM_SET_SYS://系统设置

            break;

        default:

            Log.e("fragmentFactory", "找不到指定tag+["+tag+"]+的fragment");

            break;
        }

        return fragment;
    }

提供一个切换显示fragment的方法,传入tag值,根据tag值显示对应的页面。首先我们先去栈中查找看是否存在对应的fragment,如果已经存在就直接显示隐藏,否则使用工厂方法创建实例在add进来,add的时候将tag与新建的fragment对象对应起来。

public void replaceFragment(String tag)
    {
        Fragment fragment = mFragmentManager.findFragmentByTag(tag);

        if (fragment != null)
        {
            mFragmentManager.beginTransaction()
                    .show(fragment)
                    .hide(mCurrentFragment)
                    .commitAllowingStateLoss();
        } else
        {
            fragment = fragmentFactory(tag);

            if (fragment == null)
            {
                return;
            }

            mFragmentManager.beginTransaction()
                    .add(R.id.rl_container_main, fragment, tag)
                    .hide(mCurrentFragment)
                    .commit();
        }

        mCurrentFragment = fragment;
    }

完整代码业务逻辑还是很简单的,这里我们重写了onBackpress()方法,控制当用户点击返回键时,如果当前页面是列表页则退出Activity,否则切换显示列表页面。

当然还要重写onSaveInstanceState(),防止重复显示的bug。具体解释看我的上一篇博客。

public class MainActivity extends FragmentActivity
{
    private FragmentManager mFragmentManager;

    // 当前显示的fragment
    Fragment mCurrentFragment;

    public static final String FRAM_SET_HOME = "设置主页";
    public static final String FRAM_SET_ALBUM = "相册";
    public static final String FRAM_SET_COLLECT = "收藏";
    public static final String FRAM_SET_WALLET = "钱包";
    public static final String FRAM_SET_COUPON = "卡劵";
    public static final String FRAM_SET_FACE = "表情";
    public static final String FRAM_SET_PERSON = "个人";
    public static final String FRAM_SET_SYS = "系统设置";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.empty_activity);

        mFragmentManager = getSupportFragmentManager();

        // 添加主页面
        SettingHomeFragment settingHomeFragment = new SettingHomeFragment();
        mFragmentManager.beginTransaction()
                .add(R.id.rl_container_main, settingHomeFragment, FRAM_SET_HOME)
                .commitAllowingStateLoss();

        mCurrentFragment = settingHomeFragment;
    }

    @Override
    public void onBackPressed()
    {
        if (mCurrentFragment instanceof SettingHomeFragment)
        {
            finish();
        } else
        {
            replaceFragment(FRAM_SET_HOME);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle arg0)
    {
        // super.onSaveInstanceState(arg0);
    }

    public void replaceFragment(String tag)
    {
        Fragment fragment = mFragmentManager.findFragmentByTag(tag);

        if (fragment != null)
        {
            mFragmentManager.beginTransaction()
                    .show(fragment)
                    .hide(mCurrentFragment)
                    .commitAllowingStateLoss();
        } else
        {
            fragment = fragmentFactory(tag);

            if (fragment == null)
            {
                return;
            }

            mFragmentManager.beginTransaction()
                    .add(R.id.rl_container_main, fragment, tag)
                    .hide(mCurrentFragment)
                    .commit();
        }

        mCurrentFragment = fragment;
    }

    private Fragment fragmentFactory(String tag)
    {
        Fragment fragment = null;

        switch (tag)
        {
        case FRAM_SET_HOME:// 主页

            fragment = new SettingHomeFragment();

            break;
        case FRAM_SET_ALBUM:// 相册

            fragment = new SettingAlbumFragment();

            break;
        case FRAM_SET_COLLECT:// 收藏

            fragment = new SettingCollectionFragment();

            break;
        case FRAM_SET_COUPON:// 卡劵

            fragment = new SettingCouponFragment();

            break;
        case FRAM_SET_FACE:// 表情

            fragment = new SettingFaceFragment();

            break;
        case FRAM_SET_PERSON:// 个人

            fragment = new SettingPersonFragment();

            break;
        case FRAM_SET_WALLET:// 钱包

            fragment = new SettingWalletFragment();

            break;
        case FRAM_SET_SYS:// 系统设置

            break;

        default:

            Log.e("fragmentFactory", "找不到指定tag+[" + tag + "]+的fragment");

            break;
        }

        return fragment;
    }
}

fragmnet页面与上篇博客类似,为了篇幅只贴上SettingHomeFragment的点击事件的处理,文章最后会提供完整代码下载地址

 @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
        case R.id.tv_person_setting_home:// 个人信息设置

            mActivity.replaceFragment(MainActivity.FRAM_SET_PERSON);

            break;
        case R.id.tv_album_setting_home:// 相册

            mActivity.replaceFragment(MainActivity.FRAM_SET_ALBUM);

            break;
        case R.id.tv_collection_setting_home:// 收藏

            mActivity.replaceFragment(MainActivity.FRAM_SET_COLLECT);

            break;
        case R.id.tv_wallet_setting_home:// 钱包

            mActivity.replaceFragment(MainActivity.FRAM_SET_WALLET);

            break;
        case R.id.tv_coupon_setting_home:// 卡卷

            mActivity.replaceFragment(MainActivity.FRAM_SET_COUPON);

            break;
        case R.id.tv_face_setting_home:// 表情

            mActivity.replaceFragment(MainActivity.FRAM_SET_FACE);

            break;
        case R.id.tv_sysset_setting_home:// 系统设置

            mActivity.replaceFragment(MainActivity.FRAM_SET_SYS);

            break;

        default:
            break;
        }
    }

3、文艺式

文艺式的精髓就是使用指定view的id作为tag,这样实现对代码的最大化精简,减少配置参数。

先看HomeSettingFragment中的代码,在自定义点击事件中,我们直接调用MainActivity的replace方式,传入被点击的view的id即可。

public class SettingHomeFragment1 extends Fragment
{
    /**
     * 跟布局view
     */
    View mRootView;

    private MainActivity1 mActivity;

    private  OnClickListener mClickListener = new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            mActivity.replaceFragment(v.getId());
        }
    };

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState)
    {
        mRootView = inflater.inflate(R.layout.setting_home_fragment, container, false);

        mActivity = (MainActivity1) getActivity();

        setListener();

        return mRootView;
    }

    private void setListener()
    {
        mRootView.findViewById(R.id.tv_person_setting_home).setOnClickListener(mClickListener);
        mRootView.findViewById(R.id.tv_album_setting_home).setOnClickListener(mClickListener);
        mRootView.findViewById(R.id.tv_collection_setting_home).setOnClickListener(mClickListener);
        mRootView.findViewById(R.id.tv_wallet_setting_home).setOnClickListener(mClickListener);
        mRootView.findViewById(R.id.tv_coupon_setting_home).setOnClickListener(mClickListener);
        mRootView.findViewById(R.id.tv_face_setting_home).setOnClickListener(mClickListener);
        mRootView.findViewById(R.id.tv_sysset_setting_home).setOnClickListener(mClickListener);
    }
}

然后在MainActivity中,我们根据传入的id进行fragment的创建,添加,查找。大量减少一些常量,可以降低在出现差错的风险。

public class MainActivity1 extends FragmentActivity
{
    private FragmentManager mFragmentManager;

    // 当前显示的fragment
    Fragment mCurrentFragment;

    private static final int HOME_TAG = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.empty_activity);

        mFragmentManager = getSupportFragmentManager();

        // 添加主页面
        SettingHomeFragment1 settingHomeFragment = new SettingHomeFragment1();
        mFragmentManager.beginTransaction()
                .add(R.id.rl_container_main, settingHomeFragment, String.valueOf(HOME_TAG))
                .commitAllowingStateLoss();

        mCurrentFragment = settingHomeFragment;
    }

    @Override
    public void onBackPressed()
    {
        if (mCurrentFragment instanceof SettingHomeFragment1)
        {
            finish();
        } else
        {
            replaceFragment(HOME_TAG);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle arg0)
    {
        // super.onSaveInstanceState(arg0);
    }

    public void replaceFragment(int  id)
    {
        Fragment fragment = mFragmentManager.findFragmentByTag(String.valueOf(id));

        if (fragment != null)
        {
            mFragmentManager.beginTransaction()
                    .show(fragment)
                    .hide(mCurrentFragment)
                    .commitAllowingStateLoss();
        } else
        {
            fragment = fragmentFactory(id);

            if (fragment == null)
            {
                return;
            }

            mFragmentManager.beginTransaction()
                    .add(R.id.rl_container_main, fragment, String.valueOf(id))
                    .hide(mCurrentFragment)
                    .commit();
        }

        mCurrentFragment = fragment;
    }

    private Fragment fragmentFactory(int id)
    {
        Fragment fragment = null;

        switch (id)
        {
        case R.id.tv_album_setting_home:// 相册

            fragment = new SettingAlbumFragment();

            break;
        case R.id.tv_collection_setting_home:// 收藏

            fragment = new SettingCollectionFragment();

            break;
        case R.id.tv_coupon_setting_home:// 卡劵

            fragment = new SettingCouponFragment();

            break;
        case R.id.tv_face_setting_home:// 表情

            fragment = new SettingFaceFragment();

            break;
        case R.id.tv_person_setting_home:// 个人

            fragment = new SettingPersonFragment();

            break;
        case R.id.tv_wallet_setting_home:// 钱包

            fragment = new SettingWalletFragment();

            break;
        case R.id.tv_sysset_setting_home:// 系统设置

            fragment = new SettingSysFragment();

            break;

        default:

            break;
        }

        return fragment;
    }
}

最后奉上demo下载地址
敬请期待我的下一篇博客,fragment的封装。

2、fragment的使用封装(下)

标签:

原文地址:http://blog.csdn.net/q649381130/article/details/51346551

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!