Giter Club home page Giter Club logo

circularviewpager's Introduction

CircularViewPager

This is my implementation of a circular ViewPager. If you build the sample you will see an example.

You can also download the sample app in the Google Play Store: demo

Alt text for your video

##Features

  • enables endless scrolling
  • very easy to use
  • no limitations, you can use it like a normal ViewPager

##Usage

Add a usual ViewPager to your layout and set the overScrollMode to never to avoid bad user experience:

	<android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:overScrollMode="never"/> 


Create an adapter for your ViewPager and let it extend the BaseCircularViewPagerAdapter and implement it's methods:

public class MemeCircularViewPagerAdapter extends BaseCircularViewPagerAdapter<Meme> {
private final Context mContext;

    public MemeCircularViewPagerAdapter(final Context context, final FragmentManager fragmentManager, final List<Meme> memes) {
        super(fragmentManager, memes);
        mContext = context;
    }

    @Override
    protected Fragment getFragmentForItem(final Meme meme) {
        return MemeViewPagerItemFragment.instantiateWithArgs(mContext, meme);
    }
}


Initialize your ViewPager and set the adapter to it. Also create a CircularViewPagerHandler and set it as ViewPager.OnPageChangeListener to the ViewPager:

final ViewPager viewPager = (ViewPager) findViewById(R.id.activity_main_view_pager);
viewPager.setAdapter(new MemeCircularViewPagerAdapter(this, getSupportFragmentManager(), Meme.createSampleMemes()));
viewPager.setOnPageChangeListener(new CircularViewPagerHandler(viewPager));


If you would like to get track of the page change events, you can set your own `ViewPager.OnPageChangeListener` to the `CircularViewPagerHandler`.
final CircularViewPagerHandler circularViewPagerHandler = new CircularViewPagerHandler(viewPager);
circularViewPagerHandler.setOnPageChangeListener(createOnPageChangeListener());
viewPager.setOnPageChangeListener(circularViewPagerHandler);

##How does it work To achieve circular scrolling the BaseCircularViewPagerAdapter pretends to have two more items, a "copy" of the last item at the very first position and a "copy" of the first item at the very last position. This is realised by just increasing the value returned at the getCount() method of the adapter by 2 and handling to return the correct item for the current position.

If the items would be A | B | C, the adapter actually "contains" C | A | B | C | A. The initial position of the adapter is 1, so it displays A. If the user now swipes left, the first C gets displayed. After C has settled, the current position of the ViewPager gets set to 3 by the CircularViewPagerHandler, so the second C gets displayed. The user can now swipe again and again until the procedure repeats. The same concept applies to the other direction.

That's it, this enables circular scrolling for a ViewPager.

##Integration

The lib is available on Maven Central, you can find it with Gradle, please

dependencies {
    compile 'com.github.tobiasbuchholz:circularviewpager:1.0.0'
}

##Contribution If you would like to contribute to this project make sure you send pull request to dev branch or create an issue.

##Developed by

##License

Copyright 2014 Tobias Buchholz

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.

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.