Поскольку вы прокручиваете фрагмент, вам нужно передать значения прокрутки в вашу активность.
Я предлагаю вам использовать значение по умолчанию InteractionInterface
, которое Android Studio сгенерировало в шаблоне Fragment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_blank, container, false)
root.scrollView2.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
// the key is here.
var delta = scrollY - oldScrollY
listener?.onFragmentScrolled(delta)
}
return inflater.inflate(R.layout.fragment_blank, container, false)
}
interface OnFragmentInteractionListener {
// Name your function here
fun onFragmentScrolled(delta: Float)
}
// the lines below are generated,
// not the key point here but important to binding listener
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnFragmentInteractionListener) {
listener = context
} else {
throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
}
}
override fun onDetach() {
super.onDetach()
listener = null
}
А затем, в YourActivity
, реализовать YourFragment.OnFragmentInteractionListener
Переопределите функцию
override fun onFragmentScrolled(delta: Float) {
anotherView.translationY = anotherView.translationY + delta
if (anotherView.translationY > anotherView.height)
anotherView.translationY = anotherView.height.toFloat()
if (anotherView.translationY < 0)
anotherView.translationY = 0f
}
результат будет примерно таким: mp4 link
Суть в том, что: передайте действие прокрутки из фрагмента в действие, вы можете этого добитьсяво многих отношениях это только основной;