Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes: #2 BREAKING CHANGE: SwapperView now uses a generic View identifier. This makes using enums way easier. * Use `SwapperViewContainer` in XML instead of `SwapperView` * Call `swapperViewContainer.init(...)` to setup strongly typed `SwapperView`
- Loading branch information
1 parent
1437fc4
commit 997f65d
Showing
4 changed files
with
112 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
swapper/src/main/java/com/levibostian/swapper/SwapperViewContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.levibostian.swapper | ||
|
||
import android.annotation.TargetApi | ||
import android.content.Context | ||
import android.os.Build | ||
import android.util.AttributeSet | ||
import android.view.View | ||
import android.widget.FrameLayout | ||
import java.lang.ref.WeakReference | ||
|
||
class SwapperViewContainer : FrameLayout { | ||
|
||
private var attrs: AttributeSet? = null | ||
private var defStyleAttr: Int = -1 | ||
|
||
internal val childrenExceptSwapperView: List<View> | ||
get() { | ||
val children: MutableList<View> = mutableListOf() | ||
val swapperViewIndex = this.swapperViewChildIndex | ||
|
||
for (index in 0 until childCount) { | ||
if (index != swapperViewIndex) { | ||
children.add(getChildAt(index)) | ||
} | ||
} | ||
|
||
return children | ||
} | ||
|
||
private val swapperViewChildIndex: Int? | ||
get() { | ||
for (index in 0 until childCount) { | ||
val view = getChildAt(index) | ||
|
||
if (view is SwapperView<*>) { | ||
return index | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
constructor(context: Context) : this(context, null) | ||
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) | ||
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( | ||
context, | ||
attrs, | ||
defStyleAttr | ||
) { | ||
initialize(context, attrs, defStyleAttr) | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | ||
constructor( | ||
context: Context, | ||
attrs: AttributeSet?, | ||
defStyleAttr: Int, | ||
defStyleRes: Int | ||
) : super(context, attrs, defStyleAttr, defStyleRes) { | ||
initialize(context, attrs, defStyleAttr) | ||
} | ||
|
||
private fun initialize(context: Context, attrs: AttributeSet?, defStyleAttr: Int) { | ||
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) | ||
|
||
this.attrs = attrs | ||
this.defStyleAttr = defStyleAttr | ||
} | ||
|
||
fun <SwapperViewId : Any> init(views: Map<SwapperViewId, View>, swapTo: SwapperViewId): SwapperView<SwapperViewId> { | ||
swapperViewChildIndex?.let { removeViewAt(it) } | ||
|
||
val swapperView = SwapperView<SwapperViewId>(context, attrs, defStyleAttr) | ||
addView(swapperView) | ||
swapperView.container = WeakReference(this) | ||
swapperView.init(views, swapTo) | ||
|
||
return swapperView | ||
} | ||
} |