Skip to content

Instantly share code, notes, and snippets.

@huan-nguyen
huan-nguyen / NavControllerNavigate.kt
Created July 6, 2020 04:57
Pseudo code to illustrate the process of locating Navigator to handle the navigation to a particular destination
/**
This is only pseudo code to illustrate the process of locating Navigator
to handle the navigation to a particular destination.
It is not the real code in Navigation Component.
*/
fun NavController.navigate(@IdRes destinationResId: Int) {
val destination = navGraph.getDestination(destinationResId)
val navigator = navigatorProvider.getNavigator(destination.navigatorName)
navigator.navigate(destination)
@huan-nguyen
huan-nguyen / navgraph2.xml
Last active July 6, 2020 03:09
Examples of how destinations are defined in a NavGraph
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
app:startDestination="@id/greatFragment">
<!-- Create a fragment destination -->
<fragment
android:id="@+id/myGreatFragment"
android:name="com.example.MyGreatFragment"
@huan-nguyen
huan-nguyen / nav_graph.xml
Last active June 18, 2020 14:26
Example of a NavGraph
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
app:startDestination="@id/dest_fragment1">
<fragment
android:id="@+id/dest_fragment1"
android:name="com.example.Fragment1"
android:label="fragment1"
tools:layout="@layout/fragment1" >
@huan-nguyen
huan-nguyen / activityWithNavHost.xml
Created June 18, 2020 13:55
Add navhost to activity
<androidx.constraintlayout.widget.ConstraintLayout
...
tools:context=".MainActivity">
...
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
@huan-nguyen
huan-nguyen / InboxTransition1.kt
Last active September 28, 2018 11:46
First attempt - inbox transition
override fun onBindViewHolder(holder: EmailViewHolder, position: Int) {
fun onViewClick() {
val viewRect = Rect()
holder.itemView.getGlobalVisibleRect(viewRect)
exitTransition = Explode().apply {
duration = TRANSITION_DURATION
interpolator = transitionInterpolator
epicenterCallback = object : Transition.EpicenterCallback() {
override fun onGetEpicenter(transition: Transition) = viewRect
@huan-nguyen
huan-nguyen / TapPosition.kt
Last active September 28, 2018 12:10
Updated code with tap position
override fun onViewCreated(view: View, savedState: Bundle?) {
super.onViewCreated(view, savedState)
tapPosition = savedState?.getInt(TAP_POSITION, NO_POSITION) ?: NO_POSITION
postponeEnterTransition()
...
}
...
private fun render(state: State) {
when (state) {
...
@huan-nguyen
huan-nguyen / SlideExplode.kt
Created September 24, 2018 13:16
SlideExplode
import android.animation.Animator
import android.animation.ObjectAnimator
import android.graphics.Rect
import android.transition.TransitionValues
import android.transition.Visibility
import android.view.View
import android.view.ViewGroup
private const val KEY_SCREEN_BOUNDS = "screenBounds"
Unless specified otherwise, all of the below tinting applies to both Lollipop and pre-Lollipop using AppCompat v21.
To use the support version of these attributes, remove the android namespace.
For instance, "android:colorControlNormal" becomes "colorControlNormal".
These attributes will be propagated to their corresponding attributes within the android namespace
for devices running Lollipop. Any exceptions to this will be noted by including the "android:" prefix.
All Clickable Views:
-----------
firstXAnim.addUpdateListener { _, value, _ ->
secondXAnim.animateToFinalPosition(value)
}
firstYAnim.addUpdateListener { _, value, _ ->
secondYAnim.animateToFinalPosition(value + viewYDistance)
}
// The fixed difference between Y properties of two consecutive views (i.e., dragView and firstView, firstView and secondView) that they need to maintain.
val viewYDistance = ...
var dX = 0
var dY = 0
...
dragView.setOnTouchListener { view, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
dX = view.x - event.rawX
dY = view.y - event.rawY