Git Product home page Git Product logo

scrollingpagerindicator's Introduction

Maven Central

ScrollingPagerIndicator

Pager indicator inspired by Instagram. Lightweight and easy to set up.

Supports ViewPager, RecyclerView and attaching to custom pagers.

preview preview Custom drawables

Getting started

Add dependency to Gradle script:

implementation "ru.tinkoff.scrollingpagerindicator:scrollingpagerindicator:x.x.x"

Attach to ViewPager

  1. Ensure that you have ViewPager in dependencies:
implementation "androidx.viewpager:viewpager:x.x.x"

or

implementation "androidx.viewpager2:viewpager2:x.x.x"

or

implementation "androidx.recyclerview:recyclerview:x.x.x"
  1. Add view to layout:
<androidx.viewpager.widget.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator
    android:id="@+id/indicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

There is no possibility to set fixed indicator width (because it's width is based on spi_visibleDotCount).

  1. Attach indicator to ViewPager:
ViewPager pager = findViewById(R.id.pager);
pager.setAdapter(new DemoPagerAdapter());
ScaledDotsIndicator indicator = findViewById(R.id.indicator);
indicator.attachToPager(pager);

Attach to RecyclerView

  1. Ensure that you have RecyclerView in dependencies:
implementation "androidx.recyclerview:recyclerview:x.x.x"
  1. Add view to layout:
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator
    android:id="@+id/indicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  1. Attach indicator to RecyclerView:
RecyclerView recyclerView = findViewById(R.id.recycler);
LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
DemoRecyclerViewAdapter recyclerAdapter = new DemoRecyclerViewAdapter();
recyclerView.setAdapter(recyclerAdapter);

ScrollingPagerIndicator recyclerIndicator = findViewById(R.id.indicator);
recyclerIndicator.attachToRecyclerView(recyclerView);
  1. Determine which page is current in RecyclerView:

Use attachToRecyclerView(RecyclerView) if current page of the recycler is centered. Like this:

+------------------------------+
|---+  +----------------+  +---|
|   |  |     current    |  |   |
|   |  |      page      |  |   |
|---+  +----------------+  +---|
+------------------------------+

Use attachToRecyclerView(RecyclerView recyclerView, int currentPageLeftCornerX) if current page of the recycler isn't centered. Like this:

+-|----------------------------+
| +--------+  +--------+  +----+
| | current|  |        |  |    |
| |  page  |  |        |  |    |
| +--------+  +--------+  +----|
+-|----------------------------+
  |
  | currentPageLeftCornerX

In both cases all views in RecyclerView must have the same width. Only LinearLayoutManager is supported.

Attach to any custom pager

If you want to attach indicator to some custom pager, you have to implement ScrollingPagerIndicator.PagerAttacher interface. You can take look at ru.tinkoff.scrollingpagerindicator.ViewPagerAttacher as implementation example. And then you can attach your pager like this:

indicator.attachToPager(pager, new ViewPagerAttacher());

Customization

Attribute Explanation Default Value
spi_dotSize The diameter of a dot. 6dp
spi_dotSelectedSize The diameter of a currently selected dot. 10dp
spi_dotColor The color of a dot. @android:color/darker_gray
spi_dotSelectedColor The color of the currently selected dot. @android:color/darker_gray
spi_dotSpacing The distance from center to center of each dot. 8dp
spi_visibleDotCount The maximum number of dots which will be visible at the same time. If pager has more pages than visible_dot_count, indicator will scroll to show extra dots. Must be odd number. 5
spi_visibleDotThreshold The minimum number of dots which should be visible. If pager has less pages than visibleDotThreshold, no dots will be shown. 2
spi_looped The mode for looped pagers support. You should make indicator looped if your custom pager is looped too. If pager has less items than spi_visibleDotCount, indicator will work as usual; otherwise it will always be in infinite state. false
spi_dotMinimumSize The minimum dot size for the corner dots. This size is lower or equal to spi_dotSize and greater or equal to the internal calculation for the corner dots. Internal calculation based on spi_dotSize, spi_dotSelectedSize and spi_dotSpacing
spi_orientation Visible orientation of the dots LinearLayoutManager.HORIZONTAL
spi_firstDotDrawable Custom drawable of the first dot, color is tinted and size is changed like standard dots. If first dot is also the last one then this value is taken into account. null
spi_lastDotDrawable Custom drawable of the last dot, color is tinted and size is changed like standard dots. null

TODO

  1. Some extreme customizations may work incorrect.
  2. There is no possibility to set fixed indicator width (because it's width is based on spi_visibleDotCount).

License

Copyright 2018 Tinkoff Bank

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

scrollingpagerindicator's People

Contributors

al-dr avatar aleksandr-deich avatar beigirad avatar eliaszemanek avatar josh-burton avatar koral-- avatar lebeshev avatar n-olifer avatar nastya1808 avatar nov1kov avatar psrustik avatar technoir42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

scrollingpagerindicator's Issues

dot indicator is not selected in first time

Hi @technoir42 I am using dot indicator with Horizontal RecyclerView, when the view is open in first time, the dot indicator is not appears selected, I have to scroll the list to change the dot color.

I hope you can help me..

Cheers!

image

Set faded dots count?

Is is possible to set the faded dots count from both the sides?

Eg.:
FadedDotsCount = 2

Output:
.oOOOo.

How to use it with looped RecyclerView?

I am using InfiniteAdapter as a wrapper for wrappedAdapter with real positions.

So I have getItemCount in my InfiniteAdapter:

override fun getItemCount(): Int = Int.MAX_VALUE

RecyclerViewAttacher expects real count. So, this attacher gets adapter from my recyclerview via getAdapter(). RecyclerViewAttacher receives an instance of InfiniteAdapter which returns Int.MAX_VALUE as itemCount and shows wrong dots count. I expect to see dots count the same as I have in my wrappedAdapter. So, I have to copy RecyclerViewAttacher to project and just change code a little, that it works. My point is that I would like to reuse most of code of this library.

So, I would suggest to change RecyclerViewAttacher in the following way:

  1. Create some delegate interface like:
interface RecyclerViewAdapterDelegate{
     fun getAdapterItemCount(): Int

     fun registerDataSetObserver(dataObserver : RecyclerView.AdapterDataObserver)

     fun unregisterDataSetObserver(dataObserver : RecyclerView.AdapterDataObserver)
}

May be we need some other methods to delegate from adapter, it's possible to add.
2) Pass it to constructor

class RecyclerViewBaseAttacher(private val adapterDelegate: RecyclerViewAdapterDelegate) {
    ....  
}
  1. This additional abstraction helps to use some different adapters.

If you don't mind such changes I can implement it and create a pull request. Of course I will write it in Java.

What do you think guys?

Add detach from method

Hi, it's a nice and simple to use library, good job there.

I'm having a situation where I need to detach the indicator from my recycler view or otherwise it will leak the whole activity. It would be nice to have detach methods to avoid any potential memory leak.

Thanks

Usage in project with embedded RecyclerView

My project has embedded androidx.recyclerview.widget.RecyclerView as code and when I try to use attachToRecyclerView I got error Cannot access class 'androidx.recyclerview.widget.RecyclerView'. Check your module classpath for missing or conflicting dependencies. How I can fix this?

ViewPager2.registerOnPageChangeCallback()

I connected ScrollingPagerIndicator with ViewPager2:


            adapter = Adapter(it)
            adapter.list = list
            viewPager2.adapter = adapter
            indicator.attachToPager(viewPager)

and then I've added:

viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                updatePosition(position)
                super.onPageSelected(position)
            }
        })

