Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 通过数据绑定使用OnScrollListener时不工作_Android_Kotlin_Android Recyclerview_Android Databinding_Kotlin Android Extensions - Fatal编程技术网

Android 通过数据绑定使用OnScrollListener时不工作

Android 通过数据绑定使用OnScrollListener时不工作,android,kotlin,android-recyclerview,android-databinding,kotlin-android-extensions,Android,Kotlin,Android Recyclerview,Android Databinding,Kotlin Android Extensions,我试图使用onScrollListener隐藏BottomNavigationView和mainActivity的其他UI元素。而我的recyclerview位于mainActivity内部的片段中,我正在使用自定义绑定适配器为recyclerview充气。我目前正在使用数据绑定 我的代码 class HomeFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: Vi

我试图使用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
                }
            }
        })


    }



}

recyclerview

首先从
数据绑定
对象获取
recyclerview
,然后使用
addOnScrollListener
。声明一个实例变量
private lateinit var binding:FragmentHomeBinding
并在
onCreateView()中初始化它。
binding=FragmentHomeBinding.充气(充气机、容器、假)

现在在
hideFloatingActionButton()中获取
recyclerView
方法,如
binding.rvProducts.addOnScrollListener(您的回调)