GithubHelp home page GithubHelp logo

rayworks / scrollingpagerindicator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tinkoff-mobile-tech/scrollingpagerindicator

0.0 2.0 0.0 1 MB

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

License: Apache License 2.0

Java 100.00%

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

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 "com.android.support:support-core-ui:x.x.x"

or

implementation "com.android.support:appcompat-v7:x.x.x"
  1. Add view to layout:
<android.support.v4.view.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 "com.android.support:recyclerview-v7:x.x.x"
  1. Add view to layout:
<android.support.v7.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 with HORIZONTAL orientation 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

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

josh-burton avatar n-olifer avatar rayworks avatar technoir42 avatar aleksandr-deich avatar

Watchers

James Cloos avatar  avatar

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.