With running the app I have below error:

 java.lang.IllegalStateException: Adding and removing callbacks during dispatch to callbacks is not supported
        at androidx.viewpager2.widget.CompositeOnPageChangeCallback.throwCallbackListModifiedWhileInUse(CompositeOnPageChangeCallback.java:95)
        at androidx.viewpager2.widget.CompositeOnPageChangeCallback.onPageSelected(CompositeOnPageChangeCallback.java:76)
        at androidx.viewpager2.widget.CompositeOnPageChangeCallback.onPageSelected(CompositeOnPageChangeCallback.java:73)
        at androidx.viewpager2.widget.ScrollEventAdapter.dispatchSelected(ScrollEventAdapter.java:432)
        at androidx.viewpager2.widget.ScrollEventAdapter.onScrolled(ScrollEventAdapter.java:197)
        at androidx.recyclerview.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:5173)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:4231)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3862)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:22844)

Any thoughts on how to solve it? (I still want to use VP2)

crash in recyclerView

Hello,
in our recyclerView is an item with ScrollingPagerIndicator.
It crashes when we are trying to reload some data.

    java.lang.IllegalStateException: Observer ru.tinkoff.scrollingpagerindicator.ViewPagerAttacher$1@6f7b8cb was not registered.
        at android.database.Observable.unregisterObserver(Observable.java:69)
        at android.support.v4.view.PagerAdapter.unregisterDataSetObserver(PagerAdapter.java:311)
        at ru.tinkoff.scrollingpagerindicator.ViewPagerAttacher.detachFromPager(ViewPagerAttacher.java:80)
        at ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.detachFromPager(ScrollingPagerIndicator.java:268)
        at ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.attachToPager(ScrollingPagerIndicator.java:250)
        at ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.attachToPager(ScrollingPagerIndicator.java:201)

