Skip to content

Commit

Permalink
Merge pull request #1 from tomoima525/less_memory
Browse files Browse the repository at this point in the history
less memory usage
  • Loading branch information
tomoima525 authored Aug 26, 2017
2 parents 85e05c3 + 72e8aba commit 06db076
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import butterknife.ButterKnife
/**
* Created by tomoaki on 2017/08/13.
*/
class InfiniteRotationAdapter(val list: List<ItemInfo>)
: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
class InfiniteRotationAdapter(itemList: List<ItemInfo>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private val list: List<ItemInfo> = listOf(itemList.last()) + itemList + listOf(itemList.first())

override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
(holder as? ItemViewHolder)?.let {
it.pageName.text = list[position % list.size].page
Expand All @@ -29,9 +31,7 @@ class InfiniteRotationAdapter(val list: List<ItemInfo>)
return ItemViewHolder(view)
}

override fun getItemCount() = list.size * 3

fun getListSize() = list.size
override fun getItemCount() = list.size

internal class ItemViewHolder(view: View): RecyclerView.ViewHolder(view) {
@BindView(R.id.page_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class InfiniteRotationView(context: Context, attributeSet: AttributeSet)
val snapHelper = PagerSnapHelper()
snapHelper.attachToRecyclerView(recyclerView)

adapter.getListSize()
adapter.itemCount
.takeIf { it > 1 }
?.apply {
onScrollListener = OnScrollListener(adapter.getListSize(), layoutManager)
onScrollListener = OnScrollListener(adapter.itemCount, layoutManager)
recyclerView.addOnScrollListener(onScrollListener)
recyclerView.scrollToPosition(adapter.getListSize())
recyclerView.scrollToPosition(1)
}
}

Expand All @@ -51,13 +51,12 @@ class InfiniteRotationView(context: Context, attributeSet: AttributeSet)
super.onScrolled(recyclerView, dx, dy)
val firstItemVisible = layoutManager.findFirstVisibleItemPosition()


if (firstItemVisible > itemCount && firstItemVisible % itemCount == 0) {
if (firstItemVisible > 0 && firstItemVisible % (itemCount - 1) == 0) {
// When position reaches end of the list, it should go back to the beginning
recyclerView?.scrollToPosition(itemCount)
} else if (firstItemVisible == itemCount - 1) {
recyclerView?.scrollToPosition(1)
} else if (firstItemVisible == 0) {
// When position reaches beginning of the list, it should go back to the end
recyclerView?.scrollToPosition(itemCount * 2)
recyclerView?.scrollToPosition(itemCount - 1)
}
}
}
Expand Down

0 comments on commit 06db076

Please sign in to comment.