Skip to content

Instantly share code, notes, and snippets.

View yasukotelin's full-sized avatar

Yasuhiro Kikura yasukotelin

View GitHub Profile
@yasukotelin
yasukotelin / BottomWidgetBehavior.kt
Created August 19, 2020 16:37
CoordinatorLayoutでBottomの要素を消す挙動をするBehavior
class BottomWidgetBehavior<V : View>(context: Context, attrs: AttributeSet) :
CoordinatorLayout.Behavior<V>(context, attrs) {
override fun onStartNestedScroll(
coordinatorLayout: CoordinatorLayout,
child: V,
directTargetChild: View,
target: View,
axes: Int,
type: Int
class NoSwipeViewPager(context: Context, attrs: AttributeSet) : ViewPager(context, attrs) {
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
// disable swipe
return false
}
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
// disable swipe
@yasukotelin
yasukotelin / Extentions.kt
Last active October 14, 2020 02:16
Epoxy Spacer item
fun Int.toDp(): Int = (this / Resources.getSystem().displayMetrics.density).toInt()
fun Int.toPx(): Int = (this * Resources.getSystem().displayMetrics.density).toInt()
fun Fragment.performHapticFeedback(effectId: Int) {
val vibrator = requireContext().getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
vibrator.vibrate(VibrationEffect.createPredefined(effectId))
}
}
import android.view.HapticFeedbackConstants
import android.view.MotionEvent
import android.view.View
/**
* Viewへのタップイベントに対してHaptic feedbackするヘルパー
*/
class HapticOnTapHelper : HapticOnTouchHelper {
override fun onTouch(v: View, e: MotionEvent): Boolean {
val isHandled = v.onTouchEvent(e)
@yasukotelin
yasukotelin / KotlinOriginalResult.kt
Last active November 25, 2020 08:55
kotlin-result紹介用のスニペットたち
fun main(args: Array<String>) {
when (val r1 = getUser()) {
is Result.Success -> {
when (val r2 = login(r1.data)) {
is Result.Success -> {
when (val r3 = setupSomething()) {
is Result.Success -> {
println("Success")
}
@yasukotelin
yasukotelin / MainBottomNavigationFragment.kt
Created November 26, 2020 16:26
android / architecture-components-samplesのNavigationExtension.ktをFragment呼び出しに対応したバージョン
class MainBottomNavigationFragment : DaggerFragment() {
private var _binding: FragmentMainBottomNavigationBinding? = null
private val binding get() = _binding!!
// activityViewModels()しないように注意
// activityスコープなどでViewModelを作りたい場合は別のViewModelを作ること(MainBottomNavigationViewModelなど
private val navigateExtensionsViewModel: NavigateExtensionsViewModel by viewModels()
private val navGraphIds = listOf(
@yasukotelin
yasukotelin / Center.kt
Created June 10, 2021 03:44
Jetpack Compose Center composable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@Composable
fun Center(
modifier: Modifier = Modifier,
@yasukotelin
yasukotelin / BottomSheetDialogFragmentExtensions.kt
Created July 29, 2021 02:11
BottomSheetDialogFragmentを全画面表示するExtension。onViewCreatedでcallする。
/**
* BottomSheetDialogを全画面表示する
*/
fun BottomSheetDialogFragment.fullScreen() {
dialog?.setOnShowListener { dialog ->
val d = dialog as BottomSheetDialog
val bottomSheet =
d.findViewById<FrameLayout>(R.id.design_bottom_sheet) ?: return@setOnShowListener
bottomSheet.layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT
val screenHeight = Resources.getSystem().displayMetrics.heightPixels
@yasukotelin
yasukotelin / TabLayout.kt
Last active February 14, 2022 09:12
Tabの要素数によってTab Modeを切り替えるExtension
import android.view.ViewGroup
import com.google.android.material.tabs.TabLayout
/**
* Tab表示が画面に収まる場合と収まらない場合とでTab mode指定を切り替える
*
* Tabサイズが画面に収まる場合は[TabLayout.MODE_FIXED]
* Tabサイズが画面に収まらない場合は[TabLayout.MODE_SCROLLABLE]
*/
fun TabLayout.adjustTabMode() {