From 7cb7cf62b7a8a0ee54aad32a9a684fd7f61e89f9 Mon Sep 17 00:00:00 2001 From: Ivan Abakumov Date: Tue, 11 Apr 2023 23:42:24 +0300 Subject: [PATCH 1/5] Updated gradle version --- alphabetsindexfastscrollrecycler/build.gradle | 17 ++++++++++------- app/build.gradle | 16 +++++++++++++--- app/src/main/AndroidManifest.xml | 3 ++- build.gradle | 10 ++++++---- gradle/wrapper/gradle-wrapper.properties | 2 +- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/alphabetsindexfastscrollrecycler/build.gradle b/alphabetsindexfastscrollrecycler/build.gradle index 1138506..5976d0e 100644 --- a/alphabetsindexfastscrollrecycler/build.gradle +++ b/alphabetsindexfastscrollrecycler/build.gradle @@ -1,15 +1,16 @@ apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' final ANDROIDX = '1.0.0' -apply plugin: 'com.github.dcendents.android-maven' // ADD THIS +//apply plugin: 'com.github.dcendents.android-maven' // ADD THIS group='com.github.jitpack' android { - compileSdkVersion 28 + compileSdkVersion 33 defaultConfig { minSdkVersion 15 - targetSdkVersion 28 + targetSdkVersion 33 versionCode 1 versionName "1.0" } @@ -23,7 +24,7 @@ android { allprojects { repositories { - jcenter() + mavenCentral() maven { url "https://maven.google.com" } @@ -31,8 +32,10 @@ allprojects { } dependencies { - implementation "androidx.appcompat:appcompat:${ANDROIDX}" - implementation "com.google.android.material:material:${ANDROIDX}" + implementation 'androidx.appcompat:appcompat:1.6.1' + implementation "com.google.android.material:material:1.8.0" + implementation "org.jetbrains.kotlin:kotlin-stdlib:1.6.21" + implementation 'androidx.core:core-ktx:1.8.0' } // build a jar with source files @@ -45,7 +48,7 @@ task javadoc(type: Javadoc) { failOnError false source = android.sourceSets.main.java.sourceFiles classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) - classpath += configurations.compile + classpath += configurations.implementation } // build a jar with javadoc diff --git a/app/build.gradle b/app/build.gradle index 0c7e323..f568b3b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,12 +8,12 @@ final ANDROIDX = '1.0.0' final BUTTERKNIFE = '10.1.0' android { - compileSdkVersion 28 + compileSdkVersion 33 defaultConfig { applicationId "in.myinnos.indexfastscrollrecycler" minSdkVersion 15 - targetSdkVersion 28 + targetSdkVersion 33 versionCode 1 versionName "1.0" } @@ -28,6 +28,10 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } + + buildFeatures { + dataBinding true + } } dependencies { @@ -36,7 +40,13 @@ dependencies { implementation "com.jakewharton:butterknife:${BUTTERKNIFE}" annotationProcessor "com.jakewharton:butterknife-compiler:${BUTTERKNIFE}" implementation project(':alphabetsindexfastscrollrecyclerLibrary') - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation "org.jetbrains.kotlin:kotlin-stdlib:1.6.21" + implementation 'androidx.core:core-ktx:1.8.0' + + // reflection-based flavor + implementation 'com.github.kirich1409:viewbindingpropertydelegate-full:1.5.8' + // reflection-free flavor + implementation 'com.github.kirich1409:viewbindingpropertydelegate-noreflection:1.5.8' } repositories { mavenCentral() diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1aeccd0..65f3557 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,7 +9,8 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> - + diff --git a/build.gradle b/build.gradle index d99ecba..475cb2d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,16 +1,17 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.3.21' + ext.kotlin_version = "1.6.10" repositories { - jcenter() + google() + mavenCentral() maven { url "https://maven.google.com" } google() } dependencies { - classpath 'com.android.tools.build:gradle:3.4.2' + classpath 'com.android.tools.build:gradle:7.2.2' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong @@ -20,7 +21,8 @@ buildscript { allprojects { repositories { - jcenter() + google() + mavenCentral() maven { url "https://maven.google.com" } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index bb749b9..215961a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip From 7d52949588dd26b86187901c4dae540c386d015c Mon Sep 17 00:00:00 2001 From: Ivan Abakumov Date: Tue, 11 Apr 2023 23:49:18 +0300 Subject: [PATCH 2/5] Updated Kotlin version to 1.8.10 --- alphabetsindexfastscrollrecycler/build.gradle | 4 ++-- app/build.gradle | 5 ++--- build.gradle | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/alphabetsindexfastscrollrecycler/build.gradle b/alphabetsindexfastscrollrecycler/build.gradle index 5976d0e..5b326e5 100644 --- a/alphabetsindexfastscrollrecycler/build.gradle +++ b/alphabetsindexfastscrollrecycler/build.gradle @@ -34,8 +34,8 @@ allprojects { dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation "com.google.android.material:material:1.8.0" - implementation "org.jetbrains.kotlin:kotlin-stdlib:1.6.21" - implementation 'androidx.core:core-ktx:1.8.0' + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + implementation 'androidx.core:core-ktx:1.10.0' } // build a jar with source files diff --git a/app/build.gradle b/app/build.gradle index f568b3b..6209548 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,6 +1,5 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' -apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' @@ -40,8 +39,8 @@ dependencies { implementation "com.jakewharton:butterknife:${BUTTERKNIFE}" annotationProcessor "com.jakewharton:butterknife-compiler:${BUTTERKNIFE}" implementation project(':alphabetsindexfastscrollrecyclerLibrary') - implementation "org.jetbrains.kotlin:kotlin-stdlib:1.6.21" - implementation 'androidx.core:core-ktx:1.8.0' + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + implementation 'androidx.core:core-ktx:1.10.0' // reflection-based flavor implementation 'com.github.kirich1409:viewbindingpropertydelegate-full:1.5.8' diff --git a/build.gradle b/build.gradle index 475cb2d..488d086 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = "1.6.10" + ext.kotlin_version = "1.8.10" repositories { google() mavenCentral() From c8ac037a41802b8417c9737895b88ff350798588 Mon Sep 17 00:00:00 2001 From: Ivan Abakumov Date: Wed, 12 Apr 2023 00:48:55 +0300 Subject: [PATCH 3/5] Rename .java to .kt --- ...rollRecyclerSection.java => IndexFastScrollRecyclerSection.kt} | 0 ...FastScrollRecyclerView.java => IndexFastScrollRecyclerView.kt} | 0 .../{MainActivity.java => MainActivity.kt} | 0 .../adapter/{RecyclerViewAdapter.java => RecyclerViewAdapter.kt} | 0 .../helper/{DataHelper.java => DataHelper.kt} | 0 .../utility/{AlphabetItem.java => AlphabetItem.kt} | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/{IndexFastScrollRecyclerSection.java => IndexFastScrollRecyclerSection.kt} (100%) rename alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/{IndexFastScrollRecyclerView.java => IndexFastScrollRecyclerView.kt} (100%) rename app/src/main/java/in/myinnos/indexfastscrollrecycler/{MainActivity.java => MainActivity.kt} (100%) rename app/src/main/java/in/myinnos/indexfastscrollrecycler/adapter/{RecyclerViewAdapter.java => RecyclerViewAdapter.kt} (100%) rename app/src/main/java/in/myinnos/indexfastscrollrecycler/helper/{DataHelper.java => DataHelper.kt} (100%) rename app/src/main/java/in/myinnos/indexfastscrollrecycler/utility/{AlphabetItem.java => AlphabetItem.kt} (100%) diff --git a/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerSection.java b/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerSection.kt similarity index 100% rename from alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerSection.java rename to alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerSection.kt diff --git a/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerView.java b/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerView.kt similarity index 100% rename from alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerView.java rename to alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerView.kt diff --git a/app/src/main/java/in/myinnos/indexfastscrollrecycler/MainActivity.java b/app/src/main/java/in/myinnos/indexfastscrollrecycler/MainActivity.kt similarity index 100% rename from app/src/main/java/in/myinnos/indexfastscrollrecycler/MainActivity.java rename to app/src/main/java/in/myinnos/indexfastscrollrecycler/MainActivity.kt diff --git a/app/src/main/java/in/myinnos/indexfastscrollrecycler/adapter/RecyclerViewAdapter.java b/app/src/main/java/in/myinnos/indexfastscrollrecycler/adapter/RecyclerViewAdapter.kt similarity index 100% rename from app/src/main/java/in/myinnos/indexfastscrollrecycler/adapter/RecyclerViewAdapter.java rename to app/src/main/java/in/myinnos/indexfastscrollrecycler/adapter/RecyclerViewAdapter.kt diff --git a/app/src/main/java/in/myinnos/indexfastscrollrecycler/helper/DataHelper.java b/app/src/main/java/in/myinnos/indexfastscrollrecycler/helper/DataHelper.kt similarity index 100% rename from app/src/main/java/in/myinnos/indexfastscrollrecycler/helper/DataHelper.java rename to app/src/main/java/in/myinnos/indexfastscrollrecycler/helper/DataHelper.kt diff --git a/app/src/main/java/in/myinnos/indexfastscrollrecycler/utility/AlphabetItem.java b/app/src/main/java/in/myinnos/indexfastscrollrecycler/utility/AlphabetItem.kt similarity index 100% rename from app/src/main/java/in/myinnos/indexfastscrollrecycler/utility/AlphabetItem.java rename to app/src/main/java/in/myinnos/indexfastscrollrecycler/utility/AlphabetItem.kt From 767a1bde012510fff9dd2d53741653077d08f083 Mon Sep 17 00:00:00 2001 From: Ivan Abakumov Date: Wed, 12 Apr 2023 00:48:57 +0300 Subject: [PATCH 4/5] Updated library and project to Kotlin --- .../IndexFastScrollRecyclerSection.kt | 604 +++++++++-------- .../IndexFastScrollRecyclerView.kt | 621 ++++++++++-------- .../indexfastscrollrecycler/MainActivity.kt | 136 ++-- .../adapter/RecyclerViewAdapter.kt | 147 ++--- .../helper/DataHelper.kt | 383 ++++++----- .../helper/NotFullDataHelper.java | 102 --- .../utility/AlphabetItem.kt | 16 +- 7 files changed, 963 insertions(+), 1046 deletions(-) delete mode 100644 app/src/main/java/in/myinnos/indexfastscrollrecycler/helper/NotFullDataHelper.java diff --git a/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerSection.kt b/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerSection.kt index 42c3c97..de338ca 100644 --- a/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerSection.kt +++ b/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerSection.kt @@ -1,482 +1,466 @@ -package in.myinnos.alphabetsindexfastscrollrecycler; +package `in`.myinnos.alphabetsindexfastscrollrecycler + +import android.content.Context +import android.graphics.* +import android.util.Log +import android.widget.SectionIndexer +import android.view.MotionEvent +import androidx.annotation.ColorInt +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import java.lang.Exception /* * Created by MyInnos on 31-01-2017. * Updated by AbandonedCart 07-2022. - */ - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.RectF; -import android.graphics.Typeface; -import android.util.Log; -import android.view.MotionEvent; -import android.widget.SectionIndexer; - -import androidx.annotation.ColorInt; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; - -import java.util.Objects; - -public class IndexFastScrollRecyclerSection extends RecyclerView.AdapterDataObserver { - - private float mIndexbarWidth; - private float mIndexbarMarginLeft; - private float mIndexbarMarginRight; - private float mIndexbarMarginTop; - private float mIndexbarMarginBottom; - private final float mPreviewPadding; - private final float mDensity; - private final float mScaledDensity; - private int mListViewWidth; - private int mListViewHeight; - private int mCurrentSection = -1; - private boolean mIsIndexing = false; - private final RecyclerView mRecyclerView; - private SectionIndexer mIndexer = null; - private String[] mSections = null; - private RectF mIndexbarRect; - - private int setIndexTextSize; - private int setPreviewPadding; - private boolean previewVisibility = true; - private int setIndexBarCornerRadius; - private Typeface setTypeface = null; - private Boolean setIndexBarVisibility = true; - private Boolean setSetIndexBarHighLightTextVisibility = false; - private Boolean setIndexBarStrokeVisibility = true; - public int mIndexBarStrokeWidth; - private @ColorInt - int mIndexBarStrokeColor; - private @ColorInt - int indexbarBackgroudColor; - private @ColorInt - int indexbarTextColor; - private @ColorInt - int indexbarHighLightTextColor; - - private int setPreviewTextSize; - private @ColorInt - int previewBackgroundColor; - private @ColorInt - int previewTextColor; - private int previewBackgroudAlpha; - private int indexbarBackgroudAlpha; - - public IndexFastScrollRecyclerSection(Context context, IndexFastScrollRecyclerView recyclerView) { - - setIndexTextSize = recyclerView.setIndexTextSize; - float setIndexbarWidth = recyclerView.mIndexbarWidth; - float setIndexbarMarginLeft = recyclerView.mIndexbarMarginLeft; - float setIndexbarMarginRight = recyclerView.mIndexbarMarginRight; - float setIndexbarMarginTop = recyclerView.mIndexbarMarginTop; - float setIndexbarMarginBottom = recyclerView.mIndexbarMarginBottom; - setPreviewPadding = recyclerView.mPreviewPadding; - setPreviewTextSize = recyclerView.mPreviewTextSize; - previewBackgroundColor = recyclerView.mPreviewBackgroudColor; - previewTextColor = recyclerView.mPreviewTextColor; - previewBackgroudAlpha = convertTransparentValueToBackgroundAlpha(recyclerView.mPreviewTransparentValue); - - mIndexBarStrokeColor = recyclerView.mSetIndexBarStrokeColor; - mIndexBarStrokeWidth = recyclerView.mIndexBarStrokeWidth; - - setIndexBarCornerRadius = recyclerView.mIndexBarCornerRadius; - indexbarBackgroudColor = recyclerView.mIndexbarBackgroudColor; - indexbarTextColor = recyclerView.mIndexbarTextColor; - indexbarHighLightTextColor = recyclerView.indexbarHighLightTextColor; - - indexbarBackgroudAlpha = convertTransparentValueToBackgroundAlpha(recyclerView.mIndexBarTransparentValue); - - mDensity = context.getResources().getDisplayMetrics().density; - mScaledDensity = context.getResources().getDisplayMetrics().scaledDensity; - mRecyclerView = recyclerView; - mRecyclerView.setLayoutManager(new LinearLayoutManager(context)); - //noinspection unchecked - setAdapter(mRecyclerView.getAdapter()); - - mIndexbarWidth = setIndexbarWidth * mDensity; - mIndexbarMarginLeft = setIndexbarMarginLeft * mDensity; - mIndexbarMarginRight = setIndexbarMarginRight * mDensity; - mIndexbarMarginTop = setIndexbarMarginTop * mDensity; - mIndexbarMarginBottom = setIndexbarMarginBottom * mDensity; - mPreviewPadding = setPreviewPadding * mDensity; - } - - public void draw(Canvas canvas) { - + */ class IndexFastScrollRecyclerSection( + context: Context, + recyclerView: IndexFastScrollRecyclerView +) : RecyclerView.AdapterDataObserver() { + private var mIndexbarWidth: Float + private var mIndexbarMarginLeft: Float + private var mIndexbarMarginRight: Float + private var mIndexbarMarginTop: Float + private var mIndexbarMarginBottom: Float + private val mPreviewPadding: Float + private val mDensity: Float + private val mScaledDensity: Float + private var mListViewWidth = 0 + private var mListViewHeight = 0 + private var mCurrentSection = -1 + private var mIsIndexing = false + private val mRecyclerView: RecyclerView? + private var mIndexer: SectionIndexer? = null + private var mSections: Array? = null + private var mIndexbarRect: RectF? = null + private var setIndexTextSize: Int + private var setPreviewPadding: Int + private var previewVisibility = true + private var setIndexBarCornerRadius: Int + private var setTypeface: Typeface? = null + private var setIndexBarVisibility = true + private var setSetIndexBarHighLightTextVisibility = false + private var setIndexBarStrokeVisibility = true + var mIndexBarStrokeWidth: Int + + @ColorInt + private var mIndexBarStrokeColor: Int + + @ColorInt + private var indexbarBackgroudColor: Int + + @ColorInt + private var indexbarTextColor: Int + + @ColorInt + private var indexbarHighLightTextColor: Int + private var setPreviewTextSize: Int + + @ColorInt + private var previewBackgroundColor: Int + + @ColorInt + private var previewTextColor: Int + private var previewBackgroudAlpha: Int + private var indexbarBackgroudAlpha: Int + fun draw(canvas: Canvas) { if (setIndexBarVisibility) { - - Paint indexbarPaint = new Paint(); - - indexbarPaint.setColor(indexbarBackgroudColor); - indexbarPaint.setAlpha(indexbarBackgroudAlpha); - indexbarPaint.setAntiAlias(true); - canvas.drawRoundRect(mIndexbarRect, setIndexBarCornerRadius * mDensity, - setIndexBarCornerRadius * mDensity, indexbarPaint); - + val indexbarPaint = Paint() + indexbarPaint.color = indexbarBackgroudColor + indexbarPaint.alpha = indexbarBackgroudAlpha + indexbarPaint.isAntiAlias = true + canvas.drawRoundRect( + mIndexbarRect!!, setIndexBarCornerRadius * mDensity, + setIndexBarCornerRadius * mDensity, indexbarPaint + ) if (setIndexBarStrokeVisibility) { - indexbarPaint.setStyle(Paint.Style.STROKE); - indexbarPaint.setColor(mIndexBarStrokeColor); - indexbarPaint.setStrokeWidth(mIndexBarStrokeWidth); // set stroke width - canvas.drawRoundRect(mIndexbarRect, setIndexBarCornerRadius * mDensity, - setIndexBarCornerRadius * mDensity, indexbarPaint); + indexbarPaint.style = Paint.Style.STROKE + indexbarPaint.color = mIndexBarStrokeColor + indexbarPaint.strokeWidth = mIndexBarStrokeWidth.toFloat() // set stroke width + canvas.drawRoundRect( + mIndexbarRect!!, setIndexBarCornerRadius * mDensity, + setIndexBarCornerRadius * mDensity, indexbarPaint + ) } - - if (mSections != null && mSections.length > 0) { + if (mSections != null && mSections!!.size > 0) { // Preview is shown when mCurrentSection is set - if (previewVisibility && mCurrentSection >= 0 - && !Objects.equals(mSections[mCurrentSection], "")) { - Paint previewPaint = new Paint(); - previewPaint.setColor(previewBackgroundColor); - previewPaint.setAlpha(previewBackgroudAlpha); - previewPaint.setAntiAlias(true); - previewPaint.setShadowLayer(3, 0, 0, - Color.argb(64, 0, 0, 0)); - - Paint previewTextPaint = new Paint(); - previewTextPaint.setColor(previewTextColor); - previewTextPaint.setAntiAlias(true); - previewTextPaint.setTextSize(setPreviewTextSize * mScaledDensity); - previewTextPaint.setTypeface(setTypeface); - - float previewTextWidth = previewTextPaint.measureText(mSections[mCurrentSection]); - float previewSize = 2 * mPreviewPadding + previewTextPaint.descent() - previewTextPaint.ascent(); - previewSize = Math.max(previewSize, previewTextWidth + 2 * mPreviewPadding); - RectF previewRect = new RectF((mListViewWidth - previewSize) / 2 - , (mListViewHeight - previewSize) / 2 - , (mListViewWidth - previewSize) / 2 + previewSize - , (mListViewHeight - previewSize) / 2 + previewSize); - - canvas.drawRoundRect(previewRect, 5 * mDensity, 5 * mDensity, previewPaint); - canvas.drawText(mSections[mCurrentSection], previewRect.left + (previewSize - previewTextWidth) / 2 - 1 - , previewRect.top + (previewSize - (previewTextPaint.descent() - previewTextPaint.ascent())) / 2 - previewTextPaint.ascent(), previewTextPaint); - setPreviewFadeTimeout(300); + if (previewVisibility && mCurrentSection >= 0 && mSections!![mCurrentSection] != "") { + val previewPaint = Paint() + previewPaint.color = previewBackgroundColor + previewPaint.alpha = previewBackgroudAlpha + previewPaint.isAntiAlias = true + previewPaint.setShadowLayer( + 3f, 0f, 0f, + Color.argb(64, 0, 0, 0) + ) + val previewTextPaint = Paint() + previewTextPaint.color = previewTextColor + previewTextPaint.isAntiAlias = true + previewTextPaint.textSize = setPreviewTextSize * mScaledDensity + previewTextPaint.typeface = setTypeface + val previewTextWidth = + previewTextPaint.measureText(mSections!![mCurrentSection]) + var previewSize = + 2 * mPreviewPadding + previewTextPaint.descent() - previewTextPaint.ascent() + previewSize = Math.max(previewSize, previewTextWidth + 2 * mPreviewPadding) + val previewRect = RectF( + (mListViewWidth - previewSize) / 2, + (mListViewHeight - previewSize) / 2, + (mListViewWidth - previewSize) / 2 + previewSize, + (mListViewHeight - previewSize) / 2 + previewSize + ) + canvas.drawRoundRect(previewRect, 5 * mDensity, 5 * mDensity, previewPaint) + canvas.drawText( + mSections!![mCurrentSection], + previewRect.left + (previewSize - previewTextWidth) / 2 - 1, + previewRect.top + (previewSize - (previewTextPaint.descent() - previewTextPaint.ascent())) / 2 - previewTextPaint.ascent(), + previewTextPaint + ) + setPreviewFadeTimeout(300) } - - Paint indexPaint = new Paint(); - indexPaint.setColor(indexbarTextColor); - indexPaint.setAntiAlias(true); - indexPaint.setTextSize(setIndexTextSize * mScaledDensity); - indexPaint.setTypeface(setTypeface); - - float sectionHeight = (mIndexbarRect.height() - mIndexbarMarginTop - mIndexbarMarginBottom) / mSections.length; - float paddingTop = (sectionHeight - (indexPaint.descent() - indexPaint.ascent())) / 2; - for (int i = 0; i < mSections.length; i++) { - + val indexPaint = Paint() + indexPaint.color = indexbarTextColor + indexPaint.isAntiAlias = true + indexPaint.textSize = setIndexTextSize * mScaledDensity + indexPaint.typeface = setTypeface + val sectionHeight = + (mIndexbarRect!!.height() - mIndexbarMarginTop - mIndexbarMarginBottom) / mSections!!.size + val paddingTop = (sectionHeight - (indexPaint.descent() - indexPaint.ascent())) / 2 + for (i in mSections!!.indices) { if (setSetIndexBarHighLightTextVisibility) { - if (mCurrentSection > -1 && i == mCurrentSection) { - indexPaint.setTypeface(Typeface.create(setTypeface, Typeface.BOLD)); - indexPaint.setTextSize((setIndexTextSize + 3) * mScaledDensity); - indexPaint.setColor(indexbarHighLightTextColor); + indexPaint.typeface = Typeface.create(setTypeface, Typeface.BOLD) + indexPaint.textSize = (setIndexTextSize + 3) * mScaledDensity + indexPaint.color = indexbarHighLightTextColor } else { - indexPaint.setTypeface(setTypeface); - indexPaint.setTextSize(setIndexTextSize * mScaledDensity); - indexPaint.setColor(indexbarTextColor); + indexPaint.typeface = setTypeface + indexPaint.textSize = setIndexTextSize * mScaledDensity + indexPaint.color = indexbarTextColor } - float paddingLeft = (mIndexbarWidth - indexPaint.measureText(mSections[i])) / 2; - canvas.drawText(mSections[i], mIndexbarRect.left + paddingLeft - , mIndexbarRect.top + mIndexbarMarginTop + sectionHeight * i + paddingTop - indexPaint.ascent(), indexPaint); - - + val paddingLeft = (mIndexbarWidth - indexPaint.measureText( + mSections!![i] + )) / 2 + canvas.drawText( + mSections!![i], + mIndexbarRect!!.left + paddingLeft, + mIndexbarRect!!.top + mIndexbarMarginTop + sectionHeight * i + paddingTop - indexPaint.ascent(), + indexPaint + ) } else { - float paddingLeft = (mIndexbarWidth - indexPaint.measureText(mSections[i])) / 2; - canvas.drawText(mSections[i], mIndexbarRect.left + paddingLeft - , mIndexbarRect.top + mIndexbarMarginTop + sectionHeight * i + paddingTop - indexPaint.ascent(), indexPaint); + val paddingLeft = (mIndexbarWidth - indexPaint.measureText( + mSections!![i] + )) / 2 + canvas.drawText( + mSections!![i], + mIndexbarRect!!.left + paddingLeft, + mIndexbarRect!!.top + mIndexbarMarginTop + sectionHeight * i + paddingTop - indexPaint.ascent(), + indexPaint + ) } - } } } } - public boolean onTouchEvent(MotionEvent ev) { - switch (ev.getAction()) { - case MotionEvent.ACTION_DOWN: - // If down event occurs inside index bar region, start indexing - if (contains(ev.getX(), ev.getY())) { + fun onTouchEvent(ev: MotionEvent): Boolean { + when (ev.action) { + MotionEvent.ACTION_DOWN -> // If down event occurs inside index bar region, start indexing + if (contains(ev.x, ev.y)) { // It demonstrates that the motion event started from index bar - mIsIndexing = true; + mIsIndexing = true // Determine which section the point is in, and move the list to that section - mCurrentSection = getSectionByPoint(ev.getY()); - scrollToPosition(); - return true; + mCurrentSection = getSectionByPoint(ev.y) + scrollToPosition() + return true } - break; - case MotionEvent.ACTION_MOVE: - if (mIsIndexing) { - // If this event moves inside index bar - if (contains(ev.getX(), ev.getY())) { - // Determine which section the point is in, and move the list to that section - mCurrentSection = getSectionByPoint(ev.getY()); - scrollToPosition(); - } - return true; - } - break; - case MotionEvent.ACTION_UP: - if (mIsIndexing) { - mIsIndexing = false; - mCurrentSection = -1; + MotionEvent.ACTION_MOVE -> if (mIsIndexing) { + // If this event moves inside index bar + if (contains(ev.x, ev.y)) { + // Determine which section the point is in, and move the list to that section + mCurrentSection = getSectionByPoint(ev.y) + scrollToPosition() } - break; + return true + } + MotionEvent.ACTION_UP -> if (mIsIndexing) { + mIsIndexing = false + mCurrentSection = -1 + } } - return false; + return false } - private void scrollToPosition() { + private fun scrollToPosition() { try { - int position = mIndexer.getPositionForSection(mCurrentSection); - RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager(); - if (layoutManager instanceof LinearLayoutManager) { - ((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(position, 0); - } else if (null != layoutManager) { - layoutManager.scrollToPosition(position); - } - } catch (Exception e) { - Log.d("INDEX_BAR", "Data size returns null"); + val position = mIndexer?.getPositionForSection(mCurrentSection) + val layoutManager = mRecyclerView!!.layoutManager + if (layoutManager is LinearLayoutManager) { + position?.let { layoutManager.scrollToPositionWithOffset(it, 0) } + } else position?.let { layoutManager?.scrollToPosition(it) } + } catch (e: Exception) { + Log.d("INDEX_BAR", "Data size returns null") } } - public void onSizeChanged(int w, int h) { - mListViewWidth = w; - mListViewHeight = h; - mIndexbarRect = new RectF( - w - mIndexbarMarginLeft - mIndexbarWidth, - mIndexbarMarginTop, - w - mIndexbarMarginRight, - h - mIndexbarMarginBottom - (mRecyclerView.getClipToPadding() - ? 0 : mRecyclerView.getPaddingBottom()) - ); + fun onSizeChanged(w: Int, h: Int) { + mListViewWidth = w + mListViewHeight = h + mIndexbarRect = RectF( + w - mIndexbarMarginLeft - mIndexbarWidth, + mIndexbarMarginTop, + w - mIndexbarMarginRight, + h - mIndexbarMarginBottom - if (mRecyclerView!!.clipToPadding) 0 else mRecyclerView.paddingBottom + ) } - public void setAdapter(RecyclerView.Adapter adapter) { - if (adapter instanceof SectionIndexer) { - adapter.registerAdapterDataObserver(this); - mIndexer = (SectionIndexer) adapter; - mSections = (String[]) mIndexer.getSections(); + fun setAdapter(adapter: RecyclerView.Adapter?) { + if (adapter is SectionIndexer) { + adapter.registerAdapterDataObserver(this) + mIndexer = adapter + mSections = mIndexer?.sections as Array } } - @Override - public void onChanged() { - super.onChanged(); - updateSections(); + override fun onChanged() { + super.onChanged() + updateSections() } - public void updateSections() { - mSections = (String[]) mIndexer.getSections(); + fun updateSections() { + mSections = mIndexer?.sections as Array } - public boolean contains(float x, float y) { + fun contains(x: Float, y: Float): Boolean { // Determine if the point is in index bar region, which includes the right margin of the bar - return (x >= mIndexbarRect.left && y >= mIndexbarRect.top && y <= mIndexbarRect.top + mIndexbarRect.height()); + return x >= mIndexbarRect!!.left && y >= mIndexbarRect!!.top && y <= mIndexbarRect!!.top + mIndexbarRect!!.height() } - private int getSectionByPoint(float y) { - if (mSections == null || mSections.length == 0) - return 0; - if (y < mIndexbarRect.top + mIndexbarMarginTop) - return 0; - if (y >= mIndexbarRect.top + mIndexbarRect.height() - mIndexbarMarginTop) - return mSections.length - 1; - return (int) ((y - mIndexbarRect.top - mIndexbarMarginTop) / ((mIndexbarRect.height() - mIndexbarMarginBottom - mIndexbarMarginTop) / mSections.length)); + private fun getSectionByPoint(y: Float): Int { + if (mSections == null || mSections?.isEmpty() == true) return 0 + if (y < mIndexbarRect!!.top + mIndexbarMarginTop) return 0 + return if (y >= mIndexbarRect!!.top + mIndexbarRect!!.height() - mIndexbarMarginTop) mSections!!.size - 1 else ((y - mIndexbarRect!!.top - mIndexbarMarginTop) / ((mIndexbarRect!!.height() - mIndexbarMarginBottom - mIndexbarMarginTop) / mSections!!.size)).toInt() } - private Runnable mLastFadeRunnable = null; - - private void setPreviewFadeTimeout(long delay) { + private var mLastFadeRunnable: Runnable? = null + private fun setPreviewFadeTimeout(delay: Long) { if (mRecyclerView != null) { if (mLastFadeRunnable != null) { - mRecyclerView.removeCallbacks(mLastFadeRunnable); + mRecyclerView.removeCallbacks(mLastFadeRunnable) } - mLastFadeRunnable = mRecyclerView::invalidate; - mRecyclerView.postDelayed(mLastFadeRunnable, delay); + mLastFadeRunnable = Runnable { mRecyclerView.invalidate() } + mRecyclerView.postDelayed(mLastFadeRunnable, delay) } } - private int convertTransparentValueToBackgroundAlpha(float value) { - return (int) (255 * value); + private fun convertTransparentValueToBackgroundAlpha(value: Float): Int { + return (255 * value).toInt() } /** * @param value int to set the text size of the index bar */ - public void setIndexTextSize(int value) { - setIndexTextSize = value; + fun setIndexTextSize(value: Int) { + setIndexTextSize = value } /** * @param value float to set the width of the index bar */ - public void setIndexbarWidth(float value) { - mIndexbarWidth = value; + fun setIndexbarWidth(value: Float) { + mIndexbarWidth = value } /** * @param value float to set the margin of the index bar */ - public void setIndexbarMargin(float value) { - mIndexbarMarginLeft = value; - mIndexbarMarginRight = value; - mIndexbarMarginTop = value; - mIndexbarMarginBottom = value; + fun setIndexbarMargin(value: Float) { + mIndexbarMarginLeft = value + mIndexbarMarginRight = value + mIndexbarMarginTop = value + mIndexbarMarginBottom = value } /** * @param value float to set the top margin of the index bar */ - public void setIndexbarTopMargin(float value) { - mIndexbarMarginTop = value; + fun setIndexbarTopMargin(value: Float) { + mIndexbarMarginTop = value } /** * @param value float to set the bottom margin of the index bar */ - public void setIndexbarBottomMargin(float value) { - mIndexbarMarginBottom = value; + fun setIndexbarBottomMargin(value: Float) { + mIndexbarMarginBottom = value } /** * @param value float to set the left margin of the index bar */ - public void setIndexbarHorizontalMargin(float value) { - mIndexbarMarginLeft = value; - mIndexbarMarginRight = value; + fun setIndexbarHorizontalMargin(value: Float) { + mIndexbarMarginLeft = value + mIndexbarMarginRight = value } /** * @param value float to set the right margin of the index bar */ - public void setIndexbarVerticalMargin(float value) { - mIndexbarMarginTop = value; - mIndexbarMarginBottom = value; + fun setIndexbarVerticalMargin(value: Float) { + mIndexbarMarginTop = value + mIndexbarMarginBottom = value } /** * @param value int to set preview padding */ - public void setPreviewPadding(int value) { - setPreviewPadding = value; + fun setPreviewPadding(value: Int) { + setPreviewPadding = value } /** * @param value int to set the radius of the index bar */ - public void setIndexBarCornerRadius(int value) { - setIndexBarCornerRadius = value; + fun setIndexBarCornerRadius(value: Int) { + setIndexBarCornerRadius = value } /** * @param value float to set the transparency of the color for index bar */ - public void setIndexBarTransparentValue(float value) { - indexbarBackgroudAlpha = convertTransparentValueToBackgroundAlpha(value); + fun setIndexBarTransparentValue(value: Float) { + indexbarBackgroudAlpha = convertTransparentValueToBackgroundAlpha(value) } /** * @param typeface Typeface to set the typeface of the preview & the index bar */ - public void setTypeface(Typeface typeface) { - setTypeface = typeface; + fun setTypeface(typeface: Typeface?) { + setTypeface = typeface } /** * @param shown boolean to show or hide the index bar */ - public void setIndexBarVisibility(boolean shown) { - setIndexBarVisibility = shown; + fun setIndexBarVisibility(shown: Boolean) { + setIndexBarVisibility = shown } - /** * @param shown boolean to show or hide the index bar */ - public void setIndexBarStrokeVisibility(boolean shown) { - setIndexBarStrokeVisibility = shown; + fun setIndexBarStrokeVisibility(shown: Boolean) { + setIndexBarStrokeVisibility = shown } /** * @param shown boolean to show or hide the preview box */ - public void setPreviewVisibility(boolean shown) { - previewVisibility = shown; + fun setPreviewVisibility(shown: Boolean) { + previewVisibility = shown } /** * @param value int to set the text size of the preview box */ - public void setIndexBarStrokeWidth(int value) { - mIndexBarStrokeWidth = value; + fun setIndexBarStrokeWidth(value: Int) { + mIndexBarStrokeWidth = value } - /** * @param value int to set the text size of the preview box */ - public void setPreviewTextSize(int value) { - setPreviewTextSize = value; + fun setPreviewTextSize(value: Int) { + setPreviewTextSize = value } /** * @param color The color for the preview box */ - public void setPreviewColor(@ColorInt int color) { - previewBackgroundColor = color; + fun setPreviewColor(@ColorInt color: Int) { + previewBackgroundColor = color } /** * @param color The text color for the preview box */ - public void setPreviewTextColor(@ColorInt int color) { - previewTextColor = color; + fun setPreviewTextColor(@ColorInt color: Int) { + previewTextColor = color } /** * @param value float to set the transparency value of the preview box */ - public void setPreviewTransparentValue(float value) { - previewBackgroudAlpha = convertTransparentValueToBackgroundAlpha(value); + fun setPreviewTransparentValue(value: Float) { + previewBackgroudAlpha = convertTransparentValueToBackgroundAlpha(value) } /** * @param color The color for the scroll track */ - public void setIndexBarColor(@ColorInt int color) { - indexbarBackgroudColor = color; + fun setIndexBarColor(@ColorInt color: Int) { + indexbarBackgroudColor = color } /** * @param color The text color for the index bar */ - public void setIndexBarTextColor(@ColorInt int color) { - indexbarTextColor = color; + fun setIndexBarTextColor(@ColorInt color: Int) { + indexbarTextColor = color } /** * @param color The text color for the index bar */ - public void setIndexBarStrokeColor(@ColorInt int color) { - mIndexBarStrokeColor = color; + fun setIndexBarStrokeColor(@ColorInt color: Int) { + mIndexBarStrokeColor = color } - /** * @param color The text color for the index bar */ - public void setIndexbarHighLightTextColor(@ColorInt int color) { - indexbarHighLightTextColor = color; + fun setIndexbarHighLightTextColor(@ColorInt color: Int) { + indexbarHighLightTextColor = color } /** * @param shown boolean to show or hide the index bar */ - public void setIndexBarHighLightTextVisibility(boolean shown) { - setSetIndexBarHighLightTextVisibility = shown; - } - -} + fun setIndexBarHighLightTextVisibility(shown: Boolean) { + setSetIndexBarHighLightTextVisibility = shown + } + + init { + setIndexTextSize = recyclerView.setIndexTextSize + val setIndexbarWidth = recyclerView.mIndexbarWidth + val setIndexbarMarginLeft = recyclerView.mIndexbarMarginLeft + val setIndexbarMarginRight = recyclerView.mIndexbarMarginRight + val setIndexbarMarginTop = recyclerView.mIndexbarMarginTop + val setIndexbarMarginBottom = recyclerView.mIndexbarMarginBottom + setPreviewPadding = recyclerView.mPreviewPadding + setPreviewTextSize = recyclerView.mPreviewTextSize + previewBackgroundColor = recyclerView.mPreviewBackgroudColor + previewTextColor = recyclerView.mPreviewTextColor + previewBackgroudAlpha = + convertTransparentValueToBackgroundAlpha(recyclerView.mPreviewTransparentValue) + mIndexBarStrokeColor = recyclerView.mSetIndexBarStrokeColor + mIndexBarStrokeWidth = recyclerView.mIndexBarStrokeWidth + setIndexBarCornerRadius = recyclerView.mIndexBarCornerRadius + indexbarBackgroudColor = recyclerView.mIndexbarBackgroudColor + indexbarTextColor = recyclerView.mIndexbarTextColor + indexbarHighLightTextColor = recyclerView.mIndexbarHighLightTextColor + indexbarBackgroudAlpha = + convertTransparentValueToBackgroundAlpha(recyclerView.mIndexBarTransparentValue) + mDensity = context.resources.displayMetrics.density + mScaledDensity = context.resources.displayMetrics.scaledDensity + mRecyclerView = recyclerView + mRecyclerView.setLayoutManager(LinearLayoutManager(context)) + setAdapter(mRecyclerView.getAdapter()) + mIndexbarWidth = setIndexbarWidth * mDensity + mIndexbarMarginLeft = setIndexbarMarginLeft * mDensity + mIndexbarMarginRight = setIndexbarMarginRight * mDensity + mIndexbarMarginTop = setIndexbarMarginTop * mDensity + mIndexbarMarginBottom = setIndexbarMarginBottom * mDensity + mPreviewPadding = setPreviewPadding * mDensity + } +} \ No newline at end of file diff --git a/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerView.kt b/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerView.kt index ca1d45a..c7099f9 100644 --- a/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerView.kt +++ b/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerView.kt @@ -1,478 +1,529 @@ -package in.myinnos.alphabetsindexfastscrollrecycler; +package `in`.myinnos.alphabetsindexfastscrollrecycler + + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Typeface +import android.util.AttributeSet +import android.util.TypedValue +import android.view.GestureDetector +import android.view.GestureDetector.SimpleOnGestureListener +import android.view.MotionEvent +import androidx.annotation.ColorInt +import androidx.annotation.ColorRes +import androidx.core.content.ContextCompat +import androidx.recyclerview.widget.RecyclerView /* * Created by MyInnos on 31-01-2017. * Updated by AbandonedCart 07-2022. - */ - -import android.annotation.SuppressLint; -import android.content.Context; -import android.content.res.TypedArray; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Typeface; -import android.util.AttributeSet; -import android.util.TypedValue; -import android.view.GestureDetector; -import android.view.MotionEvent; - -import androidx.annotation.ColorInt; -import androidx.annotation.ColorRes; -import androidx.annotation.NonNull; -import androidx.core.content.ContextCompat; -import androidx.recyclerview.widget.RecyclerView; - -@SuppressWarnings("unused") -public class IndexFastScrollRecyclerView extends RecyclerView { - - private IndexFastScrollRecyclerSection mScroller = null; - private GestureDetector mGestureDetector = null; - - private boolean mEnabled = true; - private boolean mTransient = false; - - public int setIndexTextSize = 12; - public float mIndexbarWidth = 20; - public float mIndexbarMarginLeft = 2; - public float mIndexbarMarginRight = 2; - public float mIndexbarMarginTop = 2; - public float mIndexbarMarginBottom = 2; - public int mPreviewPadding = 5; - public int mIndexBarCornerRadius = 5; - public float mIndexBarTransparentValue = (float) 0.6; - public int mIndexBarStrokeWidth = 2; - public @ColorInt - int mSetIndexBarStrokeColor = Color.BLACK; - public @ColorInt - int mIndexbarBackgroudColor = Color.BLACK; - public @ColorInt - int mIndexbarTextColor = Color.WHITE; - public @ColorInt - int indexbarHighLightTextColor = Color.BLACK; - - public int mPreviewTextSize = 50; - public @ColorInt - int mPreviewBackgroudColor = Color.BLACK; - public @ColorInt - int mPreviewTextColor = Color.WHITE; - public float mPreviewTransparentValue = (float) 0.4; - - public IndexFastScrollRecyclerView(Context context) { - super(context); - } - - public IndexFastScrollRecyclerView(Context context, AttributeSet attrs) { - super(context, attrs); - init(context, attrs); - } - - public IndexFastScrollRecyclerView(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - init(context, attrs); - } - - private void init(Context context, AttributeSet attrs) { + */ class IndexFastScrollRecyclerView : RecyclerView { + private var mScroller: IndexFastScrollRecyclerSection? = null + private var mGestureDetector: GestureDetector? = null + private var mEnabled = true + private var mTransient = false + var setIndexTextSize = 12 + var mIndexbarWidth = 20f + var mIndexbarMarginLeft = 2f + var mIndexbarMarginRight = 2f + var mIndexbarMarginTop = 2f + var mIndexbarMarginBottom = 2f + var mPreviewPadding = 5 + var mIndexBarCornerRadius = 5 + var mIndexBarTransparentValue = 0.6.toFloat() + var mIndexBarStrokeWidth = 2 - if (attrs != null) { - TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.IndexFastScrollRecyclerView); - - try { - setIndexTextSize = typedArray.getInt(R.styleable.IndexFastScrollRecyclerView_setIndexTextSize, setIndexTextSize); - mIndexbarWidth = typedArray.getFloat(R.styleable.IndexFastScrollRecyclerView_setIndexbarWidth, mIndexbarWidth); - mIndexbarMarginLeft = typedArray.getFloat(R.styleable.IndexFastScrollRecyclerView_setIndexbarMargin, mIndexbarMarginLeft); - mIndexbarMarginRight = typedArray.getFloat(R.styleable.IndexFastScrollRecyclerView_setIndexbarMargin, mIndexbarMarginRight); - mIndexbarMarginTop = typedArray.getFloat(R.styleable.IndexFastScrollRecyclerView_setIndexbarMargin, mIndexbarMarginTop); - mIndexbarMarginBottom = typedArray.getFloat(R.styleable.IndexFastScrollRecyclerView_setIndexbarMargin, mIndexbarMarginBottom); - mPreviewPadding = typedArray.getInt(R.styleable.IndexFastScrollRecyclerView_setPreviewPadding, mPreviewPadding); - mIndexBarCornerRadius = typedArray.getInt(R.styleable.IndexFastScrollRecyclerView_setIndexBarCornerRadius, mIndexBarCornerRadius); - mIndexBarTransparentValue = typedArray.getFloat(R.styleable.IndexFastScrollRecyclerView_setIndexBarTransparentValue, mIndexBarTransparentValue); - - mEnabled = true; - if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarShown)) { - mEnabled = typedArray.getBoolean(R.styleable.IndexFastScrollRecyclerView_setIndexBarShown, mEnabled); - } + @ColorInt + var mSetIndexBarStrokeColor = Color.BLACK - mTransient = false; - if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setTransientIndexBar)) { - mTransient = typedArray.getBoolean(R.styleable.IndexFastScrollRecyclerView_setTransientIndexBar, mTransient); - } - setTransientIndexBar(mTransient); - - if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor)) { - TypedValue tv = new TypedValue(); - typedArray.getValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, tv); - if (tv.type == TypedValue.TYPE_STRING) { - mIndexbarBackgroudColor = Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor)); - } else { - mIndexbarBackgroudColor = typedArray.getColor(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, mIndexbarBackgroudColor); - } - } + @ColorInt + var mIndexbarBackgroudColor = Color.BLACK - if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarTextColor)) { - TypedValue tv = new TypedValue(); - typedArray.getValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, tv); - if (tv.type == TypedValue.TYPE_STRING) { - mIndexbarTextColor = Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarTextColor)); - } else { - mIndexbarTextColor = typedArray.getColor(R.styleable.IndexFastScrollRecyclerView_setIndexBarTextColor, mIndexbarTextColor); - } - } + @ColorInt + var mIndexbarTextColor = Color.WHITE - if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarHighlightTextColor)) { - TypedValue tv = new TypedValue(); - typedArray.getValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, tv); - if (tv.type == TypedValue.TYPE_STRING) { - indexbarHighLightTextColor = Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarHighlightTextColor)); - } else { - indexbarHighLightTextColor = typedArray.getColor(R.styleable.IndexFastScrollRecyclerView_setIndexBarHighlightTextColor, indexbarHighLightTextColor); - } - } + @ColorInt + var mIndexbarHighLightTextColor = Color.BLACK - mPreviewTextSize = typedArray.getInt(R.styleable.IndexFastScrollRecyclerView_setPreviewTextSize, mPreviewTextSize); - mPreviewTransparentValue = typedArray.getFloat(R.styleable.IndexFastScrollRecyclerView_setPreviewTransparentValue, mPreviewTransparentValue); - - if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setPreviewColor)) { - TypedValue tv = new TypedValue(); - typedArray.getValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, tv); - if (tv.type == TypedValue.TYPE_STRING) { - mPreviewBackgroudColor = Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setPreviewColor)); - } else { - mPreviewBackgroudColor = typedArray.getColor(R.styleable.IndexFastScrollRecyclerView_setPreviewColor, mPreviewBackgroudColor); - } - } + var mPreviewTextSize = 50 - if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setPreviewTextColor)) { - TypedValue tv = new TypedValue(); - typedArray.getValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, tv); - if (tv.type == TypedValue.TYPE_STRING) { - mPreviewTextColor = Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setPreviewTextColor)); - } else { - mPreviewTextColor = typedArray.getColor(R.styleable.IndexFastScrollRecyclerView_setPreviewTextColor, mPreviewTextColor); - } - } + @ColorInt + var mPreviewBackgroudColor = Color.BLACK - if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarStrokeWidth)) { - mIndexBarStrokeWidth = typedArray.getInt(R.styleable.IndexFastScrollRecyclerView_setIndexBarStrokeWidth, mIndexBarStrokeWidth); - } + @ColorInt + var mPreviewTextColor = Color.WHITE + var mPreviewTransparentValue = 0.4.toFloat() - if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarStrokeColor)) { - TypedValue tv = new TypedValue(); - typedArray.getValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, tv); - if (tv.type == TypedValue.TYPE_STRING) { - mSetIndexBarStrokeColor = Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarStrokeColor)); - } else { - mSetIndexBarStrokeColor = typedArray.getColor(R.styleable.IndexFastScrollRecyclerView_setIndexBarStrokeColor, mSetIndexBarStrokeColor); - } - } + constructor(context: Context?) : super(context!!) {} + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { + init(context, attrs) + } + + constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super( + context, + attrs, + defStyle + ) { + init(context, attrs) + } - } finally { - typedArray.recycle(); + private fun init(context: Context, attrs: AttributeSet?) { + if (attrs != null) { + val typedArray = + context.obtainStyledAttributes(attrs, R.styleable.IndexFastScrollRecyclerView) + try { + setIndexTextSize = typedArray.getInt( + R.styleable.IndexFastScrollRecyclerView_setIndexTextSize, + setIndexTextSize + ) + mIndexbarWidth = typedArray.getFloat( + R.styleable.IndexFastScrollRecyclerView_setIndexbarWidth, + mIndexbarWidth + ) + mIndexbarMarginLeft = typedArray.getFloat( + R.styleable.IndexFastScrollRecyclerView_setIndexbarMargin, + mIndexbarMarginLeft + ) + mIndexbarMarginRight = typedArray.getFloat( + R.styleable.IndexFastScrollRecyclerView_setIndexbarMargin, + mIndexbarMarginRight + ) + mIndexbarMarginTop = typedArray.getFloat( + R.styleable.IndexFastScrollRecyclerView_setIndexbarMargin, + mIndexbarMarginTop + ) + mIndexbarMarginBottom = typedArray.getFloat( + R.styleable.IndexFastScrollRecyclerView_setIndexbarMargin, + mIndexbarMarginBottom + ) + mPreviewPadding = typedArray.getInt( + R.styleable.IndexFastScrollRecyclerView_setPreviewPadding, + mPreviewPadding + ) + mIndexBarCornerRadius = typedArray.getInt( + R.styleable.IndexFastScrollRecyclerView_setIndexBarCornerRadius, + mIndexBarCornerRadius + ) + mIndexBarTransparentValue = typedArray.getFloat( + R.styleable.IndexFastScrollRecyclerView_setIndexBarTransparentValue, + mIndexBarTransparentValue + ) + mEnabled = true + if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarShown)) { + mEnabled = typedArray.getBoolean( + R.styleable.IndexFastScrollRecyclerView_setIndexBarShown, + mEnabled + ) + } + mTransient = false + if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setTransientIndexBar)) { + mTransient = typedArray.getBoolean( + R.styleable.IndexFastScrollRecyclerView_setTransientIndexBar, + mTransient + ) + } + setTransientIndexBar(mTransient) + if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor)) { + val tv = TypedValue() + typedArray.getValue( + R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, + tv + ) + mIndexbarBackgroudColor = if (tv.type == TypedValue.TYPE_STRING) { + Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor)) + } else { + typedArray.getColor( + R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, + mIndexbarBackgroudColor + ) + } + } + if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarTextColor)) { + val tv = TypedValue() + typedArray.getValue( + R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, + tv + ) + mIndexbarTextColor = if (tv.type == TypedValue.TYPE_STRING) { + Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarTextColor)) + } else { + typedArray.getColor( + R.styleable.IndexFastScrollRecyclerView_setIndexBarTextColor, + mIndexbarTextColor + ) + } } + if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarHighlightTextColor)) { + val tv = TypedValue() + typedArray.getValue( + R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, + tv + ) + mIndexbarHighLightTextColor = if (tv.type == TypedValue.TYPE_STRING) { + Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarHighlightTextColor)) + } else { + typedArray.getColor( + R.styleable.IndexFastScrollRecyclerView_setIndexBarHighlightTextColor, + mIndexbarHighLightTextColor + ) + } + } + mPreviewTextSize = typedArray.getInt( + R.styleable.IndexFastScrollRecyclerView_setPreviewTextSize, + mPreviewTextSize + ) + mPreviewTransparentValue = typedArray.getFloat( + R.styleable.IndexFastScrollRecyclerView_setPreviewTransparentValue, + mPreviewTransparentValue + ) + if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setPreviewColor)) { + val tv = TypedValue() + typedArray.getValue( + R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, + tv + ) + mPreviewBackgroudColor = if (tv.type == TypedValue.TYPE_STRING) { + Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setPreviewColor)) + } else { + typedArray.getColor( + R.styleable.IndexFastScrollRecyclerView_setPreviewColor, + mPreviewBackgroudColor + ) + } + } + if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setPreviewTextColor)) { + val tv = TypedValue() + typedArray.getValue( + R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, + tv + ) + mPreviewTextColor = if (tv.type == TypedValue.TYPE_STRING) { + Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setPreviewTextColor)) + } else { + typedArray.getColor( + R.styleable.IndexFastScrollRecyclerView_setPreviewTextColor, + mPreviewTextColor + ) + } + } + if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarStrokeWidth)) { + mIndexBarStrokeWidth = typedArray.getInt( + R.styleable.IndexFastScrollRecyclerView_setIndexBarStrokeWidth, + mIndexBarStrokeWidth + ) + } + if (typedArray.hasValue(R.styleable.IndexFastScrollRecyclerView_setIndexBarStrokeColor)) { + val tv = TypedValue() + typedArray.getValue( + R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, + tv + ) + mSetIndexBarStrokeColor = if (tv.type == TypedValue.TYPE_STRING) { + Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarStrokeColor)) + } else { + typedArray.getColor( + R.styleable.IndexFastScrollRecyclerView_setIndexBarStrokeColor, + mSetIndexBarStrokeColor + ) + } + } + } finally { + typedArray.recycle() + } - // This line here is neccesary else the attributes won't be updated if a value is passed from XML - mScroller = new IndexFastScrollRecyclerSection(context, this); - mScroller.setIndexBarVisibility(mEnabled); + // This line here is neccesary else the attributes won't be updated if a value is passed from XML + mScroller = IndexFastScrollRecyclerSection(context, this) + mScroller!!.setIndexBarVisibility(mEnabled) } } - @Override - public void draw(Canvas canvas) { - super.draw(canvas); + override fun draw(canvas: Canvas) { + super.draw(canvas) // Overlay index bar - if (mScroller != null) - mScroller.draw(canvas); + if (mScroller != null) mScroller!!.draw(canvas) } @SuppressLint("ClickableViewAccessibility") - @Override - public boolean onTouchEvent(MotionEvent ev) { + override fun onTouchEvent(ev: MotionEvent): Boolean { if (mEnabled) { // Intercept ListView's touch event - if (mScroller != null && mScroller.onTouchEvent(ev)) - return true; - + if (mScroller != null && mScroller!!.onTouchEvent(ev)) return true if (mGestureDetector == null) { - mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() { - - @Override - public boolean onFling(MotionEvent e1, MotionEvent e2, - float velocityX, float velocityY) { - return super.onFling(e1, e2, velocityX, velocityY); + mGestureDetector = GestureDetector(context, object : SimpleOnGestureListener() { + override fun onFling( + e1: MotionEvent, e2: MotionEvent, + velocityX: Float, velocityY: Float + ): Boolean { + return super.onFling(e1, e2, velocityX, velocityY) } - - }); + }) } - mGestureDetector.onTouchEvent(ev); + mGestureDetector!!.onTouchEvent(ev) } - - return super.onTouchEvent(ev); + return super.onTouchEvent(ev) } - @Override - public boolean onInterceptTouchEvent(MotionEvent ev) { - if (mEnabled && mScroller != null && mScroller.contains(ev.getX(), ev.getY())) - return true; - - return super.onInterceptTouchEvent(ev); + override fun onInterceptTouchEvent(ev: MotionEvent): Boolean { + return if (mEnabled && mScroller != null && mScroller!!.contains( + ev.x, + ev.y + ) + ) true else super.onInterceptTouchEvent( + ev + ) } - @Override - public void setAdapter(Adapter adapter) { - super.setAdapter(adapter); - if (mScroller != null) - //noinspection unchecked - mScroller.setAdapter(adapter); + override fun setAdapter(adapter: Adapter<*>?) { + super.setAdapter(adapter) + if (mScroller != null) mScroller?.setAdapter(adapter as Adapter?) } - @Override - protected void onSizeChanged(int w, int h, int oldw, int oldh) { - super.onSizeChanged(w, h, oldw, oldh); - if (mScroller != null) - mScroller.onSizeChanged(w, h); + override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { + super.onSizeChanged(w, h, oldw, oldh) + if (mScroller != null) mScroller?.onSizeChanged(w, h) } /** * @param value int to set the text size of the index bar */ - public void setIndexTextSize(int value) { - mScroller.setIndexTextSize(value); + fun setIndexTextSize(value: Int) { + mScroller!!.setIndexTextSize(value) } /** * @param value float to set the width of the index bar */ - public void setIndexbarWidth(float value) { - mScroller.setIndexbarWidth(value); + fun setIndexbarWidth(value: Float) { + mScroller!!.setIndexbarWidth(value) } /** * @param value float to set the margin of the index bar */ - public void setIndexbarMargin(float value) { - mScroller.setIndexbarMargin(value); + fun setIndexbarMargin(value: Float) { + mScroller!!.setIndexbarMargin(value) } /** * @param value float to set the top margin of the index bar */ - public void setIndexbarTopMargin(float value) { - mScroller.setIndexbarTopMargin(value); + fun setIndexbarTopMargin(value: Float) { + mScroller!!.setIndexbarTopMargin(value) } /** * @param value float to set the bottom margin of the index bar */ - public void setIndexbarBottomMargin(float value) { - mScroller.setIndexbarBottomMargin(value); + fun setIndexbarBottomMargin(value: Float) { + mScroller!!.setIndexbarBottomMargin(value) } /** * @param value float to set the Horizontal margin of the index bar */ - public void setIndexbarHorizontalMargin(float value) { - mScroller.setIndexbarHorizontalMargin(value); + fun setIndexbarHorizontalMargin(value: Float) { + mScroller!!.setIndexbarHorizontalMargin(value) } /** * @param value float to set the Vertical margin of the index bar */ - public void setIndexbarVerticalMargin(float value) { - mScroller.setIndexbarVerticalMargin(value); + fun setIndexbarVerticalMargin(value: Float) { + mScroller!!.setIndexbarVerticalMargin(value) } /** * @param value int to set the preview padding */ - public void setPreviewPadding(int value) { - mScroller.setPreviewPadding(value); + fun setPreviewPadding(value: Int) { + mScroller!!.setPreviewPadding(value) } /** * @param value int to set the corner radius of the index bar */ - public void setIndexBarCornerRadius(int value) { - mScroller.setIndexBarCornerRadius(value); + fun setIndexBarCornerRadius(value: Int) { + mScroller!!.setIndexBarCornerRadius(value) } /** * @param value float to set the transparency value of the index bar */ - public void setIndexBarTransparentValue(float value) { - mScroller.setIndexBarTransparentValue(value); + fun setIndexBarTransparentValue(value: Float) { + mScroller!!.setIndexBarTransparentValue(value) } /** * @param typeface Typeface to set the typeface of the preview & the index bar */ - public void setTypeface(Typeface typeface) { - mScroller.setTypeface(typeface); + fun setTypeface(typeface: Typeface?) { + mScroller!!.setTypeface(typeface) } /** * @param shown boolean to show or hide the index bar */ - public void setIndexBarVisibility(boolean shown) { - mScroller.setIndexBarVisibility(shown); - mEnabled = shown; - } - - private final Runnable scrollRunnable = () -> { - mScroller.setIndexBarVisibility(false); - invalidate(); - }; - private final OnScrollListener scrollListener = new RecyclerView.OnScrollListener() { - @Override - public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { - super.onScrollStateChanged(recyclerView, newState); - if (newState == RecyclerView.SCROLL_STATE_DRAGGING) { - recyclerView.removeCallbacks(scrollRunnable); - mScroller.setIndexBarVisibility(true); - } else if (newState == RecyclerView.SCROLL_STATE_IDLE) { - recyclerView.postDelayed(scrollRunnable, 1000); + fun setIndexBarVisibility(shown: Boolean) { + mScroller!!.setIndexBarVisibility(shown) + mEnabled = shown + } + + private val scrollRunnable = Runnable { + mScroller!!.setIndexBarVisibility(false) + invalidate() + } + private val scrollListener: OnScrollListener = object : OnScrollListener() { + override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { + super.onScrollStateChanged(recyclerView, newState) + if (newState == SCROLL_STATE_DRAGGING) { + recyclerView.removeCallbacks(scrollRunnable) + mScroller!!.setIndexBarVisibility(true) + } else if (newState == SCROLL_STATE_IDLE) { + recyclerView.postDelayed(scrollRunnable, 1000) } } - }; - public void setTransientIndexBar(boolean enabled) { + } + + fun setTransientIndexBar(enabled: Boolean) { if (enabled) { - //noinspection deprecation - setOnScrollListener(scrollListener); + setOnScrollListener(scrollListener) } else { - removeCallbacks(scrollRunnable); - removeOnScrollListener(scrollListener); + removeCallbacks(scrollRunnable) + removeOnScrollListener(scrollListener) } - mTransient = enabled; + mTransient = enabled } /** * @param shown boolean to show or hide the index bar */ - public void setIndexBarStrokeVisibility(boolean shown) { - mScroller.setIndexBarStrokeVisibility(shown); + fun setIndexBarStrokeVisibility(shown: Boolean) { + mScroller!!.setIndexBarStrokeVisibility(shown) } /** * @param color The color for the index bar */ - public void setIndexBarStrokeColor(String color) { - mScroller.setIndexBarStrokeColor(Color.parseColor(color)); + fun setIndexBarStrokeColor(color: String?) { + mScroller!!.setIndexBarStrokeColor(Color.parseColor(color)) } /** * @param color The color for the preview box */ - public void setIndexBarStrokeColor(@ColorRes int color) { - mScroller.setIndexBarStrokeColor(ContextCompat.getColor(getContext(), color)); + fun setIndexBarStrokeColor(@ColorRes color: Int) { + mScroller!!.setIndexBarStrokeColor(ContextCompat.getColor(context, color)) } /** * @param value int to set the text size of the preview box */ - public void setIndexBarStrokeWidth(int value) { - mScroller.setIndexBarStrokeWidth(value); + fun setIndexBarStrokeWidth(value: Int) { + mScroller!!.setIndexBarStrokeWidth(value) } - /** * @param shown boolean to show or hide the preview */ - public void setPreviewVisibility(boolean shown) { - mScroller.setPreviewVisibility(shown); + fun setPreviewVisibility(shown: Boolean) { + mScroller!!.setPreviewVisibility(shown) } /** * @param value int to set the text size of the preview box */ - public void setPreviewTextSize(int value) { - mScroller.setPreviewTextSize(value); + fun setPreviewTextSize(value: Int) { + mScroller!!.setPreviewTextSize(value) } /** * @param color The color for the preview box */ - public void setPreviewColor(@ColorRes int color) { - mScroller.setPreviewColor(ContextCompat.getColor(getContext(), color)); + fun setPreviewColor(@ColorRes color: Int) { + mScroller!!.setPreviewColor(ContextCompat.getColor(context, color)) } /** * @param color The color for the preview box */ - public void setPreviewColor(String color) { - mScroller.setPreviewColor(Color.parseColor(color)); + fun setPreviewColor(color: String?) { + mScroller!!.setPreviewColor(Color.parseColor(color)) } /** * @param color The text color for the preview box */ - public void setPreviewTextColor(@ColorRes int color) { - mScroller.setPreviewTextColor(ContextCompat.getColor(getContext(), color)); + fun setPreviewTextColor(@ColorRes color: Int) { + mScroller!!.setPreviewTextColor(ContextCompat.getColor(context, color)) } /** * @param value float to set the transparency value of the preview box */ - public void setPreviewTransparentValue(float value) { - mScroller.setPreviewTransparentValue(value); + fun setPreviewTransparentValue(value: Float) { + mScroller!!.setPreviewTransparentValue(value) } /** * @param color The text color for the preview box */ - public void setPreviewTextColor(String color) { - mScroller.setPreviewTextColor(Color.parseColor(color)); + fun setPreviewTextColor(color: String?) { + mScroller!!.setPreviewTextColor(Color.parseColor(color)) } /** * @param color The color for the index bar */ - public void setIndexBarColor(String color) { - mScroller.setIndexBarColor(Color.parseColor(color)); + fun setIndexBarColor(color: String?) { + mScroller!!.setIndexBarColor(Color.parseColor(color)) } /** * @param color The color for the index bar */ - public void setIndexBarColor(@ColorRes int color) { - mScroller.setIndexBarColor(ContextCompat.getColor(getContext(), color)); + fun setIndexBarColor(@ColorRes color: Int) { + mScroller!!.setIndexBarColor(ContextCompat.getColor(context, color)) } - /** * @param color The text color for the index bar */ - public void setIndexBarTextColor(String color) { - mScroller.setIndexBarTextColor(Color.parseColor(color)); + fun setIndexBarTextColor(color: String?) { + mScroller!!.setIndexBarTextColor(Color.parseColor(color)) } /** * @param color The text color for the index bar */ - public void setIndexBarTextColor(@ColorRes int color) { - mScroller.setIndexBarTextColor(ContextCompat.getColor(getContext(), color)); + fun setIndexBarTextColor(@ColorRes color: Int) { + mScroller!!.setIndexBarTextColor(ContextCompat.getColor(context, color)) } /** * @param color The text color for the index bar */ - public void setIndexbarHighLightTextColor(String color) { - mScroller.setIndexbarHighLightTextColor(Color.parseColor(color)); + fun setIndexbarHighLightTextColor(color: String?) { + mScroller!!.setIndexbarHighLightTextColor(Color.parseColor(color)) } /** * @param color The text color for the index bar */ - public void setIndexbarHighLightTextColor(@ColorRes int color) { - mScroller.setIndexbarHighLightTextColor(ContextCompat.getColor(getContext(), color)); + fun setIndexbarHighLightTextColor(@ColorRes color: Int) { + mScroller!!.setIndexbarHighLightTextColor(ContextCompat.getColor(context, color)) } /** * @param shown boolean to show or hide the index bar */ - public void setIndexBarHighLightTextVisibility(boolean shown) { - mScroller.setIndexBarHighLightTextVisibility(shown); + fun setIndexBarHighLightTextVisibility(shown: Boolean) { + mScroller!!.setIndexBarHighLightTextVisibility(shown) } - public void updateSections() { - mScroller.updateSections(); + fun updateSections() { + mScroller!!.updateSections() } -} +} \ No newline at end of file diff --git a/app/src/main/java/in/myinnos/indexfastscrollrecycler/MainActivity.kt b/app/src/main/java/in/myinnos/indexfastscrollrecycler/MainActivity.kt index 40105c9..f943eb6 100644 --- a/app/src/main/java/in/myinnos/indexfastscrollrecycler/MainActivity.kt +++ b/app/src/main/java/in/myinnos/indexfastscrollrecycler/MainActivity.kt @@ -1,98 +1,72 @@ -package in.myinnos.indexfastscrollrecycler; +package `in`.myinnos.indexfastscrollrecycler -import android.os.Bundle; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; +import `in`.myinnos.alphabetsindexfastscrollrecycler.IndexFastScrollRecyclerView +import `in`.myinnos.indexfastscrollrecycler.adapter.RecyclerViewAdapter +import `in`.myinnos.indexfastscrollrecycler.helper.DataHelper +import `in`.myinnos.indexfastscrollrecycler.utility.AlphabetItem +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import java.util.* -import androidx.appcompat.app.AppCompatActivity; -import androidx.recyclerview.widget.LinearLayoutManager; +class MainActivity : AppCompatActivity() { -import butterknife.BindView; -import butterknife.ButterKnife; -import in.myinnos.alphabetsindexfastscrollrecycler.IndexFastScrollRecyclerView; -import in.myinnos.indexfastscrollrecycler.adapter.RecyclerViewAdapter; -import in.myinnos.indexfastscrollrecycler.helper.DataHelper; -import in.myinnos.indexfastscrollrecycler.helper.NotFullDataHelper; -import in.myinnos.indexfastscrollrecycler.utility.AlphabetItem; + var mRecyclerView: IndexFastScrollRecyclerView? = null -public class MainActivity extends AppCompatActivity { + private val mAlphabetItems by lazy { ArrayList() } - //@BindView(R.id.fast_scroller_recycler) - IndexFastScrollRecyclerView mRecyclerView; + // getAlphabetFullData() - full data, getAlphabetNotFullData() - not full data + private val mDataArray by lazy { DataHelper.getAlphabetFullData() } - private List mDataArray; - private List mAlphabetItems; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - //ButterKnife.bind(this); - - mRecyclerView = findViewById(R.id.fast_scroller_recycler); - - initialiseData(); - initialiseUI(); + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + mRecyclerView = findViewById(R.id.fast_scroller_recycler) + initialiseData() + initialiseUI() } - protected void initialiseData() { - - //Recycler view full data - //mDataArray = DataHelper.getAlphabetData(); - - //Recycler view not full data - mDataArray = NotFullDataHelper.getAlphabetNotFullData(); - - - // 123 + private fun initialiseData() { //Alphabet fast scroller data - mAlphabetItems = new ArrayList<>(); - List strAlphabets = new ArrayList<>(); - for (int i = 0; i < mDataArray.size(); i++) { - String name = mDataArray.get(i); - if (name == null || name.trim().isEmpty()) - continue; - - String word = name.substring(0, 1); + val strAlphabets: MutableList = ArrayList() + for (i in mDataArray.indices) { + val name = mDataArray[i] + if (name.trim { it <= ' ' }.isEmpty()) continue + val word = name.substring(0, 1) if (!strAlphabets.contains(word)) { - strAlphabets.add(word); - mAlphabetItems.add(new AlphabetItem(i, word, false)); + strAlphabets.add(word) + mAlphabetItems.add(AlphabetItem(i, word, false)) } } } - protected void initialiseUI() { - mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); - mRecyclerView.setAdapter(new RecyclerViewAdapter(mDataArray)); - - mRecyclerView.setIndexTextSize(12); - mRecyclerView.setIndexBarColor("#33334c"); - mRecyclerView.setIndexBarCornerRadius(0); - mRecyclerView.setIndexBarTransparentValue((float) 0.4); - - mRecyclerView.setIndexbarTopMargin(60); - mRecyclerView.setIndexbarBottomMargin(100); - mRecyclerView.setIndexbarHorizontalMargin(20); - - mRecyclerView.setPreviewPadding(0); - mRecyclerView.setIndexBarTextColor("#FFFFFF"); - - mRecyclerView.setPreviewTextSize(60); - mRecyclerView.setPreviewColor("#33334c"); - mRecyclerView.setPreviewTextColor("#FFFFFF"); - mRecyclerView.setPreviewTransparentValue(0.6f); - - mRecyclerView.setIndexBarVisibility(true); - - mRecyclerView.setIndexBarStrokeVisibility(true); - mRecyclerView.setIndexBarStrokeWidth(1); - mRecyclerView.setIndexBarStrokeColor("#000000"); - - mRecyclerView.setIndexbarHighLightTextColor("#33334c"); - mRecyclerView.setIndexBarHighLightTextVisibility(true); - - Objects.requireNonNull(mRecyclerView.getLayoutManager()).scrollToPosition(0); + private fun initialiseUI() { + mRecyclerView?.apply { + layoutManager = LinearLayoutManager(this@MainActivity) + adapter = RecyclerViewAdapter(mDataArray) + setIndexTextSize(12) + setIndexBarColor("#33334c") + setIndexBarCornerRadius(0) + setIndexBarTransparentValue(0.4.toFloat()) + setIndexbarTopMargin(60f) + setIndexbarBottomMargin(100f) + setIndexbarHorizontalMargin(20f) + setPreviewPadding(0) + setIndexBarTextColor("#FFFFFF") + setPreviewTextSize(60) + setPreviewColor("#33334c") + setPreviewTextColor("#FFFFFF") + setPreviewTransparentValue(0.6f) + setIndexBarVisibility(true) + setIndexBarStrokeVisibility(true) + setIndexBarStrokeWidth(1) + setIndexBarStrokeColor("#000000") + setIndexbarHighLightTextColor("#33334c") + setIndexBarHighLightTextVisibility(true) + } + Objects.requireNonNull(mRecyclerView?.layoutManager) + .scrollToPosition(0) } } \ No newline at end of file diff --git a/app/src/main/java/in/myinnos/indexfastscrollrecycler/adapter/RecyclerViewAdapter.kt b/app/src/main/java/in/myinnos/indexfastscrollrecycler/adapter/RecyclerViewAdapter.kt index ed824f4..12e8149 100644 --- a/app/src/main/java/in/myinnos/indexfastscrollrecycler/adapter/RecyclerViewAdapter.kt +++ b/app/src/main/java/in/myinnos/indexfastscrollrecycler/adapter/RecyclerViewAdapter.kt @@ -1,73 +1,51 @@ -package in.myinnos.indexfastscrollrecycler.adapter; +package `in`.myinnos.indexfastscrollrecycler.adapter + +import `in`.myinnos.indexfastscrollrecycler.Helpers.Companion.sectionsHelper +import `in`.myinnos.indexfastscrollrecycler.R +import android.widget.SectionIndexer +import android.view.ViewGroup +import android.view.LayoutInflater +import android.view.View +import android.widget.TextView +import android.widget.ImageButton +import androidx.recyclerview.widget.RecyclerView +import java.util.* +import kotlin.collections.ArrayList /** * Created by MyInnos on 01-02-2017. */ +class RecyclerViewAdapter(private val mDataArray: ArrayList?) : + RecyclerView.Adapter(), SectionIndexer { -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageButton; -import android.widget.SectionIndexer; -import android.widget.TextView; + private val mSections = "ABCDEFGHIJKLMNOPQRSTUVWXYZ#" + private var sectionsTranslator = HashMap() + private var mSectionPositions: ArrayList? = null -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -import androidx.recyclerview.widget.RecyclerView; -import butterknife.BindView; -import butterknife.ButterKnife; -import in.myinnos.indexfastscrollrecycler.Helpers; -import in.myinnos.indexfastscrollrecycler.R; - -public class RecyclerViewAdapter extends RecyclerView.Adapter - implements SectionIndexer { - - private List mDataArray; - - private String mSections = "ABCDEFGHIJKLMNOPQRSTUVWXYZ#"; - private HashMap sectionsTranslator = new HashMap<>(); - private ArrayList mSectionPositions; - - public RecyclerViewAdapter(List dataset) { - mDataArray = dataset; + override fun getItemCount(): Int { + return mDataArray?.size ?: 0 } - @Override - public int getItemCount() { - if (mDataArray == null) - return 0; - return mDataArray.size(); + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + val v: View = LayoutInflater.from(parent.context) + .inflate(R.layout.item_recycler_view_layout, parent, false) + return ViewHolder(v) } - @Override - public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - View v = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.item_recycler_view_layout, parent, false); - return new ViewHolder(v); - } - - @Override - public void onBindViewHolder(final ViewHolder holder, int position) { - holder.mTextView.setText(mDataArray.get(position)); - holder.mImageButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - mDataArray.remove(holder.getAdapterPosition()); - notifyDataSetChanged(); - } - }); + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + holder.mTextView.text = mDataArray!![position] + holder.mImageButton.setOnClickListener { + mDataArray.removeAt(holder.adapterPosition) + notifyDataSetChanged() + } } - @Override - public int getSectionForPosition(int position) { - return 0; + override fun getSectionForPosition(position: Int): Int { + return 0 } - @Override - public Object[] getSections() { - /* List sections = new ArrayList<>(26); + override fun getSections(): Array { + /* List sections = new ArrayList<>(26); mSectionPositions = new ArrayList<>(26); for (int i = 0, size = mDataArray.size(); i < size; i++) { String section = String.valueOf(mDataArray.get(i).charAt(0)).toUpperCase(); @@ -76,46 +54,39 @@ public class RecyclerViewAdapter extends RecyclerView.Adapter sections = new ArrayList<>(27); - ArrayList alphabetFull = new ArrayList<>(); - - mSectionPositions = new ArrayList<>(); - for (int i = 0, size = mDataArray.size(); i < size; i++) { - String section = String.valueOf(mDataArray.get(i).charAt(0)).toUpperCase(); - if (!sections.contains(section)) { - sections.add(section); - mSectionPositions.add(i); + val sections: MutableList = ArrayList(27) + val alphabetFull = ArrayList() + mSectionPositions = ArrayList() + run { + var i = 0 + val size = mDataArray!!.size + while (i < size) { + val section = mDataArray[i][0].toString().uppercase(Locale.getDefault()) + if (!sections.contains(section)) { + sections.add(section) + mSectionPositions?.add(i) + } + i++ } } - for (int i = 0; i < mSections.length(); i++) { - alphabetFull.add(String.valueOf(mSections.charAt(i))); + for (element in mSections) { + alphabetFull.add(element.toString()) } - - sectionsTranslator = Helpers.Companion.sectionsHelper(sections, alphabetFull); - - - - return alphabetFull.toArray(new String[0]); + sectionsTranslator = sectionsHelper(sections, alphabetFull) + return alphabetFull.toTypedArray() } - @Override - public int getPositionForSection(int sectionIndex) { - return mSectionPositions.get(sectionsTranslator.get(sectionIndex)); + override fun getPositionForSection(sectionIndex: Int): Int { + return mSectionPositions!![sectionsTranslator[sectionIndex]!!] } - static class ViewHolder extends RecyclerView.ViewHolder { - //@BindView(R.id.tv_alphabet) - TextView mTextView; - //@BindView(R.id.ib_alphabet) - ImageButton mImageButton; + class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + var mTextView: TextView + var mImageButton: ImageButton - ViewHolder(View itemView) { - super(itemView); - //ButterKnife.bind(this, itemView); - mTextView = itemView.findViewById(R.id.tv_alphabet); - mImageButton = itemView.findViewById(R.id.ib_alphabet); + init { + mTextView = itemView.findViewById(R.id.tv_alphabet) + mImageButton = itemView.findViewById(R.id.ib_alphabet) } } - } \ No newline at end of file diff --git a/app/src/main/java/in/myinnos/indexfastscrollrecycler/helper/DataHelper.kt b/app/src/main/java/in/myinnos/indexfastscrollrecycler/helper/DataHelper.kt index 0a10b8d..d96bff6 100644 --- a/app/src/main/java/in/myinnos/indexfastscrollrecycler/helper/DataHelper.kt +++ b/app/src/main/java/in/myinnos/indexfastscrollrecycler/helper/DataHelper.kt @@ -1,172 +1,223 @@ -package in.myinnos.indexfastscrollrecycler.helper; +package `in`.myinnos.indexfastscrollrecycler.helper + +import java.util.ArrayList /** * Created by MyInnos on 01-02-2017. */ - -import java.util.ArrayList; -import java.util.List; - -public class DataHelper { - - public static List getAlphabetData() { - return new ArrayList() {{ - add("A item 1"); - add("A item 2"); - add("A item 3"); - add("A item 4"); - add("A item 5"); - - add("B item 1"); - add("B item 2"); - add("B item 3"); - add("B item 4"); - add("B item 5"); - - add("C item 1"); - add("C item 2"); - add("C item 3"); - add("C item 4"); - add("C item 5"); - - add("D item 1"); - add("D item 2"); - add("D item 3"); - add("D item 4"); - add("D item 5"); - - add("E item 1"); - add("E item 2"); - add("E item 3"); - add("E item 4"); - add("E item 5"); - - add("F item 1"); - add("F item 2"); - add("F item 3"); - add("F item 4"); - add("F item 5"); - - add("G item 1"); - add("G item 2"); - add("G item 3"); - add("G item 4"); - add("G item 5"); - - add("H item 1"); - add("H item 2"); - add("H item 3"); - add("H item 4"); - add("H item 5"); - - add("I item 1"); - add("I item 2"); - add("I item 3"); - add("I item 4"); - add("I item 5"); - - add("J item 1"); - add("J item 2"); - add("J item 3"); - add("J item 4"); - add("J item 5"); - - add("K item 1"); - add("K item 2"); - add("K item 3"); - add("K item 4"); - add("K item 5"); - - add("L item 1"); - add("L item 2"); - add("L item 3"); - add("L item 4"); - add("L item 5"); - - add("M item 1"); - add("M item 2"); - add("M item 3"); - add("M item 4"); - add("M item 5"); - - add("N item 1"); - add("N item 2"); - add("N item 3"); - add("N item 4"); - add("N item 5"); - - add("O item 1"); - add("O item 2"); - add("O item 3"); - add("O item 4"); - add("O item 5"); - - add("P item 1"); - add("P item 2"); - add("P item 3"); - add("P item 4"); - add("P item 5"); - - add("Q item 1"); - add("Q item 2"); - add("Q item 3"); - add("Q item 4"); - add("Q item 5"); - - add("R item 1"); - add("R item 2"); - add("R item 3"); - add("R item 4"); - add("R item 5"); - - add("S item 1"); - add("S item 2"); - add("S item 3"); - add("S item 4"); - add("S item 5"); - - add("T item 1"); - add("T item 2"); - add("T item 3"); - add("T item 4"); - add("T item 5"); - - add("U item 1"); - add("U item 2"); - add("U item 3"); - add("U item 4"); - add("U item 5"); - - add("V item 1"); - add("V item 2"); - add("V item 3"); - add("V item 4"); - add("V item 5"); - - add("W item 1"); - add("W item 2"); - add("W item 3"); - add("W item 4"); - add("W item 5"); - - add("X item 1"); - add("X item 2"); - add("X item 3"); - add("X item 4"); - add("X item 5"); - - add("Y item 1"); - add("Y item 2"); - add("Y item 3"); - add("Y item 4"); - add("Y item 5"); - - add("Z item 1"); - add("Z item 2"); - add("Z item 3"); - add("Z item 4"); - add("Z item 5"); - - }}; +object DataHelper { + fun getAlphabetFullData(): ArrayList { + return ArrayList().apply { + add("A item 1") + add("A item 2") + add("A item 3") + add("A item 4") + add("A item 5") + add("B item 1") + add("B item 2") + add("B item 3") + add("B item 4") + add("B item 5") + add("C item 1") + add("C item 2") + add("C item 3") + add("C item 4") + add("C item 5") + add("D item 1") + add("D item 2") + add("D item 3") + add("D item 4") + add("D item 5") + add("E item 1") + add("E item 2") + add("E item 3") + add("E item 4") + add("E item 5") + add("F item 1") + add("F item 2") + add("F item 3") + add("F item 4") + add("F item 5") + add("G item 1") + add("G item 2") + add("G item 3") + add("G item 4") + add("G item 5") + add("H item 1") + add("H item 2") + add("H item 3") + add("H item 4") + add("H item 5") + add("I item 1") + add("I item 2") + add("I item 3") + add("I item 4") + add("I item 5") + add("J item 1") + add("J item 2") + add("J item 3") + add("J item 4") + add("J item 5") + add("K item 1") + add("K item 2") + add("K item 3") + add("K item 4") + add("K item 5") + add("L item 1") + add("L item 2") + add("L item 3") + add("L item 4") + add("L item 5") + add("M item 1") + add("M item 2") + add("M item 3") + add("M item 4") + add("M item 5") + add("N item 1") + add("N item 2") + add("N item 3") + add("N item 4") + add("N item 5") + add("O item 1") + add("O item 2") + add("O item 3") + add("O item 4") + add("O item 5") + add("P item 1") + add("P item 2") + add("P item 3") + add("P item 4") + add("P item 5") + add("Q item 1") + add("Q item 2") + add("Q item 3") + add("Q item 4") + add("Q item 5") + add("R item 1") + add("R item 2") + add("R item 3") + add("R item 4") + add("R item 5") + add("S item 1") + add("S item 2") + add("S item 3") + add("S item 4") + add("S item 5") + add("T item 1") + add("T item 2") + add("T item 3") + add("T item 4") + add("T item 5") + add("U item 1") + add("U item 2") + add("U item 3") + add("U item 4") + add("U item 5") + add("V item 1") + add("V item 2") + add("V item 3") + add("V item 4") + add("V item 5") + add("W item 1") + add("W item 2") + add("W item 3") + add("W item 4") + add("W item 5") + add("X item 1") + add("X item 2") + add("X item 3") + add("X item 4") + add("X item 5") + add("Y item 1") + add("Y item 2") + add("Y item 3") + add("Y item 4") + add("Y item 5") + add("Z item 1") + add("Z item 2") + add("Z item 3") + add("Z item 4") + add("Z item 5") + } + } + + fun getAlphabetNotFullData(): ArrayList { + return ArrayList().apply { + add("A item 1") + add("A item 2") + add("A item 3") + add("A item 4") + add("A item 5") + add("B item 1") + add("B item 2") + add("B item 3") + add("B item 4") + add("B item 5") + add("C item 1") + add("C item 2") + add("C item 3") + add("C item 4") + add("C item 5") + add("D item 1") + add("D item 2") + add("D item 3") + add("D item 4") + add("D item 5") + add("E item 1") + add("E item 2") + add("E item 3") + add("E item 4") + add("E item 5") + add("F item 1") + add("F item 2") + add("F item 3") + add("F item 4") + add("F item 5") + add("G item 1") + add("G item 2") + add("G item 3") + add("G item 4") + add("G item 5") + add("H item 1") + add("H item 2") + add("H item 3") + add("H item 4") + add("H item 5") + add("I item 1") + add("I item 2") + add("I item 3") + add("I item 4") + add("I item 5") + add("J item 1") + add("J item 2") + add("J item 3") + add("J item 4") + add("J item 5") + add("K item 1") + add("K item 2") + add("K item 3") + add("K item 4") + add("K item 5") + add("L item 1") + add("L item 2") + add("L item 3") + add("L item 4") + add("L item 5") + add("M item 1") + add("M item 2") + add("M item 3") + add("M item 4") + add("M item 5") + add("N item 1") + add("N item 2") + add("N item 3") + add("N item 4") + add("N item 5") + add("Z item 1") + add("Z item 2") + add("Z item 3") + add("Z item 4") + add("Z item 5") + } } } \ No newline at end of file diff --git a/app/src/main/java/in/myinnos/indexfastscrollrecycler/helper/NotFullDataHelper.java b/app/src/main/java/in/myinnos/indexfastscrollrecycler/helper/NotFullDataHelper.java deleted file mode 100644 index 4bcf338..0000000 --- a/app/src/main/java/in/myinnos/indexfastscrollrecycler/helper/NotFullDataHelper.java +++ /dev/null @@ -1,102 +0,0 @@ -package in.myinnos.indexfastscrollrecycler.helper; - -import java.util.ArrayList; -import java.util.List; - -public class NotFullDataHelper { - public static List getAlphabetNotFullData() { - return new ArrayList() {{ - add("A item 1"); - add("A item 2"); - add("A item 3"); - add("A item 4"); - add("A item 5"); - - add("B item 1"); - add("B item 2"); - add("B item 3"); - add("B item 4"); - add("B item 5"); - - add("C item 1"); - add("C item 2"); - add("C item 3"); - add("C item 4"); - add("C item 5"); - - add("D item 1"); - add("D item 2"); - add("D item 3"); - add("D item 4"); - add("D item 5"); - - add("E item 1"); - add("E item 2"); - add("E item 3"); - add("E item 4"); - add("E item 5"); - - add("F item 1"); - add("F item 2"); - add("F item 3"); - add("F item 4"); - add("F item 5"); - - add("G item 1"); - add("G item 2"); - add("G item 3"); - add("G item 4"); - add("G item 5"); - - add("H item 1"); - add("H item 2"); - add("H item 3"); - add("H item 4"); - add("H item 5"); - - add("I item 1"); - add("I item 2"); - add("I item 3"); - add("I item 4"); - add("I item 5"); - - add("J item 1"); - add("J item 2"); - add("J item 3"); - add("J item 4"); - add("J item 5"); - - add("K item 1"); - add("K item 2"); - add("K item 3"); - add("K item 4"); - add("K item 5"); - - add("L item 1"); - add("L item 2"); - add("L item 3"); - add("L item 4"); - add("L item 5"); - - add("M item 1"); - add("M item 2"); - add("M item 3"); - add("M item 4"); - add("M item 5"); - - add("N item 1"); - add("N item 2"); - add("N item 3"); - add("N item 4"); - add("N item 5"); - - add("Z item 1"); - add("Z item 2"); - add("Z item 3"); - add("Z item 4"); - add("Z item 5"); - - }}; - } -} - diff --git a/app/src/main/java/in/myinnos/indexfastscrollrecycler/utility/AlphabetItem.kt b/app/src/main/java/in/myinnos/indexfastscrollrecycler/utility/AlphabetItem.kt index 871f501..b0461a1 100644 --- a/app/src/main/java/in/myinnos/indexfastscrollrecycler/utility/AlphabetItem.kt +++ b/app/src/main/java/in/myinnos/indexfastscrollrecycler/utility/AlphabetItem.kt @@ -1,18 +1,6 @@ -package in.myinnos.indexfastscrollrecycler.utility; +package `in`.myinnos.indexfastscrollrecycler.utility /** * Created by MyInnos on 01-02-2017. */ - -public class AlphabetItem { - - public int position; - public String word; - public boolean isActive; - - public AlphabetItem(int pos, String word, boolean isActive) { - this.position = pos; - this.word = word; - this.isActive = isActive; - } -} \ No newline at end of file +class AlphabetItem(var position: Int, var word: String, var isActive: Boolean) \ No newline at end of file From 8bcd5bbd3f798a05affe4b0aa5a1fac230d2db2b Mon Sep 17 00:00:00 2001 From: Ivan Abakumov Date: Thu, 20 Apr 2023 13:16:15 +0300 Subject: [PATCH 5/5] Fixed small issues --- README.md | 14 +- .../IndexFastScrollRecyclerSection.kt | 96 +++++------ .../IndexFastScrollRecyclerView.kt | 154 +++++++++--------- .../indexfastscrollrecycler/MainActivity.kt | 18 +- app/src/main/res/values/colors.xml | 7 + 5 files changed, 145 insertions(+), 144 deletions(-) diff --git a/README.md b/README.md index 500f5c6..5e51809 100644 --- a/README.md +++ b/README.md @@ -103,13 +103,13 @@ Features ```java mRecyclerView.setIndexBarTransparentValue((float) 0.4); ``` -- Change IndexbarMargin: +- Change IndexBarMargin: ```java - mRecyclerView.setIndexbarMargin(4); + mRecyclerView.setIndexBarMargin(4); ``` -- Change IndexbarWidth: +- Change IndexBarWidth: ```java - mRecyclerView.setIndexbarWidth(40); + mRecyclerView.setIndexBarWidth(40); ``` - Change PreviewPadding: ```java @@ -130,9 +130,9 @@ mRecyclerView.setIndexBarVisibility(true); ``` - Change IndexBarHighLateTextColor: ```java -mRecyclerView.setIndexbarHighLightTextColor("#33334c); +mRecyclerView.setIndexBarHighLightTextColor("#33334c); -mRecyclerView.setIndexbarHighLightTextColor(R.color.index_bar_highlight_text_color); +mRecyclerView.setIndexBarHighLightTextColor(R.color.index_bar_highlight_text_color); ``` - To hide/show IndexBarHighLightText: ```java @@ -179,7 +179,7 @@ Thanks to all the people who contributed to AlphabetIndex Fast Scroll RecyclerVi [@RaphaelMarion](https://github.com/RaphaelMarion "RaphaelMarion") [@appspell](https://github.com/appspell "appspell") [@Libernys](https://github.com/Libernys "Libernys") -[@IvanAbacumov](https://github.com/IvanAbacumov "IvanAbacumov") +[@IvanAbacumov](https://github.com/IvanAbacumov "IvanAbakumov") [@benniebotha](https://github.com/benniebotha "benniebotha") [@AbandonedCart](https://github.com/AbandonedCart "AbandonedCart") [@Hamza417](https://github.com/Hamza417 "Hamza417") --> diff --git a/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerSection.kt b/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerSection.kt index de338ca..b1b8793 100644 --- a/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerSection.kt +++ b/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerSection.kt @@ -17,11 +17,11 @@ import java.lang.Exception context: Context, recyclerView: IndexFastScrollRecyclerView ) : RecyclerView.AdapterDataObserver() { - private var mIndexbarWidth: Float - private var mIndexbarMarginLeft: Float - private var mIndexbarMarginRight: Float - private var mIndexbarMarginTop: Float - private var mIndexbarMarginBottom: Float + private var mIndexBarWidth: Float + private var mIndexBarMarginLeft: Float + private var mIndexBarMarginRight: Float + private var mIndexBarMarginTop: Float + private var mIndexBarMarginBottom: Float private val mPreviewPadding: Float private val mDensity: Float private val mScaledDensity: Float @@ -124,7 +124,7 @@ import java.lang.Exception indexPaint.textSize = setIndexTextSize * mScaledDensity indexPaint.typeface = setTypeface val sectionHeight = - (mIndexbarRect!!.height() - mIndexbarMarginTop - mIndexbarMarginBottom) / mSections!!.size + (mIndexbarRect!!.height() - mIndexBarMarginTop - mIndexBarMarginBottom) / mSections!!.size val paddingTop = (sectionHeight - (indexPaint.descent() - indexPaint.ascent())) / 2 for (i in mSections!!.indices) { if (setSetIndexBarHighLightTextVisibility) { @@ -137,23 +137,23 @@ import java.lang.Exception indexPaint.textSize = setIndexTextSize * mScaledDensity indexPaint.color = indexbarTextColor } - val paddingLeft = (mIndexbarWidth - indexPaint.measureText( + val paddingLeft = (mIndexBarWidth - indexPaint.measureText( mSections!![i] )) / 2 canvas.drawText( mSections!![i], mIndexbarRect!!.left + paddingLeft, - mIndexbarRect!!.top + mIndexbarMarginTop + sectionHeight * i + paddingTop - indexPaint.ascent(), + mIndexbarRect!!.top + mIndexBarMarginTop + sectionHeight * i + paddingTop - indexPaint.ascent(), indexPaint ) } else { - val paddingLeft = (mIndexbarWidth - indexPaint.measureText( + val paddingLeft = (mIndexBarWidth - indexPaint.measureText( mSections!![i] )) / 2 canvas.drawText( mSections!![i], mIndexbarRect!!.left + paddingLeft, - mIndexbarRect!!.top + mIndexbarMarginTop + sectionHeight * i + paddingTop - indexPaint.ascent(), + mIndexbarRect!!.top + mIndexBarMarginTop + sectionHeight * i + paddingTop - indexPaint.ascent(), indexPaint ) } @@ -207,10 +207,10 @@ import java.lang.Exception mListViewWidth = w mListViewHeight = h mIndexbarRect = RectF( - w - mIndexbarMarginLeft - mIndexbarWidth, - mIndexbarMarginTop, - w - mIndexbarMarginRight, - h - mIndexbarMarginBottom - if (mRecyclerView!!.clipToPadding) 0 else mRecyclerView.paddingBottom + w - mIndexBarMarginLeft - mIndexBarWidth, + mIndexBarMarginTop, + w - mIndexBarMarginRight, + h - mIndexBarMarginBottom - if (mRecyclerView!!.clipToPadding) 0 else mRecyclerView.paddingBottom ) } @@ -238,8 +238,8 @@ import java.lang.Exception private fun getSectionByPoint(y: Float): Int { if (mSections == null || mSections?.isEmpty() == true) return 0 - if (y < mIndexbarRect!!.top + mIndexbarMarginTop) return 0 - return if (y >= mIndexbarRect!!.top + mIndexbarRect!!.height() - mIndexbarMarginTop) mSections!!.size - 1 else ((y - mIndexbarRect!!.top - mIndexbarMarginTop) / ((mIndexbarRect!!.height() - mIndexbarMarginBottom - mIndexbarMarginTop) / mSections!!.size)).toInt() + if (y < mIndexbarRect!!.top + mIndexBarMarginTop) return 0 + return if (y >= mIndexbarRect!!.top + mIndexbarRect!!.height() - mIndexBarMarginTop) mSections!!.size - 1 else ((y - mIndexbarRect!!.top - mIndexBarMarginTop) / ((mIndexbarRect!!.height() - mIndexBarMarginBottom - mIndexBarMarginTop) / mSections!!.size)).toInt() } private var mLastFadeRunnable: Runnable? = null @@ -267,48 +267,48 @@ import java.lang.Exception /** * @param value float to set the width of the index bar */ - fun setIndexbarWidth(value: Float) { - mIndexbarWidth = value + fun setIndexBarWidth(value: Float) { + mIndexBarWidth = value } /** * @param value float to set the margin of the index bar */ - fun setIndexbarMargin(value: Float) { - mIndexbarMarginLeft = value - mIndexbarMarginRight = value - mIndexbarMarginTop = value - mIndexbarMarginBottom = value + fun setIndexBarMargin(value: Float) { + mIndexBarMarginLeft = value + mIndexBarMarginRight = value + mIndexBarMarginTop = value + mIndexBarMarginBottom = value } /** * @param value float to set the top margin of the index bar */ - fun setIndexbarTopMargin(value: Float) { - mIndexbarMarginTop = value + fun setIndexBarTopMargin(value: Float) { + mIndexBarMarginTop = value } /** * @param value float to set the bottom margin of the index bar */ - fun setIndexbarBottomMargin(value: Float) { - mIndexbarMarginBottom = value + fun setIndexBarBottomMargin(value: Float) { + mIndexBarMarginBottom = value } /** * @param value float to set the left margin of the index bar */ - fun setIndexbarHorizontalMargin(value: Float) { - mIndexbarMarginLeft = value - mIndexbarMarginRight = value + fun setIndexBarHorizontalMargin(value: Float) { + mIndexBarMarginLeft = value + mIndexBarMarginRight = value } /** * @param value float to set the right margin of the index bar */ - fun setIndexbarVerticalMargin(value: Float) { - mIndexbarMarginTop = value - mIndexbarMarginBottom = value + fun setIndexBarVerticalMargin(value: Float) { + mIndexBarMarginTop = value + mIndexBarMarginBottom = value } /** @@ -419,7 +419,7 @@ import java.lang.Exception /** * @param color The text color for the index bar */ - fun setIndexbarHighLightTextColor(@ColorInt color: Int) { + fun setIndexBarHighLightTextColor(@ColorInt color: Int) { indexbarHighLightTextColor = color } @@ -432,23 +432,23 @@ import java.lang.Exception init { setIndexTextSize = recyclerView.setIndexTextSize - val setIndexbarWidth = recyclerView.mIndexbarWidth - val setIndexbarMarginLeft = recyclerView.mIndexbarMarginLeft - val setIndexbarMarginRight = recyclerView.mIndexbarMarginRight - val setIndexbarMarginTop = recyclerView.mIndexbarMarginTop - val setIndexbarMarginBottom = recyclerView.mIndexbarMarginBottom + val setIndexbarWidth = recyclerView.mIndexBarWidth + val setIndexbarMarginLeft = recyclerView.mIndexBarMarginLeft + val setIndexbarMarginRight = recyclerView.mIndexBarMarginRight + val setIndexbarMarginTop = recyclerView.mIndexBarMarginTop + val setIndexbarMarginBottom = recyclerView.mIndexBarMarginBottom setPreviewPadding = recyclerView.mPreviewPadding setPreviewTextSize = recyclerView.mPreviewTextSize - previewBackgroundColor = recyclerView.mPreviewBackgroudColor + previewBackgroundColor = recyclerView.mPreviewBackgroundColor previewTextColor = recyclerView.mPreviewTextColor previewBackgroudAlpha = convertTransparentValueToBackgroundAlpha(recyclerView.mPreviewTransparentValue) mIndexBarStrokeColor = recyclerView.mSetIndexBarStrokeColor mIndexBarStrokeWidth = recyclerView.mIndexBarStrokeWidth setIndexBarCornerRadius = recyclerView.mIndexBarCornerRadius - indexbarBackgroudColor = recyclerView.mIndexbarBackgroudColor - indexbarTextColor = recyclerView.mIndexbarTextColor - indexbarHighLightTextColor = recyclerView.mIndexbarHighLightTextColor + indexbarBackgroudColor = recyclerView.mIndexBarBackgroundColor + indexbarTextColor = recyclerView.mIndexBarTextColor + indexbarHighLightTextColor = recyclerView.mIndexBarHighLightTextColor indexbarBackgroudAlpha = convertTransparentValueToBackgroundAlpha(recyclerView.mIndexBarTransparentValue) mDensity = context.resources.displayMetrics.density @@ -456,11 +456,11 @@ import java.lang.Exception mRecyclerView = recyclerView mRecyclerView.setLayoutManager(LinearLayoutManager(context)) setAdapter(mRecyclerView.getAdapter()) - mIndexbarWidth = setIndexbarWidth * mDensity - mIndexbarMarginLeft = setIndexbarMarginLeft * mDensity - mIndexbarMarginRight = setIndexbarMarginRight * mDensity - mIndexbarMarginTop = setIndexbarMarginTop * mDensity - mIndexbarMarginBottom = setIndexbarMarginBottom * mDensity + mIndexBarWidth = setIndexbarWidth * mDensity + mIndexBarMarginLeft = setIndexbarMarginLeft * mDensity + mIndexBarMarginRight = setIndexbarMarginRight * mDensity + mIndexBarMarginTop = setIndexbarMarginTop * mDensity + mIndexBarMarginBottom = setIndexbarMarginBottom * mDensity mPreviewPadding = setPreviewPadding * mDensity } } \ No newline at end of file diff --git a/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerView.kt b/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerView.kt index c7099f9..cceaddb 100644 --- a/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerView.kt +++ b/alphabetsindexfastscrollrecycler/src/main/java/in/myinnos/alphabetsindexfastscrollrecycler/IndexFastScrollRecyclerView.kt @@ -25,11 +25,11 @@ import androidx.recyclerview.widget.RecyclerView private var mEnabled = true private var mTransient = false var setIndexTextSize = 12 - var mIndexbarWidth = 20f - var mIndexbarMarginLeft = 2f - var mIndexbarMarginRight = 2f - var mIndexbarMarginTop = 2f - var mIndexbarMarginBottom = 2f + var mIndexBarWidth = 20f + var mIndexBarMarginLeft = 2f + var mIndexBarMarginRight = 2f + var mIndexBarMarginTop = 2f + var mIndexBarMarginBottom = 2f var mPreviewPadding = 5 var mIndexBarCornerRadius = 5 var mIndexBarTransparentValue = 0.6.toFloat() @@ -39,18 +39,18 @@ import androidx.recyclerview.widget.RecyclerView var mSetIndexBarStrokeColor = Color.BLACK @ColorInt - var mIndexbarBackgroudColor = Color.BLACK + var mIndexBarBackgroundColor = Color.BLACK @ColorInt - var mIndexbarTextColor = Color.WHITE + var mIndexBarTextColor = Color.WHITE @ColorInt - var mIndexbarHighLightTextColor = Color.BLACK + var mIndexBarHighLightTextColor = Color.BLACK var mPreviewTextSize = 50 @ColorInt - var mPreviewBackgroudColor = Color.BLACK + var mPreviewBackgroundColor = Color.BLACK @ColorInt var mPreviewTextColor = Color.WHITE @@ -78,25 +78,25 @@ import androidx.recyclerview.widget.RecyclerView R.styleable.IndexFastScrollRecyclerView_setIndexTextSize, setIndexTextSize ) - mIndexbarWidth = typedArray.getFloat( + mIndexBarWidth = typedArray.getFloat( R.styleable.IndexFastScrollRecyclerView_setIndexbarWidth, - mIndexbarWidth + mIndexBarWidth ) - mIndexbarMarginLeft = typedArray.getFloat( + mIndexBarMarginLeft = typedArray.getFloat( R.styleable.IndexFastScrollRecyclerView_setIndexbarMargin, - mIndexbarMarginLeft + mIndexBarMarginLeft ) - mIndexbarMarginRight = typedArray.getFloat( + mIndexBarMarginRight = typedArray.getFloat( R.styleable.IndexFastScrollRecyclerView_setIndexbarMargin, - mIndexbarMarginRight + mIndexBarMarginRight ) - mIndexbarMarginTop = typedArray.getFloat( + mIndexBarMarginTop = typedArray.getFloat( R.styleable.IndexFastScrollRecyclerView_setIndexbarMargin, - mIndexbarMarginTop + mIndexBarMarginTop ) - mIndexbarMarginBottom = typedArray.getFloat( + mIndexBarMarginBottom = typedArray.getFloat( R.styleable.IndexFastScrollRecyclerView_setIndexbarMargin, - mIndexbarMarginBottom + mIndexBarMarginBottom ) mPreviewPadding = typedArray.getInt( R.styleable.IndexFastScrollRecyclerView_setPreviewPadding, @@ -131,12 +131,12 @@ import androidx.recyclerview.widget.RecyclerView R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, tv ) - mIndexbarBackgroudColor = if (tv.type == TypedValue.TYPE_STRING) { + mIndexBarBackgroundColor = if (tv.type == TypedValue.TYPE_STRING) { Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor)) } else { typedArray.getColor( R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, - mIndexbarBackgroudColor + mIndexBarBackgroundColor ) } } @@ -146,12 +146,12 @@ import androidx.recyclerview.widget.RecyclerView R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, tv ) - mIndexbarTextColor = if (tv.type == TypedValue.TYPE_STRING) { + mIndexBarTextColor = if (tv.type == TypedValue.TYPE_STRING) { Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarTextColor)) } else { typedArray.getColor( R.styleable.IndexFastScrollRecyclerView_setIndexBarTextColor, - mIndexbarTextColor + mIndexBarTextColor ) } } @@ -161,12 +161,12 @@ import androidx.recyclerview.widget.RecyclerView R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, tv ) - mIndexbarHighLightTextColor = if (tv.type == TypedValue.TYPE_STRING) { + mIndexBarHighLightTextColor = if (tv.type == TypedValue.TYPE_STRING) { Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarHighlightTextColor)) } else { typedArray.getColor( R.styleable.IndexFastScrollRecyclerView_setIndexBarHighlightTextColor, - mIndexbarHighLightTextColor + mIndexBarHighLightTextColor ) } } @@ -184,12 +184,12 @@ import androidx.recyclerview.widget.RecyclerView R.styleable.IndexFastScrollRecyclerView_setIndexBarColor, tv ) - mPreviewBackgroudColor = if (tv.type == TypedValue.TYPE_STRING) { + mPreviewBackgroundColor = if (tv.type == TypedValue.TYPE_STRING) { Color.parseColor(typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setPreviewColor)) } else { typedArray.getColor( R.styleable.IndexFastScrollRecyclerView_setPreviewColor, - mPreviewBackgroudColor + mPreviewBackgroundColor ) } } @@ -233,17 +233,16 @@ import androidx.recyclerview.widget.RecyclerView typedArray.recycle() } - // This line here is neccesary else the attributes won't be updated if a value is passed from XML + // This line here is necessary else the attributes won't be updated if a value is passed from XML mScroller = IndexFastScrollRecyclerSection(context, this) - mScroller!!.setIndexBarVisibility(mEnabled) + mScroller?.setIndexBarVisibility(mEnabled) } } override fun draw(canvas: Canvas) { super.draw(canvas) - // Overlay index bar - if (mScroller != null) mScroller!!.draw(canvas) + if (mScroller != null) mScroller?.draw(canvas) } @SuppressLint("ClickableViewAccessibility") @@ -261,19 +260,14 @@ import androidx.recyclerview.widget.RecyclerView } }) } - mGestureDetector!!.onTouchEvent(ev) + mGestureDetector?.onTouchEvent(ev) } return super.onTouchEvent(ev) } override fun onInterceptTouchEvent(ev: MotionEvent): Boolean { - return if (mEnabled && mScroller != null && mScroller!!.contains( - ev.x, - ev.y - ) - ) true else super.onInterceptTouchEvent( - ev - ) + return if (mEnabled && mScroller != null && mScroller!!.contains(ev.x, ev.y)) true + else super.onInterceptTouchEvent(ev) } override fun setAdapter(adapter: Adapter<*>?) { @@ -290,89 +284,89 @@ import androidx.recyclerview.widget.RecyclerView * @param value int to set the text size of the index bar */ fun setIndexTextSize(value: Int) { - mScroller!!.setIndexTextSize(value) + mScroller?.setIndexTextSize(value) } /** * @param value float to set the width of the index bar */ - fun setIndexbarWidth(value: Float) { - mScroller!!.setIndexbarWidth(value) + fun setIndexBarWidth(value: Float) { + mScroller?.setIndexBarWidth(value) } /** * @param value float to set the margin of the index bar */ - fun setIndexbarMargin(value: Float) { - mScroller!!.setIndexbarMargin(value) + fun setIndexBarMargin(value: Float) { + mScroller?.setIndexBarMargin(value) } /** * @param value float to set the top margin of the index bar */ - fun setIndexbarTopMargin(value: Float) { - mScroller!!.setIndexbarTopMargin(value) + fun setIndexBarTopMargin(value: Float) { + mScroller?.setIndexBarTopMargin(value) } /** * @param value float to set the bottom margin of the index bar */ - fun setIndexbarBottomMargin(value: Float) { - mScroller!!.setIndexbarBottomMargin(value) + fun setIndexBarBottomMargin(value: Float) { + mScroller?.setIndexBarBottomMargin(value) } /** * @param value float to set the Horizontal margin of the index bar */ - fun setIndexbarHorizontalMargin(value: Float) { - mScroller!!.setIndexbarHorizontalMargin(value) + fun setIndexBarHorizontalMargin(value: Float) { + mScroller?.setIndexBarHorizontalMargin(value) } /** * @param value float to set the Vertical margin of the index bar */ - fun setIndexbarVerticalMargin(value: Float) { - mScroller!!.setIndexbarVerticalMargin(value) + fun setIndexBarVerticalMargin(value: Float) { + mScroller?.setIndexBarVerticalMargin(value) } /** * @param value int to set the preview padding */ fun setPreviewPadding(value: Int) { - mScroller!!.setPreviewPadding(value) + mScroller?.setPreviewPadding(value) } /** * @param value int to set the corner radius of the index bar */ fun setIndexBarCornerRadius(value: Int) { - mScroller!!.setIndexBarCornerRadius(value) + mScroller?.setIndexBarCornerRadius(value) } /** * @param value float to set the transparency value of the index bar */ fun setIndexBarTransparentValue(value: Float) { - mScroller!!.setIndexBarTransparentValue(value) + mScroller?.setIndexBarTransparentValue(value) } /** * @param typeface Typeface to set the typeface of the preview & the index bar */ fun setTypeface(typeface: Typeface?) { - mScroller!!.setTypeface(typeface) + mScroller?.setTypeface(typeface) } /** * @param shown boolean to show or hide the index bar */ fun setIndexBarVisibility(shown: Boolean) { - mScroller!!.setIndexBarVisibility(shown) + mScroller?.setIndexBarVisibility(shown) mEnabled = shown } private val scrollRunnable = Runnable { - mScroller!!.setIndexBarVisibility(false) + mScroller?.setIndexBarVisibility(false) invalidate() } private val scrollListener: OnScrollListener = object : OnScrollListener() { @@ -380,7 +374,7 @@ import androidx.recyclerview.widget.RecyclerView super.onScrollStateChanged(recyclerView, newState) if (newState == SCROLL_STATE_DRAGGING) { recyclerView.removeCallbacks(scrollRunnable) - mScroller!!.setIndexBarVisibility(true) + mScroller?.setIndexBarVisibility(true) } else if (newState == SCROLL_STATE_IDLE) { recyclerView.postDelayed(scrollRunnable, 1000) } @@ -401,129 +395,129 @@ import androidx.recyclerview.widget.RecyclerView * @param shown boolean to show or hide the index bar */ fun setIndexBarStrokeVisibility(shown: Boolean) { - mScroller!!.setIndexBarStrokeVisibility(shown) + mScroller?.setIndexBarStrokeVisibility(shown) } /** * @param color The color for the index bar */ fun setIndexBarStrokeColor(color: String?) { - mScroller!!.setIndexBarStrokeColor(Color.parseColor(color)) + mScroller?.setIndexBarStrokeColor(Color.parseColor(color)) } /** * @param color The color for the preview box */ fun setIndexBarStrokeColor(@ColorRes color: Int) { - mScroller!!.setIndexBarStrokeColor(ContextCompat.getColor(context, color)) + mScroller?.setIndexBarStrokeColor(ContextCompat.getColor(context, color)) } /** * @param value int to set the text size of the preview box */ fun setIndexBarStrokeWidth(value: Int) { - mScroller!!.setIndexBarStrokeWidth(value) + mScroller?.setIndexBarStrokeWidth(value) } /** * @param shown boolean to show or hide the preview */ fun setPreviewVisibility(shown: Boolean) { - mScroller!!.setPreviewVisibility(shown) + mScroller?.setPreviewVisibility(shown) } /** * @param value int to set the text size of the preview box */ fun setPreviewTextSize(value: Int) { - mScroller!!.setPreviewTextSize(value) + mScroller?.setPreviewTextSize(value) } /** * @param color The color for the preview box */ fun setPreviewColor(@ColorRes color: Int) { - mScroller!!.setPreviewColor(ContextCompat.getColor(context, color)) + mScroller?.setPreviewColor(ContextCompat.getColor(context, color)) } /** * @param color The color for the preview box */ fun setPreviewColor(color: String?) { - mScroller!!.setPreviewColor(Color.parseColor(color)) + mScroller?.setPreviewColor(Color.parseColor(color)) } /** * @param color The text color for the preview box */ fun setPreviewTextColor(@ColorRes color: Int) { - mScroller!!.setPreviewTextColor(ContextCompat.getColor(context, color)) + mScroller?.setPreviewTextColor(ContextCompat.getColor(context, color)) } /** * @param value float to set the transparency value of the preview box */ fun setPreviewTransparentValue(value: Float) { - mScroller!!.setPreviewTransparentValue(value) + mScroller?.setPreviewTransparentValue(value) } /** * @param color The text color for the preview box */ fun setPreviewTextColor(color: String?) { - mScroller!!.setPreviewTextColor(Color.parseColor(color)) + mScroller?.setPreviewTextColor(Color.parseColor(color)) } /** * @param color The color for the index bar */ fun setIndexBarColor(color: String?) { - mScroller!!.setIndexBarColor(Color.parseColor(color)) + mScroller?.setIndexBarColor(Color.parseColor(color)) } /** * @param color The color for the index bar */ fun setIndexBarColor(@ColorRes color: Int) { - mScroller!!.setIndexBarColor(ContextCompat.getColor(context, color)) + mScroller?.setIndexBarColor(ContextCompat.getColor(context, color)) } /** * @param color The text color for the index bar */ fun setIndexBarTextColor(color: String?) { - mScroller!!.setIndexBarTextColor(Color.parseColor(color)) + mScroller?.setIndexBarTextColor(Color.parseColor(color)) } /** * @param color The text color for the index bar */ fun setIndexBarTextColor(@ColorRes color: Int) { - mScroller!!.setIndexBarTextColor(ContextCompat.getColor(context, color)) + mScroller?.setIndexBarTextColor(ContextCompat.getColor(context, color)) } /** * @param color The text color for the index bar */ - fun setIndexbarHighLightTextColor(color: String?) { - mScroller!!.setIndexbarHighLightTextColor(Color.parseColor(color)) + fun setIndexBarHighLightTextColor(color: String?) { + mScroller?.setIndexBarHighLightTextColor(Color.parseColor(color)) } /** * @param color The text color for the index bar */ - fun setIndexbarHighLightTextColor(@ColorRes color: Int) { - mScroller!!.setIndexbarHighLightTextColor(ContextCompat.getColor(context, color)) + fun setIndexBarHighLightTextColor(@ColorRes color: Int) { + mScroller?.setIndexBarHighLightTextColor(ContextCompat.getColor(context, color)) } /** * @param shown boolean to show or hide the index bar */ fun setIndexBarHighLightTextVisibility(shown: Boolean) { - mScroller!!.setIndexBarHighLightTextVisibility(shown) + mScroller?.setIndexBarHighLightTextVisibility(shown) } fun updateSections() { - mScroller!!.updateSections() + mScroller?.updateSections() } } \ No newline at end of file diff --git a/app/src/main/java/in/myinnos/indexfastscrollrecycler/MainActivity.kt b/app/src/main/java/in/myinnos/indexfastscrollrecycler/MainActivity.kt index f943eb6..17213b0 100644 --- a/app/src/main/java/in/myinnos/indexfastscrollrecycler/MainActivity.kt +++ b/app/src/main/java/in/myinnos/indexfastscrollrecycler/MainActivity.kt @@ -47,23 +47,23 @@ class MainActivity : AppCompatActivity() { layoutManager = LinearLayoutManager(this@MainActivity) adapter = RecyclerViewAdapter(mDataArray) setIndexTextSize(12) - setIndexBarColor("#33334c") + setIndexBarColor(R.color.color_setIndexBarColor) setIndexBarCornerRadius(0) setIndexBarTransparentValue(0.4.toFloat()) - setIndexbarTopMargin(60f) - setIndexbarBottomMargin(100f) - setIndexbarHorizontalMargin(20f) + setIndexBarTopMargin(60f) + setIndexBarBottomMargin(100f) + setIndexBarHorizontalMargin(20f) setPreviewPadding(0) - setIndexBarTextColor("#FFFFFF") + setIndexBarTextColor(R.color.color_setIndexBarTextColor) setPreviewTextSize(60) - setPreviewColor("#33334c") - setPreviewTextColor("#FFFFFF") + setPreviewColor(R.color.color_setPreviewColor) + setPreviewTextColor(R.color.color_setPreviewTextColor) setPreviewTransparentValue(0.6f) setIndexBarVisibility(true) setIndexBarStrokeVisibility(true) setIndexBarStrokeWidth(1) - setIndexBarStrokeColor("#000000") - setIndexbarHighLightTextColor("#33334c") + setIndexBarStrokeColor(R.color.color_setIndexBarStrokeColor) + setIndexBarHighLightTextColor(R.color.color_setIndexBarHighLightTextColor) setIndexBarHighLightTextVisibility(true) } Objects.requireNonNull(mRecyclerView?.layoutManager) diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 3ab3e9c..c1fe994 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,4 +3,11 @@ #3F51B5 #303F9F #FF4081 + + #33334c + #FFFFFF + #33334c + #FFFFFF + #000000 + #33334c