could you please add a check while detachFromPager() ?

Negative offset in ScrollingPagerIndicator.onPageScrolled(int page, float offset)

So it seems like if you are scrolling your viewpager slides too fast it's possible to get a negative offset
value in ScrollingPagerIndicator.onPageScrolled(int page, float offset). Although ViewPager.onPageScrolled claims that possible value belongs to [0, 1).

btw by scrolling fast I mean using emulator and arrow keys to scroll.

screen shot 2018-02-20 at 19 46 22

How to use it inside the layout of recycler view item or viewpager?

Thanks for the awesome library. I have used it in many projects. The issue I'm facing is how can I put it inside the layout of the recycler view item. By this I mean is for example in recycler view I have two items one is ImageView and the other is TextView I want to add this in the center of these two views. What I want to achieve is a way to constraint this 'ScrollingPagerIndicator' to a specific view of the recycler view layout.

java.lang.IllegalStateException: Fixed width is not supported

Hey guys, I have got next issue:

java.lang.IllegalStateException: Fixed width is not supported at ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.onMeasure(ScrollingPagerIndicator.java:308) at android.view.View.measure(View.java:19909) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) at android.view.View.measure(View.java:19909)

Here is layout and there is no fixed size:

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.prilaga.instastories.view.widget.CustomViewPager
            android:id="@+id/media_view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:pagerViewModel="@{viewModel}"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/bar_color">

            <EditText
                android:id="@+id/caption_edit_text"
                android:layout_width="match_parent"
                android:layout_marginBottom="-5dp"
                android:layout_height="wrap_content"
                android:text="@{viewModel.captionObservable}"
                android:textColor="@color/white"
                android:visibility="@{viewModel.captionVisibilityObservable}"/>

            <android.support.v7.widget.AppCompatImageButton
                android:id="@+id/hashtag_button"
                style="@style/Widget.AppCompat.Button.Action"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/caption_edit_text"
                android:onClick="@{() -> viewModel.openHashtags()}"
                android:src="@drawable/ic_hashtag"/>

            <ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator
                android:id="@+id/media_pager_indicator"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/hashtag_button"
                android:layout_centerHorizontal="true"
                android:padding="@dimen/l_size"
                app:spi_dotColor="@color/white"
                app:spi_dotSelectedColor="@color/blue_insta"
                />

            <android.support.v7.widget.AppCompatImageButton
                android:id="@+id/save_or_delete_button"
                style="@style/Widget.AppCompat.Button.Action"
                android:layout_below="@+id/caption_edit_text"
                android:layout_toLeftOf="@+id/repost_button"
                android:layout_toStartOf="@+id/repost_button"
                android:onClick="@{() -> viewModel.saveOrDeleteMedia()}"
                android:src="@{viewModel.saveOrTrashIconObservable}"
                tools:src="@drawable/ic_save_white"/>

            <android.support.v7.widget.AppCompatImageButton
                android:id="@+id/repost_button"
                style="@style/Widget.AppCompat.Button.Action"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/caption_edit_text"
                android:onClick="@{() -> viewModel.repostMedia()}"
                android:src="@drawable/ic_share"/>

        </RelativeLayout>

    </FrameLayout>

