Skip to content

Instantly share code, notes, and snippets.

View samuel22gj's full-sized avatar
👨‍💻
Coding

Samuel Huang samuel22gj

👨‍💻
Coding
View GitHub Profile
@samuel22gj
samuel22gj / ParallaxRecyclerView.java
Last active January 30, 2018 08:23
A RecyclerView that make the first ViewHolder has parallax effect.
/**
* Make the first {@link RecyclerView.ViewHolder} has parallax effect.
*/
public class ParallaxRecyclerView extends ECRecyclerView {
private static final String TAG = ParallaxRecyclerView.class.getSimpleName();
private static final float DEFAULT_PARALLAX_MULTIPLIER = 0.5f;
private float mParallaxMultiplier = DEFAULT_PARALLAX_MULTIPLIER;
private OnParallaxScrollListener mParallaxScrollListener;
@samuel22gj
samuel22gj / SpacingDividerItemDecoration.java
Last active April 26, 2018 07:06
A RecyclerView.ItemDecoration that can add spacing divider between items.
/**
* SpacingDividerItemDecoration is a {@link RecyclerView.ItemDecoration} that can add space divider
* between items of a {@link android.support.v7.widget.LinearLayoutManager}. It supports both {@link #HORIZONTAL}
* and {@link #VERTICAL} orientations.
*/
public class SpacingDividerItemDecoration extends RecyclerView.ItemDecoration {
@IntDef({HORIZONTAL, VERTICAL})
@Retention(RetentionPolicy.SOURCE)
public @interface OrientationMode {}
@samuel22gj
samuel22gj / ClickAreaHelper.java
Last active September 28, 2018 06:55
Class intended to support multiple click areas for a View.
/**
* Class intended to support multiple click areas for a {@link View}.
*/
public abstract class ClickAreaHelper implements View.OnTouchListener, View.OnClickListener, View.OnLongClickListener {
private OnClickAreaListener mOnClickAreaListener;
private OnLongClickAreaListener mOnLongClickAreaListener;
private View mView;
public class ToolbarOffsetChangedListener implements AppBarLayout.OnOffsetChangedListener {
private static final String TAG = "ToolbarOffsetChangedListener";
private Toolbar toolbar;
private Drawable[] icons;
/**
* 觸發開始改變顏色的 offset [0, - AppBar's height]
*/
private int verticalOffsetTrigger = -1;
/**
* @see <a href="https://medium.com/androiddevelopers/windows-insets-fragment-transitions-9024b239a436">Windows Insets + Fragment Transitions</a>
*/
public class DispatchWindowInsetsToAllChildrenListener implements OnApplyWindowInsetsListener {
@Override
public WindowInsetsCompat onApplyWindowInsets(View view, WindowInsetsCompat windowInsetsCompat) {
boolean consumed = false;
if (view instanceof ViewGroup) {
sealed class SharedPreferenceDelegates<in R, T> : ReadWriteProperty<R, T> {
class BooleanType(
private val sharedPreferences: SharedPreferences,
private val key: String,
private val defaultValue: Boolean = false
) : SharedPreferenceDelegates<Any?, Boolean>() {
override fun getValue(thisRef: Any?, property: KProperty<*>): Boolean {
return sharedPreferences.getBoolean(key, defaultValue)
@samuel22gj
samuel22gj / TestNameToastRule.kt
Last active February 22, 2020 13:12
Rule to be used in tests that display a Toast with class name and method name when each test starting.
/**
* Rule to be used in tests that display a Toast with class name and method name when each test starting.
*
* ```
* @RunWith(AndroidJUnit4::class)
* class FooTest {
*
* @get:Rule
* val testNameToastRule = TestNameToastRule()
*
object RecyclerViewUtils {
fun <VH : RecyclerView.ViewHolder> scrollToHolderWithFallback(
recyclerViewMatcher: Matcher<View>,
viewHolderMatcher: Matcher<VH>,
failureViewAction: ViewAction,
maxRetryCount: Int
) {
var count = maxRetryCount
/**
* A customized [BottomNavigationView] with some convenience functions.
*/
class BottomNavigationViewPlus @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
@AttrRes defStyleAttr: Int = com.google.android.material.R.attr.bottomNavigationStyle
) : BottomNavigationView(
context, attrs, defStyleAttr
) {
/**
* A mediator to link a fragment container with a BottomNavigationView.
*
* The mediator will synchronize the fragment container's content
* with the selected item when a item of BottomNavigationView is selected.
*/
class FragmentBottomNavigationMediator(
private val fragmentManager: FragmentManager,
@IdRes private val containerViewId: Int,
private val bottomNavigationView: BottomNavigationView,