Don't collapse Toolbar when RecyclerView fits the screen

前端 未结 10 1506
陌清茗
陌清茗 2020-11-30 21:08


I\'ve made an app using Android Design Library, with a Toolbar and TabLayout.
Actually 2 tabs are present, both with 2 RecyclerView, that automati

相关标签:
10条回答
  • 2020-11-30 21:27

    RecyclerView now (since version 23.2) supports wrap_content. Just use wrap_content as the height.

    0 讨论(0)
  • 2020-11-30 21:27

    You can add within your XML, the property layout_behaviour with value @string/appbar_scrolling_view_behavior this way:

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    
    0 讨论(0)
  • 2020-11-30 21:31

    If helps anyome in Kotlin based on correct answer, I did this for Kotlin:

    fun changeToolbarScroll(isToScrolling: Boolean){
    
        val params = toolbar.layoutParams as AppBarLayout.LayoutParams
        val appBarLayoutParams = appBarLayout.layoutParams as 
                                 CoordinatorLayout.LayoutParams
        params.scrollFlags = 0
        toolbar.layoutParams = params
        appBarLayoutParams.behavior = null
        appBarLayout.layoutParams = appBarLayoutParams
    
        if(isToScrolling){
            params.scrollFlags =
                AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL or 
                AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS
            toolbar.layoutParams = params
    
            appBarLayoutParams.behavior = AppBarLayout.Behavior()
            appBarLayout.layoutParams = appBarLayoutParams
        }
    }
    

    In my case, I have a problem with a MainActivity that's manage navigation, toolbar and other shared things by 2 Fragments, the first Framgent use a RecyclerView and second is for show the deatil. The problems is when I set a Menu and change MenuItem from MainAcitivity

    It can sound silly and totally logical but remember Always make chages to the Menu or MenuItems before call supportFragmentManager.beginTransaction() when change fragments otherwise don't work, or dont update properly, no matter changes in .add, .replace(), show()...

    fun showDetailImageFragment(searchImage: SearchImage) {
        val searchFragment = 
        supportFragmentManager.findFragmentByTag(SEARCH_IMAGES)
    
        changeToolbarScroll(false)
    
        if (supportActionBar != null) {
            supportActionBar!!.collapseActionView()
            supportActionBar!!.setDisplayHomeAsUpEnabled(true)
            supportActionBar!!.title = getString(R.string.detail_image_title)
        }
    
        actionSearch.isVisible = false
        actionNighMOde.isVisible = false
        actionAppSetings.isVisible = false
        actionAbout.isVisible = false
    
        supportFragmentManager.beginTransaction()
            .setCustomAnimations(
                R.animator.fade_in,
                R.animator.fade_out,
                R.animator.fade_in,
                R.animator.fade_out
            )
            .hide(searchFragment!!)
            .add(
                R.id.frameLayout,
                DetailImageFragment().newInstance(searchImage)
            ).addToBackStack(null)
            .commit()
    }
    
    0 讨论(0)
  • 2020-11-30 21:38

    Add this code after you change data in your adapter:

    recyclerView.afterMeasured {
        val isTurnedOff = recyclerView.turnOffNestedScrollingIfEnoughItems()
        if (isTurnedOff) appBarLayout.setExpanded(true)
    }
    

    And this is the functions:

    inline fun <T: View> T.afterMeasured(crossinline action: T.() -> Unit) {
        viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {             
                viewTreeObserver.removeOnGlobalLayoutListener(this)
                action()
            }
        })
    }
    
    
    fun RecyclerView.turnOffNestedScrollingIfEnoughItems(): Boolean {
        val lm = (layoutManager as LinearLayoutManager)
        val count = if (lm.itemCount <= 0) 0 else lm.itemCount - 1
        val isFirstVisible = lm.findFirstCompletelyVisibleItemPosition() == 0
        val isLastItemVisible = lm.findLastCompletelyVisibleItemPosition() == count
    
        isNestedScrollingEnabled = !(isLastItemVisible && isFirstVisible)
        return isNestedScrollingEnabled.not()
    }
    
    0 讨论(0)
提交回复
热议问题