Skip to content

Instantly share code, notes, and snippets.

@Lalmi-Issam
Last active February 11, 2025 14:26
Show Gist options
  • Save Lalmi-Issam/23c18f9e10bcbd7566c3fc5c9e179eb5 to your computer and use it in GitHub Desktop.
Save Lalmi-Issam/23c18f9e10bcbd7566c3fc5c9e179eb5 to your computer and use it in GitHub Desktop.
ViewPager2 RTL Horizontal RecyclerView Android Kotlin
package /* TESTED ON ANDROID 12 */
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.View.LAYOUT_DIRECTION_RTL
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.anymax_v1.MainActivity
import com.example.anymax_v1.R
import com.example.anymax_v1.adapters.DayOfWeekAdapter
import com.example.anymax_v1.databinding.FragmentHomeBinding
class Homeschedule : Fragment() {
lateinit var binding: FragmentHomeBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home_schedule, container, false)
val daysOfWeek = listOf(
getString(R.string.sunday), getString(R.string.monday), getString(R.string.tuesday),
getString(R.string.wednesday), getString(R.string.thursday), getString(R.string.friday),
getString(R.string.saturday)
)
val adapter = DayOfWeekAdapter(daysOfWeek) { selectedDay ->
// Handle day selection here
Log.d("ScheduleFragment", "Selected day: $selectedDay")
}
binding.recyclerViewDays.apply {
// Set the layout manager for horizontal scrolling in RTL
layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
addOnItemTouchListener(object : RecyclerView.OnItemTouchListener {
var lastX = 0
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
when (e.action) {
MotionEvent.ACTION_DOWN -> {
lastX = e.x.toInt()
}
MotionEvent.ACTION_MOVE -> {
// Determine scrolling direction based on layout direction
val isScrollingRight = if (layoutDirection == LAYOUT_DIRECTION_RTL) {
e.x > lastX // Reverse scrolling logic for RTL
} else {
e.x < lastX
}
val layoutManager = rv.layoutManager as LinearLayoutManager
if ((isScrollingRight && layoutManager.findLastCompletelyVisibleItemPosition() == rv.adapter!!.itemCount - 1) ||
(!isScrollingRight && layoutManager.findFirstCompletelyVisibleItemPosition() == 0)) {
// Enable ViewPager swipe when at the start or end of the RecyclerView
(requireActivity() as MainActivity).binding.viewPager.isUserInputEnabled = true
} else {
// Disable ViewPager swipe while scrolling in the RecyclerView
(requireActivity() as MainActivity).binding.viewPager.isUserInputEnabled = false
}
}
MotionEvent.ACTION_UP -> {
lastX = 0
// Reset to allow swipe after touch ends
(requireActivity() as MainActivity).binding.viewPager.isUserInputEnabled = true
}
}
return false
}
override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {
// No-op
}
override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
// No-op
}
})
this.adapter = adapter
}
return binding.root
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment