Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
* [r] Cleans up code
Browse files Browse the repository at this point in the history
* [M] Adds Horizontal scrolling support
* [M] Adds functionality to hide handle
* [m] Updates sample application to have a sample for horizontal scrolling
* [u] Updates libraries and gradle tools versioning
  • Loading branch information
shahsurajk committed Oct 19, 2020
1 parent 73db556 commit 5a235f3
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 85 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
org.gradle.jvmargs=-Xmx4608m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ import androidx.core.content.ContextCompat
import androidx.core.widget.TextViewCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.Runnable
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
Expand Down Expand Up @@ -73,10 +78,9 @@ import kotlin.math.roundToInt
* **Handle Size:** The fastScroller automatically adjusts the size of the handle, but if [RecyclerViewFastScroller.isFixedSizeHandle] is set as true handle [R.styleable.RecyclerViewFastScroller_handleHeight]
* and [R.styleable.RecyclerViewFastScroller_handleWidth] need to be provided else default value of 18dp will be taken for both.
*
* @version 1.0
* @version 2.0
* */

// todo@shahsurajk write for x direction
class RecyclerViewFastScroller @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {
Expand Down Expand Up @@ -124,8 +128,8 @@ class RecyclerViewFastScroller @JvmOverloads constructor(
val fastScrollDirection: FastScrollDirection = FastScrollDirection.VERTICAL
const val isFixedSizeHandle: Boolean = false
const val isFastScrollEnabled: Boolean = true
const val DEFAULT_ANIM_DURATION: Long = 100
const val DEFAULT_POPUP_VISIBILITY_DURATION = 200L
const val animationDuration: Long = 100
const val popupVisibilityDuration = 200L
const val hasEmptyItemDecorator: Boolean = true
const val handleVisibilityDuration: Int = 0
const val trackMargin: Int = 0
Expand Down Expand Up @@ -211,7 +215,10 @@ class RecyclerViewFastScroller @JvmOverloads constructor(
refreshHandleImageViewSize()
}

var handleVisibilityDuration: Int = 0
/**
* The duration for which the handle should remain visible
* */
var handleVisibilityDuration: Int = -1

// --- internal properties
private var popupPosition: PopupPosition = Defaults.popupPosition
Expand Down Expand Up @@ -330,7 +337,10 @@ class RecyclerViewFastScroller @JvmOverloads constructor(
?: ContextCompat.getDrawable(context, Defaults.handleDrawableInt))

handleVisibilityDuration =
attribs.getInt(R.styleable.RecyclerViewFastScroller_handleVisibilityDuration, Defaults.handleVisibilityDuration)
attribs.getInt(
R.styleable.RecyclerViewFastScroller_handleVisibilityDuration,
Defaults.handleVisibilityDuration
)

handleHeight =
attribs.getDimensionPixelSize(
Expand Down Expand Up @@ -472,7 +482,7 @@ class RecyclerViewFastScroller @JvmOverloads constructor(
// hide the popup with a default anim delay
handler.postDelayed(
popupAnimationRunnable,
Defaults.DEFAULT_POPUP_VISIBILITY_DURATION
Defaults.popupVisibilityDuration
)
}
super.onTouchEvent(motionEvent)
Expand Down Expand Up @@ -539,8 +549,8 @@ class RecyclerViewFastScroller @JvmOverloads constructor(
FastScrollDirection.VERTICAL -> {
handleImageView.setPadding(padding, 0, padding, 0)
popupTextView.layoutParams = LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
).also {
if (Build.VERSION.SDK_INT > 16)
it.addRule(ALIGN_END, R.id.trackView)
Expand Down Expand Up @@ -655,7 +665,7 @@ class RecyclerViewFastScroller @JvmOverloads constructor(
}

/**
* Custom animator extension, as [ViewPropertyAnimator] doesn't have individual listeners, decluttering, also present in android KTX
* Custom animator extension, as [ViewPropertyAnimator] doesn't have individual listeners, de-cluttering, also present in android KTX
* */
private inline fun ViewPropertyAnimator.onAnimationCancelled(crossinline body: () -> Unit) {
this.setListener(object : Animator.AnimatorListener {
Expand All @@ -682,13 +692,13 @@ class RecyclerViewFastScroller @JvmOverloads constructor(
private fun View.animateVisibility(makeVisible: Boolean = true) {

val scaleFactor: Float = if (makeVisible) 1f else 0f
this.animate().scaleX(scaleFactor).setDuration(Defaults.DEFAULT_ANIM_DURATION)
this.animate().scaleX(scaleFactor).setDuration(Defaults.animationDuration)
.onAnimationCancelled {
this.animate().scaleX(scaleFactor).duration = Defaults.DEFAULT_ANIM_DURATION
this.animate().scaleX(scaleFactor).duration = Defaults.animationDuration
}
this.animate().scaleY(scaleFactor).setDuration(Defaults.DEFAULT_ANIM_DURATION)
this.animate().scaleY(scaleFactor).setDuration(Defaults.animationDuration)
.onAnimationCancelled {
this.animate().scaleY(scaleFactor).duration = Defaults.DEFAULT_ANIM_DURATION
this.animate().scaleY(scaleFactor).duration = Defaults.animationDuration
}
}

Expand Down
6 changes: 3 additions & 3 deletions recyclerviewfastscroller/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
<attr name="handleHasFixedSize" format="boolean" />
<attr name="supportSwipeToRefresh" format="boolean" />

<attr name="trackMarginStart" format="dimension"/>
<attr name="trackMarginEnd" format="dimension"/>
<attr name="trackMarginStart" format="dimension" />
<attr name="trackMarginEnd" format="dimension" />

<attr name="handleVisibilityDuration" format="integer"/>
<attr name="handleVisibilityDuration" format="integer" />
</declare-styleable>
</resources>
2 changes: 2 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

vectorDrawables.useSupportLibrary = true

multiDexEnabled true // material design lib adds a lot of methods
}

buildTypes {
Expand Down
6 changes: 3 additions & 3 deletions sample/src/main/java/com/qtalk/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.fragment.app.FragmentPagerAdapter
import com.qtalk.sample.fragments.AdvancedFragment
import com.qtalk.sample.fragments.BasicFragment
import com.qtalk.sample.fragments.ContactsFragment
import com.qtalk.sample.fragments.ProgrammingLanguageFragment
import com.qtalk.sample.fragments.ProgrammingLanguagesFragment
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
Expand Down Expand Up @@ -37,7 +37,7 @@ class MainActivity : AppCompatActivity() {
return when (position) {
PAGE_INDEX_BASIC -> BasicFragment()
PAGE_INDEX_ADVANCE -> AdvancedFragment()
PAGE_INDEX_LANGUAGES -> ProgrammingLanguageFragment()
PAGE_INDEX_LANGUAGES -> ProgrammingLanguagesFragment()
PAGE_INDEX_CONTACTS -> ContactsFragment()
else -> throw IllegalArgumentException("Not expecting $position.")
}
Expand All @@ -52,7 +52,7 @@ class MainActivity : AppCompatActivity() {
when (position) {
PAGE_INDEX_BASIC -> return mContext?.resources?.getString(R.string.basic_fragment)
PAGE_INDEX_ADVANCE -> return mContext?.resources?.getString(R.string.advanced_fragment)
PAGE_INDEX_LANGUAGES -> return mContext?.resources?.getString(R.string.programming_language_fragment)
PAGE_INDEX_LANGUAGES -> return mContext?.resources?.getString(R.string.programming_languages_fragment)
PAGE_INDEX_CONTACTS -> return mContext?.resources?.getString(R.string.contacts_fragment)
}
return ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AdvancedFragment : Fragment() {
val adapter = AdvancedAdapter(
activity,
countriesList,
with(view.fast_scroller as RecyclerViewFastScroller) {
with(view.fastScroller as RecyclerViewFastScroller) {
handleDrawable
})

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.qtalk.sample.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller
import com.qtalk.sample.R
import com.qtalk.sample.adapters.ProgrammingLanguagesAdapter
import kotlinx.android.synthetic.main.fragment_programming_languages.view.*
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import java.util.Locale

class ProgrammingLanguagesFragment : Fragment() {

private val programmingLanguages: List<String> by lazy {
Json.decodeFromString<List<String>>(
requireActivity().assets.open("programming_languages.json").use {
it.reader().readText()
}
).sortedBy { it.toLowerCase(Locale.ROOT) }
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_programming_languages, container, false).apply {

recyclerView.layoutManager = LinearLayoutManager(
context,
LinearLayoutManager.HORIZONTAL,
false
)

fab.setOnClickListener {
with(fastScroller) {
val tmp = handleWidth
handleWidth = handleHeight
handleHeight = tmp

fastScrollDirection =
if (fastScrollDirection == RecyclerViewFastScroller.FastScrollDirection.HORIZONTAL) {
recyclerView.layoutManager =
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
RecyclerViewFastScroller.FastScrollDirection.VERTICAL
} else {
recyclerView.layoutManager =
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
RecyclerViewFastScroller.FastScrollDirection.HORIZONTAL
}
}
}
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
view.recyclerView.adapter = ProgrammingLanguagesAdapter(programmingLanguages)
}
}
2 changes: 1 addition & 1 deletion sample/src/main/res/layout/fragment_advanced.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
android:background="@color/tealLight" />

<com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller
android:id="@+id/fast_scroller"
android:id="@+id/fastScroller"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:handleHeight="32dp"
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/res/layout/fragment_basic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:layout_height="match_parent">

<com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller
android:id="@+id/fast_scroller"
android:id="@+id/fastScroller"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:handleHeight="32dp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller
android:id="@+id/fast_scroller"
android:id="@+id/fastScroller"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:fastScrollDirection="horizontal"
app:handleHeight="@dimen/default_handle_size"
app:handleWidth="32dp"
app:fastScrollDirection="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTextStyle="@style/ContactsPopupTextViewStyle">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/recycler_view_margin"
android:layout_marginRight="@dimen/recycler_view_margin"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
android:layout_marginRight="@dimen/recycler_view_margin" />

</com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller>

Expand All @@ -29,8 +29,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:srcCompat="@drawable/arrow_top_right_bottom_left"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
app:srcCompat="@drawable/arrow_top_right_bottom_left" />

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 1 addition & 1 deletion sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<string name="app_name">Sample</string>
<string name="basic_fragment">Basic</string>
<string name="advanced_fragment">Advanced</string>
<string name="programming_language_fragment">Programming Languages</string>
<string name="programming_languages_fragment">Programming Languages</string>
<string name="contacts_fragment">Contacts</string>
<string name="countries_by_population">Countries by population</string>
</resources>

0 comments on commit 5a235f3

Please sign in to comment.