Skip to content

Commit

Permalink
Development (#3)
Browse files Browse the repository at this point in the history
* Feature/item position fixes (#2)

* - Resolved item position issue
- Updated library version
- Changed version

* - Managed manual scroll scenario

* - Changed item scale type
  • Loading branch information
chiragmi authored Aug 24, 2021
1 parent cd76f31 commit b5cfced
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ AutoScrollCircularPagerView is a library to show slider with features of circula
```groovy
dependencies {
...
implementation 'com.github.Mindinventory:AutoScrollCircularPagerView:0.0.1'
implementation 'com.github.Mindinventory:AutoScrollCircularPagerView:0.0.2'
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mindinventory.autoscrollviewpager

import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

Expand All @@ -14,6 +16,7 @@ class MainActivity : AppCompatActivity() {
}
private val myAdapter by lazy { MyAdapter() }

@RequiresApi(Build.VERSION_CODES.Q)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:layout_width="0dp"
android:layout_height="250dp"
android:orientation="horizontal"
app:auto_scroll_delay="5000"
app:auto_scroll_delay="2000"
app:dot_margin="20dp"
app:dot_gravity="bottom"
app:layout_constraintLeft_toLeftOf="parent"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_viewpager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/ivThumb"
android:scaleType="centerCrop"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down
22 changes: 11 additions & 11 deletions autoscrollcircularpagerview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ group='com.github.Mindinventory'

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
buildToolsVersion "30.0.3"

defaultConfig {
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "0.0.1"
versionCode 2
versionName "0.0.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand All @@ -37,12 +37,12 @@ android {

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.21"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.21"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mindinventory

import android.content.Context
import android.graphics.PorterDuff
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
Expand All @@ -10,6 +11,7 @@ import android.view.Gravity
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.annotation.RequiresApi
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import androidx.core.view.setMargins
Expand All @@ -36,6 +38,7 @@ class AutoScrollCircularPagerView @JvmOverloads constructor(
private var marginDots = -1
private var dotGravity = DotGravity.BOTTOM.value

@RequiresApi(Build.VERSION_CODES.Q)
private val runnable = Runnable {
autoScrollViewpager()
}
Expand Down Expand Up @@ -93,6 +96,7 @@ class AutoScrollCircularPagerView @JvmOverloads constructor(
}
}

@RequiresApi(Build.VERSION_CODES.Q)
fun setAutoScrollDelay(autoScrollDelay: Long) {
if (autoScrollDelay < 500) {
this.autoScrollDelay = autoScrollDelay
Expand All @@ -105,13 +109,12 @@ class AutoScrollCircularPagerView @JvmOverloads constructor(
val snapHelper: SnapHelper = PagerSnapHelper()
snapHelper.attachToRecyclerView(rvAutoScroll)
rvAutoScroll.addOnScrollListener(
CenterItemFinder(
context,
OnScrollListener(
rvAutoScroll.layoutManager as LinearLayoutManager,
object : CenterItemCallback {
override fun onScrollFinished(middleElement: Int) {
centerItemPosition = middleElement
onPageSelected(middleElement)
override fun onScrollFinished(visibleItemPosition: Int) {
centerItemPosition = visibleItemPosition
onPageSelected(centerItemPosition)
}

override fun onScrolled(dx: Int) {
Expand All @@ -128,6 +131,7 @@ class AutoScrollCircularPagerView @JvmOverloads constructor(
rvAutoScroll.adapter = circularAdapter
}

@RequiresApi(Build.VERSION_CODES.Q)
@Suppress("UNCHECKED_CAST")
fun <E> setItems(items: ArrayList<E>, clearPreviousElements: Boolean = false) {
if (rvAutoScroll.adapter is CircularAdapter<*>) {
Expand All @@ -137,11 +141,8 @@ class AutoScrollCircularPagerView @JvmOverloads constructor(
//scroll and dots are not required for just 1 item in the list
if (items.size > 1) {
//To manage selection of first item when auto scroll start
centerItemPosition += if (items.size % 2 == 0) {
1
} else {
2
}
val number: Int = Int.MAX_VALUE / items.size / 2
centerItemPosition = number * items.size
rvAutoScroll.layoutManager?.scrollToPosition(centerItemPosition)
setDots(items)
}
Expand Down Expand Up @@ -195,11 +196,13 @@ class AutoScrollCircularPagerView @JvmOverloads constructor(
return circularAdapter?.getActualItemCount() ?: 0 > 1
}

@RequiresApi(Build.VERSION_CODES.Q)
private fun startAutoScrollIfRequired() {
if (requiredAutoScroll() && !scrollHandler.hasCallbacks(runnable) && isAutoScrollEnabled)
scrollHandler.postDelayed(runnable, autoScrollDelay)
}

@RequiresApi(Build.VERSION_CODES.Q)
private fun stopAutoScrollIfRequired() {
if (scrollHandler.hasCallbacks(runnable)) {
scrollHandler.removeCallbacks(runnable)
Expand All @@ -209,19 +212,23 @@ class AutoScrollCircularPagerView @JvmOverloads constructor(
/**
* smooth scroll item to next position
*/
@RequiresApi(Build.VERSION_CODES.Q)
private fun autoScrollViewpager() {
rvAutoScroll.adapter?.let {
rvAutoScroll.smoothScrollToPosition(centerItemPosition + 1)
centerItemPosition += 1
rvAutoScroll.smoothScrollToPosition(centerItemPosition)
}
stopAutoScrollIfRequired()
startAutoScrollIfRequired()
}

@RequiresApi(Build.VERSION_CODES.Q)
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
stopAutoScrollIfRequired()
}

@RequiresApi(Build.VERSION_CODES.Q)
override fun onVisibilityAggregated(isVisible: Boolean) {
super.onVisibilityAggregated(isVisible)
if (isVisible) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mindinventory

interface CenterItemCallback {
fun onScrollFinished(middleElement: Int)
fun onScrollFinished(visibleItemPosition: Int)
fun onScrolled(dx: Int)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mindinventory

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class OnScrollListener(
private val layoutManager: LinearLayoutManager,
private val callback: CenterItemCallback,
private val controlState: Int
) : RecyclerView.OnScrollListener() {

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
callback.onScrolled(dx)
}

override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
if (newState == controlState) {
val visibleItemPosition = layoutManager.findFirstVisibleItemPosition()
callback.onScrollFinished(visibleItemPosition)
}
}
}
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.21"
ext.kotlin_version = "1.5.21"
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath "com.android.tools.build:gradle:4.1.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

Expand All @@ -19,7 +19,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
Expand Down

0 comments on commit b5cfced

Please sign in to comment.