Skip to content

Instantly share code, notes, and snippets.

@abhishekBansal
abhishekBansal / InfiniteGradient.kt
Created March 25, 2022 09:48
Compose Infinite Moving gradient
val infiniteTransition = rememberInfiniteTransition()
val animationDuration = 5000
val color1 by infiniteTransition.animateColor(
initialValue = Color.Green,
targetValue = Color.Cyan,
animationSpec = infiniteRepeatable(
animation = tween(animationDuration, easing = FastOutSlowInEasing),
repeatMode = RepeatMode.Reverse
)
)
// apply touch listener on ViewPager RecyclerView
val recyclerView = binding.viewPager[0] as? RecyclerView
if (recyclerView != null) {
recyclerView.addOnItemTouchListener(swipeControlTouchListener)
} else {
Log.w(localClassName, "RecyclerView is null, API/Version changed ?!")
}
swipeControlTouchListener.setSwipeDirection(SwipeDirection.LEFT)
private fun isSwipeAllowed(event: MotionEvent): Boolean {
if (direction === SwipeDirection.ALL) return true
if (direction == SwipeDirection.NONE) //disable any swipe
return false
if (event.action == MotionEvent.ACTION_DOWN) {
initialXValue = event.x
return true
}
if (event.action == MotionEvent.ACTION_MOVE) {
try {
enum class SwipeDirection {
ALL, // swipe allowed in left and right both directions
LEFT, // swipe allowed in only Left direction
RIGHT, // only right
NONE // swipe is disabled completely
}
with(binding) {
progressBar.setVisible(false)
adapter.addItems(item)
recyclerView.setVisible(true)
successText.setVisible(true)
}
private fun showSuccessState(item: List<String>) {
binding.progressBar.setVisible(false)
binding.adapter.addItems(item)
binding.recyclerView.setVisible(true)
binding.successText.setVisible(true)
}
private fun showSuccessState(item: List<String>) {
progressBar.setVisible(false)
adapter.addItems(item)
recyclerView.setVisible(true)
successText.setVisible(true)
}
class DemoFragment : Fragment(R.layout.fragment_demo) {
private val binding by viewBinding(FragmentDemoBinding::bind)
}
class DemoFragment : Fragment() {
// declare binding
private var binding: FragmentDemoBinding? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// inflate and return view
binding = FragmentDemoBinding.inflate(inflater, container, false)
return binding?.root
}