Android - 底部导航标签调用的片段每次都在重新加载

时间:2018-05-02 19:06:59

标签: android android-fragments kotlin bottomnavigationview

我正在创建一个使用底部导航的项目。 我正在使用片段进行标签调用

val transaction = supportFragmentManager.beginTransaction()
transaction.add(R.id.frame_layout, Fragment1.newInstance(),Fragment1.newInstance().TAG)
transaction.addToBackStack(null).commit()

bottomNavigationView.setOnNavigationItemSelectedListener(object : BottomNavigationView.OnNavigationItemSelectedListener {

        override fun onNavigationItemSelected(item: MenuItem): Boolean {

            if(item.itemId == lastSelectedItemId){
                return true
            }

            var menuTag:String ?= null
            var selectedFragment: Fragment? = null
            lastSelectedItemId = item.itemId

            when (item.itemId) {
                R.id.tab1 -> {
                    selectedFragment = Fragment1.newInstance()
                    menuTag = selectedFragment.TAG
                }
                R.id.tab2 -> {
                    selectedFragment = Fragment2.newInstance()
                    menuTag = selectedFragment.TAG
                }
                R.id.tab3 -> {
                    selectedFragment = Fragment3.newInstance()
                    menuTag = selectedFragment.TAG
                }
                R.id.tab4 ->{
                    selectedFragment = Fragment4.newInstance()
                    menuTag = selectedFragment.TAG
                }
            }

            val transaction = supportFragmentManager.beginTransaction()
            transaction.replace(R.id.frame_layout, selectedFragment,menuTag)
            transaction.addToBackStack(menuTag).commit()
            return true
        }
    })

但片段总是重新加载 我累了一些方法

  1. 替换()。addToBackStack(空)
  2. 替换()。addToBackStack(标签)
  3. 添加,隐藏,显示
  4. addToBackStack() - 使用这个,当我点击android默认的底部后退按钮时,它弄乱了我的片段布局。与return @ OnNavigationItemSelectedListener相同

    我跟随了许多与此问题相关的论坛,但在我的情况下,没有任何工作。

    我的第四个片段是

    class Fragment4 : Fragment() {
    
    var listOfMoreMenus = ArrayList<MoreLinksObject>()
    var adapter: MoreLinksAdapter?=null
    public var TAG: String = "more"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
    
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val moreView = inflater.inflate(R.layout.fragment_more, container, false)
        val listView = moreView.findViewById(R.id.moreLinks) as ListView
    
        listOfMoreMenus.add(MoreLinksObject("test1","link1")) 
        listOfMoreMenus.add(MoreLinksObject("test2","link2")) 
        listOfMoreMenus.add(MoreLinksObject("test3","link3")) 
        listView.setNestedScrollingEnabled(true)
        adapter = MoreLinksAdapter(getActivity()!!,listOfMoreMenus)
        listView.adapter = adapter
        return moreView
    
    }
    
    companion object {
        fun newInstance(): Fragment4 {
            return Fragment4()
        }
    }
    
    
    inner class MoreLinksAdapter: BaseAdapter {
    
        var  listOfMoreMenus= ArrayList<MoreLinksObject>()
        var context: Context?=null
        constructor(context:Context, listOfMoreMenus: ArrayList<MoreLinksObject>):super(){
            this.listOfMoreMenus=listOfMoreMenus
            this.context=context
    
        }
    
        override fun getItem(position: Int): Any {
            return listOfMoreMenus[position]
        }
    
        override fun getItemId(position: Int): Long {
            return position.toLong()
        }
    
        override fun getCount(): Int {
            return  listOfMoreMenus.size
        }
    
        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            val MoreLinkObj =  listOfMoreMenus[position]
            var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            var myView = inflator.inflate(R.layout.more_menu, null)
            val moreMenuName = myView.findViewById(R.id.moreMenuName) as TextView
            moreMenuName.text =  MoreLinkObj.name!!
            return myView
        }
    
    }
    }
    

1 个答案:

答案 0 :(得分:2)

嗯,每次选择一个选项时,您都会提供每个片段的新实例,每次调用newInstance()。你有两个选择:

1)片段数组

在创建片段并将其重新使用时,将片段保存在全局数组中

2)标记

在片段中添加一个标签,然后用它来查找相同的片段。

因为您已经设置了最适合您的标签:

val FRAGMENT_1_TAG =  "FRAGMENT_1_TAG"
val FRAGMENT_2_TAG =  "FRAGMENT_2_TAG"
val FRAGMENT_3_TAG =  "FRAGMENT_3_TAG"
val FRAGMENT_4_TAG =  "FRAGMENT_4_TAG"

when (item.itemId) {
                R.id.tab1 -> {
                title = "Fragment1"
                selectedFragment = supportFragmentManager.findFragmentByTag(FRAGMENT_1_TAG);
                if (selectedFragment == null) {
                    selectedFragment = Fragment1.newInstance()
                }
                menuTag = FRAGMENT_1_TAG
                }
                R.id.tab2 -> {
                title = "Fragment2"
                selectedFragment = supportFragmentManager.findFragmentByTag(FRAGMENT_2_TAG);
                if (selectedFragment == null) {
                    selectedFragment = Fragment2.newInstance()
                }
                    menuTag = FRAGMENT_2_TAG
                }
                R.id.tab3 -> {
                title = "Fragment3"
                selectedFragment = supportFragmentManager.findFragmentByTag(FRAGMENT_3_TAG);
                if (selectedFragment == null) {
                    selectedFragment = Fragment3.newInstance()
                }
                    menuTag = FRAGMENT_3_TAG
                }
                R.id.tab4 ->{
                title = "Fragment4"
                selectedFragment = supportFragmentManager.findFragmentByTag(FRAGMENT_4_TAG);
                if (selectedFragment == null) {
                    selectedFragment = Fragment4.newInstance()
                }
                    menuTag = FRAGMENT_4_TAG
                }
            }

修改

要清除列表视图,您可以在添加新内容之前清除它,因此每次调用onCreateView时,您的列表都将从头开始初始化:

listOfMoreMenus.clear()
listOfMoreMenus.add(MoreLinksObject("test1","link1")) 
listOfMoreMenus.add(MoreLinksObject("test2","link2")) 
listOfMoreMenus.add(MoreLinksObject("test3","link3")) 

至于更改标题,如果您的工具栏属于活动,您可以在每次更改片段时设置标题。(见上文)。如果它属于片段,则需要在onCreateView方法中更改它,以便每次更改片段时都会更改它。