skip to Main Content

Good afternoon.

I have a RecyclerView inside a ConstraintLayout. The RecyclerView receives a number of items that can vary.

How can I find out, for comparison, when the RecycerView exceeds the screen width to put an expand button or not?

With a GridLayout I have figured it out using .spanCount. Another option that I have used is val column: Int = (view.layoutParams as GridLayoutManager.LayoutParams).spanIndex, but this does not work for me in the case of the RecyclerView within the Constraint.

Can someone help me please? Thanks in advance

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for the reply. I tried that but it didn't work for me. At the moment, what is working for me is:

      private fun showOrHideExpandIcon() {
        recyclerView.layoutManager = FlexboxLayoutManager(context)
        recyclerView.consumeViewTreeObserver {
          val rows = (recyclerView.layoutManager as? FlexboxLayoutManager)?.flexLinesInternal?.size
          if (rows != null) {
            if (rows == 1) {
              iconImageView.visibility = View.GONE
            }
          }
        }
      }
    

    The consumeViewTreeObserver function is a Global Observer created elsewhere so that resources are not used until they are loaded.

    Still, I would be interested to know if there is a way to count for the Linear Layout if you use more than one line


  2. Please try this:

    if (recyclerView.canScrollHorizontally(-1)) {
        // Show expand button
    } else {
        // Hide expand button
    }
    

    Here, if you give negative parameter to canScrollHorizontally() it means check if it scrolls left side, positive parameter means right side

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search