Reputation: 145
I am trying to use onScrollListener to hide BottomNavigationView and other UI elements of mainActivity. while my recyclerview is located in the fragment inside the mainActivity and I am using custom binding adapter to inflate the recyclerview. I am currently using databinding.
My code
class HomeFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val binding= FragmentHomeBinding.inflate(inflater,container,false)
hideFloatingActionButton()
binding.dataList = DataProvider.productList
return binding.root
}
private fun hideFloatingActionButton(){
rv_products?.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy > 0) {
// Scrolling up
Toast.makeText(activity,"Going upp", Toast.LENGTH_SHORT).show()
// if (fab_button.visibility==View.INVISIBLE){
// fab_button.visibility=View.VISIBLE
// }
if (bottomNav.visibility==View.INVISIBLE){
bottomNav.visibility=View.VISIBLE
}
} else {
// Scrolling down
// if (fab_button.visibility==View.VISIBLE){
// fab_button.visibility=View.INVISIBLE
// }
}
if (bottomNav.visibility==View.VISIBLE){
bottomNav.visibility=View.INVISIBLE
}
}
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
// Do something
Toast.makeText(activity,"Going upp", Toast.LENGTH_SHORT).show()
} else if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
// Do something
} else {
// Do something
}
}
})
}
}
recyclerview
Upvotes: 0
Views: 362
Reputation: 1559
First get the recyclerView
from data binding
object, then use addOnScrollListener
. Declare an instance variable private lateinit var binding: FragmentHomeBinding
and initialize it inside onCreateView()
binding= FragmentHomeBinding.inflate(inflater,container,false)
.
Now get recyclerView
inside hideFloatingActionButton()
method like binding.rvProducts.addOnScrollListener(your callback)
Upvotes: 1