Skip to content

Commit

Permalink
auto rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoima525 committed Aug 26, 2017
1 parent 06db076 commit 04b9fb2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ dependencies {
implementation 'com.android.support:design:26.0.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.jakewharton:butterknife:8.8.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.1'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
kapt 'com.jakewharton:butterknife-compiler:8.8.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import android.view.View
import android.widget.RelativeLayout
import butterknife.BindView
import butterknife.ButterKnife
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit

/**
* Created by tomoaki on 2017/08/13.
Expand All @@ -23,6 +27,8 @@ class InfiniteRotationView(context: Context, attributeSet: AttributeSet)
private val layoutManager: LinearLayoutManager
private lateinit var onScrollListener: OnScrollListener

private var dispose: Disposable? = null

init {
View.inflate(context, R.layout.view_infinite_rotation, this)
ButterKnife.bind(this)
Expand All @@ -38,15 +44,41 @@ class InfiniteRotationView(context: Context, attributeSet: AttributeSet)
adapter.itemCount
.takeIf { it > 1 }
?.apply {
onScrollListener = OnScrollListener(adapter.itemCount, layoutManager)
onScrollListener = OnScrollListener(
adapter.itemCount,
layoutManager,
{
// When dragging, we assume user swiped. So we will stop auto rotation
if (it == RecyclerView.SCROLL_STATE_DRAGGING) {
dispose?.dispose()
}
}
)
recyclerView.addOnScrollListener(onScrollListener)
recyclerView.scrollToPosition(1)
}
}

fun autoScroll(listSize: Int, intervalInMillis: Long) {
dispose?.let {
if(!it.isDisposed) return
}
dispose = Flowable.interval(intervalInMillis, TimeUnit.MILLISECONDS)
.map { it % listSize + 1 }
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
recyclerView.smoothScrollToPosition(it.toInt() + 1)
}
}

fun stopAutoScroll() {
dispose?.let(Disposable::dispose)
}

class OnScrollListener(
val itemCount: Int,
val layoutManager: LinearLayoutManager) : RecyclerView.OnScrollListener() {
val layoutManager: LinearLayoutManager,
val stateChanged: (Int) -> Unit) : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val firstItemVisible = layoutManager.findFirstVisibleItemPosition()
Expand All @@ -59,5 +91,10 @@ class InfiniteRotationView(context: Context, attributeSet: AttributeSet)
recyclerView?.scrollToPosition(itemCount - 1)
}
}

override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
stateChanged(newState)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ButterKnife.bind(this)
rotationView.setAdapter(InfiniteRotationAdapter(createItems()))
val items = createItems()
rotationView.setAdapter(InfiniteRotationAdapter(items))
rotationView.autoScroll(items.size, 2000)
}

override fun onDestroy() {
super.onDestroy()
rotationView.stopAutoScroll()
}

private fun createItems() = Array(4, { i ->
Expand Down

0 comments on commit 04b9fb2

Please sign in to comment.