@PhilNehaev @d-tarasov @ivampir @artyomkiss @sotnich

overflow Integer.MAX_VALUE

Hi, i tried once with recyclerView and in its adapter i set the itemCount to

        @Override
        public int getItemCount() {
            return Integer.MAX_VALUE;
        }

Then it crashes because of OutOfMemoryError

    java.lang.OutOfMemoryError: float[] of length 2147483647 would overflow
        at ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.initDots(ScrollingPagerIndicator.java:464)
        at ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.setDotCount(ScrollingPagerIndicator.java:324)
        at ru.tinkoff.scrollingpagerindicator.RecyclerViewAttacher.attachToPager(RecyclerViewAttacher.java:111)
        at ru.tinkoff.scrollingpagerindicator.RecyclerViewAttacher.attachToPager(RecyclerViewAttacher.java:13)
        at ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.attachToPager(ScrollingPagerIndicator.java:251)
        at ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.attachToRecyclerView(ScrollingPagerIndicator.java:219)

Though it's minor issue, there would be any chance to have such many items. ๐Ÿ˜

Import error while attaching to ViewPager2

Version 1.2.4 doesn't import the ScaledDotsIndicator in java file. There is an error importing the ScaledDotsIndicator class.
The below statement doesn't seem to work.

import ru.tinkoff.scrollingpagerindicator.ViewPager2Attacher;

Error Screenshot 1:
image

More actions:
image

Any solution on this?

Fatal Exception: java.lang.IndexOutOfBoundsException Position must be [0, adapter.getItemCount()]

Hi,

I'm using ViewPager2 with ListAdapter in RecycleView. Every time data is updated, view is attached to viewPager, again. When changing adapter data, setting a new list, a crash from the title occurs.
Do you know the reason why?

Full log:
ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.setCurrentPosition (ScrollingPagerIndicator.java:390)
ru.tinkoff.scrollingpagerindicator.ViewPager2Attacher.updateIndicatorDotsAndPosition (ViewPager2Attacher.java:66)
ru.tinkoff.scrollingpagerindicator.ViewPager2Attacher.attachToPager (ViewPager2Attacher.java:23)
ru.tinkoff.scrollingpagerindicator.ViewPager2Attacher.attachToPager (ViewPager2Attacher.java:7)
ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.attachToPager (ScrollingPagerIndicator.java:297)
ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.attachToPager (ScrollingPagerIndicator.java:247)

the last dot in indicator not getting selected when last item of recyclerview is reached by scrolling

` <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />

    <ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator
        android:id="@+id/indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recycler_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginBottom="@dimen/_5sdp"
        app:spi_dotSize="@dimen/_5sdp"
        app:spi_dotSelectedSize="@dimen/_5sdp"
        app:spi_visibleDotCount="5"
        app:spi_visibleDotThreshold="5"/>`

vHolder.indicator.attachToRecyclerView(vHolder.recyclerView,0); vHolder.indicator.setCurrentPosition(0); vHolder.indicator.setDotColor(vHolder.recyclerView.getContext().getResources().getColor(R.color.unselected_green)); vHolder.indicator.setSelectedDotColor(vHolder.recyclerView.getContext().getResources().getColor(R.color.selected_dark_green)); vHolder.indicator.setDotCount(RemoteConfigManager.init().getFeedFeatureNavList().size());

set visibleDotCount as even numbers

Firstly, thank you for the project.
I was trying to set the visibleDotCount property as 4 and noticed that on the code force to be an odd number, is there any reason for that? Is there any plans to add this as a feature?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.