Skip to content

Instantly share code, notes, and snippets.

@d4vidi
d4vidi / RVPagerSnapFancyDecorator.kt
Last active July 27, 2022 11:17
A RecyclerView decorator that lets your easily turn a PagerSnapHelper from a provider of a simple full-screen "ViewPager" onto the common fancy cards carousel (such as duo lingo's: http://i.imgur.com/UXpVigQ.gif).
package com.d4vidi
import android.content.Context
import android.graphics.Rect
import android.support.annotation.Px
import android.support.v7.widget.RecyclerView
import android.view.View
/**
* A [RecyclerView decorator][RecyclerView.ItemDecoration] which, if applied over a recycler view
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
// Usual setups
recyclerView.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
recyclerView.adapter = ...
// Decorator set-up
package com.d4vidi
import android.support.annotation.Px
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.PagerSnapHelper
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewTreeObserver
data class VisiblePageState(
package com.d4vidi.vp2rv.core
import android.support.annotation.Px
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
data class VisiblePageState(
var index: Int,
var view: View,
RVPagerSnapHelperListenable().attachToRecyclerView(
recyclerView,
object : RVPagerStateListener {
override fun onPageScroll(pagesState: List<VisiblePageState>) {/*...*/}
override fun onPageSelected(index: Int) {/*...*/}
override fun onScrollStateChanged(state: RVPageScrollState) {/*...*/}
})
@d4vidi
d4vidi / PerfLogger2.java
Last active December 26, 2018 15:34
This is an improvement of Parashuram's `PerfLogger` described in http://blog.nparashuram.com/2018/11/react-native-performance-playbook-part-i.html, which allows for custom sections / events reporting alongside RN core's markers, and also enables the alignment of the report's 0-time with `Appication.onCreate()`
package com.parashuram;
import android.os.Process;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.bridge.ReactContext;
@d4vidi
d4vidi / VolatileLiveData.kt
Last active June 14, 2020 23:26
A MutableLiveData variant that introduces a volatile behavior
package com.und0.vocabularymate.x.utils
import androidx.annotation.MainThread
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import java.util.concurrent.atomic.AtomicInteger
/**
* A [MutableLiveData] variant that introduces a _volatile_ behavior. Namely, it has no "memory" of values set
android {
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// ...
}
### proguard-rules.pro ###
# Standard rules for my App:
-keep class this.and.That { *; }
-dontwarn com.on.**
android {
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
// Also consider the library's custom ProGuard rules
proguardFiles "${project(':thelibrary').projectDir}/proguard-rules-app.pro"
}
}