add fragment防止fragment重建,show和hide的使用

(1)布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".main.activity.MainActivity">

    <LinearLayout
            android:id="@+id/mainLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true">

        <RadioGroup
                android:id="@+id/mainRadioGroup"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

            <RadioButton
                    android:id="@+id/mainNews"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:button="@null"
                    android:text="@string/news"
                    android:textSize="12sp"
                    android:drawableTop="@drawable/btn_main_news_selector"
                    android:gravity="center"
                    android:layout_marginTop="5dp"/>

            <RadioButton
                    android:id="@+id/mainVideo"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:button="@null"
                    android:text="@string/video"
                    android:textSize="12sp"
                    android:drawableTop="@drawable/btn_main_video_selector"
                    android:gravity="center"
                    android:layout_marginTop="5dp"/>

            <ImageView
                    android:id="@+id/mainPush"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:src="@drawable/icon_push"
                    android:padding="5dp"
                    android:gravity="center"/>

            <RadioButton
                    android:id="@+id/mainCommunity"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:button="@null"
                    android:text="@string/community"
                    android:textSize="12sp"
                    android:drawableTop="@drawable/btn_main_community_selector"
                    android:gravity="center"
                    android:layout_marginTop="5dp"/>

            <RadioButton
                    android:id="@+id/mainUser"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:button="@null"
                    android:text="@string/user"
                    android:textSize="12sp"
                    android:drawableTop="@drawable/btn_main_user_selector"
                    android:gravity="center"
                    android:layout_marginTop="5dp"/>

        </RadioGroup>

    </LinearLayout>

    <FrameLayout
            android:id="@+id/mainFrame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_above="@id/mainLinearLayout">

    </FrameLayout>

    <View
            android:layout_below="@id/mainFrame"
            android:background="@color/tab_item_text_color"
            android:layout_width="match_parent"
            android:layout_height="0.1dp"/>

</RelativeLayout>

(2)MainActivity中切换

class MainActivity : BaseActivity<MainView, MainPresenter>() {

    private lateinit var fTransition: FragmentTransaction

    var currentFragment = Fragment()

    override fun initData() {
    }

    override fun initView() {
        val mNewsFragment = NewsFragment()
        val mVideoFragment = VideoFragment()
        val mCommunityFragment = CommunityFragment()
        val mUserFragment = UserFragment()

        mainRadioGroup.setOnCheckedChangeListener { _, checkedId ->
            var mFragment: Fragment? = null
            when(checkedId){
                R.id.mainNews -> {
                    mFragment = mNewsFragment
                }
                R.id.mainVideo -> {
                    mFragment = mVideoFragment
                }
                R.id.mainCommunity -> {
                    mFragment = mCommunityFragment
                }
                R.id.mainUser -> {
                    mFragment = mUserFragment
                }
            }
            if (mFragment != null) {
                switchFragment(mFragment)
            }
        }
        mainRadioGroup.check(R.id.mainNews)
    }

    private fun switchFragment(fragment: Fragment){
        fTransition = supportFragmentManager.beginTransaction()
        if (currentFragment != fragment){
            fTransition.hide(currentFragment)
            currentFragment = fragment
            if (!fragment.isAdded){
                fTransition.add(R.id.mainFrame,fragment).show(fragment).commit()
            }else{
                fTransition.show(fragment).commit()
            }
        }
    }

    override fun getLayoutId() = R.layout.activity_main

    override fun setupPresenter(): MainPresenter? = MainPresenter(applicationContext)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Fragment在现在的android开发中已经很常见了,基本所有的app都会涉及到。在Fragment的切换中...
    yk2fxy阅读 5,462评论 0 5
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,497评论 0 17
  • 本人初学Android,最近做了一个实现安卓简单音乐播放功能的播放器,收获不少,于是便记录下来自己的思路与知识总结...
    落日柳风阅读 19,210评论 2 41
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,786评论 1 92
  • 谈到阅读,我挺有感触的,本没有读几本书,认真读了两三遍的书只有那本《不要用勤奋掩饰你的懒惰》,可是现在也记不得它讲...
    且试风夕阅读 122评论 0 2