Skip to content

Instantly share code, notes, and snippets.

View sevar83's full-sized avatar

Svetlozar Kostadinov sevar83

View GitHub Profile
@sevar83
sevar83 / ListLayout.java
Created January 29, 2016 14:15
ListLayout - a LinearLayout getting its items from Adapter. Very simple alternative of RecyclerView when a nested list is needed. It wraps its content with just setting wrap_content as height easily in constrast of RecyclerView.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Adapter;
import android.widget.LinearLayout;
@sevar83
sevar83 / VarColumnGridLayoutManager
Last active July 7, 2022 10:37
GridLayoutManager with variable number of columns calculated based on the measured view width
final VarColumnGridLayoutManager layoutManager
= new VarColumnGridLayoutManager(getContext(), OrientationHelper.VERTICAL, false);
VarColumnGridLayoutManager.ColumnCountProvider columnProvider
= new VarColumnGridLayoutManager.DefaultColumnCountProvider(getContext());
layoutManager.setColumnCountProvider(columnProvider);
mRecyclerView.setLayoutManager(layoutManager);
@sevar83
sevar83 / LMBindingAdapter.java
Created September 3, 2016 15:00
Binding adapter for https://github.com/evant/binding-collection-adapter which assigns common parameters to a List/Grid RecyclerView. Automatically restores the state of the old LayoutManager into the new one.
public class LMBindingAdapter {
// TODO the creation and modification of a layout manager must be done in one class. The creation is in a factory, but the modification is here.
@BindingAdapter(value = {"viewMode", "orientation", "reverseLayout", "spanCount", "spanSizeLookup"}, requireAll = false)
public static void setLayoutManager(RecyclerView recyclerView,
RecyclerViewMode viewMode,
@LayoutManagers.Orientation int orientation,
boolean reverseLayout,
int spanCount,
final SpanSizeLookup spanSizeLookup) {
if (viewMode == null) {
@sevar83
sevar83 / HorizontalPaddingItemDecoration.kt
Created February 20, 2017 08:02
Adds start/end padding to first/last item of a horizontal RecyclerView (like Play Store app)
class HorizontalPaddingItemDecoration(
private val paddingStart: Int = 0,
private val paddingEnd: Int = 0
) : RecyclerView.ItemDecoration() {
private var isRtl: Boolean? = null
fun isRTL(ctx: Context): Boolean {
val config = ctx.resources.configuration
return config.layoutDirection == View.LAYOUT_DIRECTION_RTL
@sevar83
sevar83 / ParcelSortedMapAdapter.kt
Created February 21, 2017 16:33
PaperParcel SortedSet and SortedMap adapters
class ParcelSortedMapAdapter<K, V>(
private val keyAdapter: TypeAdapter<K>,
private val valueAdapter: TypeAdapter<V>
) : TypeAdapter<@JvmSuppressWildcards SortedMap<K, V>> {
override fun readFromParcel(source: Parcel): SortedMap<K, V> {
val size = source.readInt()
val map = TreeMap<K, V>()
for (ignored in 1..size) {
val key = keyAdapter.readFromParcel(source)
@sevar83
sevar83 / ControllerLifecycle.kt
Last active August 30, 2019 13:56
Conductor / Glide lifecycle integration
import com.bumptech.glide.manager.Lifecycle
import com.bumptech.glide.manager.LifecycleListener
import com.bumptech.glide.util.Util
import java.util.*
/**
* A [com.bumptech.glide.manager.Lifecycle] implementation for tracking and notifying
* listeners of [com.bluelinelabs.conductor.Controller] lifecycle events.
*/
class ControllerLifecycle : Lifecycle {
@sevar83
sevar83 / PaddingTransition.kt
Created November 24, 2017 13:54
Non-stretching Glide placeholder
import android.graphics.drawable.Drawable
import com.bumptech.glide.request.transition.Transition
class PaddingTransition<T : Drawable>(private val realTransition: Transition<in T>) : Transition<T> {
override fun transition(current: T, adapter: Transition.ViewAdapter): Boolean {
val width = current.intrinsicWidth
val height = current.intrinsicHeight
return realTransition.transition(current, PaddingViewAdapter(adapter, width, height))
}