通过数据绑定使用时 OnScrollListener 不工作
OnScrollListener not working when used via databinding
我正在尝试使用 onScrollListener 来隐藏 BottomNavigationView 和 mainActivity 的其他 UI 元素。而我的 recyclerview 位于 mainActivity 内的片段中,我正在使用自定义绑定适配器来膨胀 recyclerview。我目前正在使用数据绑定。
我的代码
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
}
}
})
}
}
回收站视图
首先从 data binding
对象中获取 recyclerView
,然后使用 addOnScrollListener
。声明一个实例变量private lateinit var binding: FragmentHomeBinding
并在onCreateView()
binding= FragmentHomeBinding.inflate(inflater,container,false)
.
里面初始化
现在在 hideFloatingActionButton()
方法中获取 recyclerView
,如 binding.rvProducts.addOnScrollListener(your callback)
我正在尝试使用 onScrollListener 来隐藏 BottomNavigationView 和 mainActivity 的其他 UI 元素。而我的 recyclerview 位于 mainActivity 内的片段中,我正在使用自定义绑定适配器来膨胀 recyclerview。我目前正在使用数据绑定。
我的代码
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
}
}
})
}
}
回收站视图
首先从 data binding
对象中获取 recyclerView
,然后使用 addOnScrollListener
。声明一个实例变量private lateinit var binding: FragmentHomeBinding
并在onCreateView()
binding= FragmentHomeBinding.inflate(inflater,container,false)
.
现在在 hideFloatingActionButton()
方法中获取 recyclerView
,如 binding.rvProducts.addOnScrollListener(your callback)