美文网首页
add fragment防止fragment重建,show和hi

add fragment防止fragment重建,show和hi

作者: PeterWu丷 | 来源:发表于2019-06-10 09:55 被阅读0次

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

    相关文章

      网友评论

          本文标题:add fragment防止fragment重建,show和hi

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