Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a OnPageChangeListener to the SliderLayout #100

Merged
merged 4 commits into from
May 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions demo/src/main/java/com/daimajia/slider/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand All @@ -18,11 +19,12 @@
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;

import java.util.HashMap;


public class MainActivity extends ActionBarActivity implements BaseSliderView.OnSliderClickListener{
public class MainActivity extends ActionBarActivity implements BaseSliderView.OnSliderClickListener, ViewPagerEx.OnPageChangeListener{

private SliderLayout mDemoSlider;

Expand Down Expand Up @@ -54,8 +56,8 @@ protected void onCreate(Bundle savedInstanceState) {
.setOnSliderClickListener(this);

//add your extra information
if (textSliderView.getBundle() != null)
textSliderView.getBundle()
textSliderView.bundle(new Bundle());
textSliderView.getBundle()
.putString("extra",name);

mDemoSlider.addSlider(textSliderView);
Expand All @@ -64,6 +66,7 @@ protected void onCreate(Bundle savedInstanceState) {
mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
mDemoSlider.setCustomAnimation(new DescriptionAnimation());
mDemoSlider.setDuration(4000);
mDemoSlider.addOnPageChangeListener(this);
ListView l = (ListView)findViewById(R.id.transformers);
l.setAdapter(new TransformerAdapter(this));
l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
Expand Down Expand Up @@ -109,4 +112,15 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
return super.onOptionsItemSelected(item);
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

@Override
public void onPageSelected(int position) {
Log.d("Slider Demo", "Page Changed: " + position);
}

@Override
public void onPageScrollStateChanged(int state) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public void setViewPager(ViewPagerEx pager){
throw new IllegalStateException("Viewpager does not have adapter instance");
}
mPager = pager;
mPager.setOnPageChangeListener(this);
mPager.addOnPageChangeListener(this);
((InfinitePagerAdapter)mPager.getAdapter()).getRealAdapter().registerDataSetObserver(dataChangeObserver);
}

Expand Down Expand Up @@ -455,8 +455,7 @@ public void onPageSelected(int position) {
if(mItemCount == 0){
return;
}
int n = position % mItemCount;
setItemAsSelected(n - 1);
setItemAsSelected(position-1);
}
@Override
public void onPageScrollStateChanged(int state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ public boolean onTouch(View v, MotionEvent event) {
}
}

public void addOnPageChangeListener(ViewPagerEx.OnPageChangeListener onPageChangeListener){
if(onPageChangeListener != null){
mViewPager.addOnPageChangeListener(onPageChangeListener);
}
}

public void removeOnPageChangeListener(ViewPagerEx.OnPageChangeListener onPageChangeListener) {
mViewPager.removeOnPageChangeListener(onPageChangeListener);
}

public void setCustomIndicator(PagerIndicator indicator){
if(mIndicator != null){
mIndicator.destroySelf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public float getInterpolation(float t) {
private boolean mCalledSuper;
private int mDecorChildCount;

private OnPageChangeListener mOnPageChangeListener;
private ArrayList<OnPageChangeListener> mOnPageChangeListeners = new ArrayList<>();
private OnPageChangeListener mInternalPageChangeListener;
private OnAdapterChangeListener mAdapterChangeListener;
private PageTransformer mPageTransformer;
Expand Down Expand Up @@ -305,6 +305,21 @@ public void onPageScrollStateChanged(int state) {
}
}

private void triggerOnPageChangeEvent(int position) {
for (OnPageChangeListener eachListener : mOnPageChangeListeners) {
if (eachListener != null) {
InfinitePagerAdapter infiniteAdapter = (InfinitePagerAdapter)mAdapter;
if (infiniteAdapter.getRealCount() == 0) {
return;
}
int n = position % infiniteAdapter.getRealCount();
eachListener.onPageSelected(n);
}
}
if (mInternalPageChangeListener != null) {
mInternalPageChangeListener.onPageSelected(position);
}
}
/**
* A PageTransformer is invoked whenever a visible/attached page is scrolled.
* This offers an opportunity for the application to apply a custom transformation
Expand Down Expand Up @@ -394,8 +409,10 @@ private void setScrollState(int newState) {
// PageTransformers can do complex things that benefit from hardware layers.
enableLayers(newState != SCROLL_STATE_IDLE);
}
if (mOnPageChangeListener != null) {
mOnPageChangeListener.onPageScrollStateChanged(newState);
for (OnPageChangeListener eachListener : mOnPageChangeListeners) {
if (eachListener != null) {
eachListener.onPageScrollStateChanged(newState);
}
}
}

Expand Down Expand Up @@ -539,12 +556,7 @@ void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int
// We don't have any idea how big we are yet and shouldn't have any pages either.
// Just set things up and let the pending layout handle things.
mCurItem = item;
if (dispatchSelected && mOnPageChangeListener != null) {
mOnPageChangeListener.onPageSelected(item);
}
if (dispatchSelected && mInternalPageChangeListener != null) {
mInternalPageChangeListener.onPageSelected(item);
}
triggerOnPageChangeEvent(item);
requestLayout();
} else {
populate(item);
Expand All @@ -563,18 +575,12 @@ private void scrollToItem(int item, boolean smoothScroll, int velocity,
}
if (smoothScroll) {
smoothScrollTo(destX, 0, velocity);
if (dispatchSelected && mOnPageChangeListener != null) {
mOnPageChangeListener.onPageSelected(item);
}
if (dispatchSelected && mInternalPageChangeListener != null) {
mInternalPageChangeListener.onPageSelected(item);
if (dispatchSelected) {
triggerOnPageChangeEvent(item);
}
} else {
if (dispatchSelected && mOnPageChangeListener != null) {
mOnPageChangeListener.onPageSelected(item);
}
if (dispatchSelected && mInternalPageChangeListener != null) {
mInternalPageChangeListener.onPageSelected(item);
if (dispatchSelected) {
triggerOnPageChangeEvent(item);
}
completeScroll(false);
scrollTo(destX, 0);
Expand All @@ -583,13 +589,25 @@ private void scrollToItem(int item, boolean smoothScroll, int velocity,
}

/**
* Set a listener that will be invoked whenever the page changes or is incrementally
* Add a listener that will be invoked whenever the page changes or is incrementally
* scrolled. See {@link OnPageChangeListener}.
*
* @param listener Listener to set
* @param listener Listener to add
*/
public void addOnPageChangeListener(OnPageChangeListener listener) {
if (!mOnPageChangeListeners.contains(listener)) {
mOnPageChangeListeners.add(listener);
}
}

/**
* Remove a listener that was added with addOnPageChangeListener
* See {@link OnPageChangeListener}.
*
* @param listener Listener to remove
*/
public void setOnPageChangeListener(OnPageChangeListener listener) {
mOnPageChangeListener = listener;
public void removeOnPageChangeListener(OnPageChangeListener listener) {
mOnPageChangeListeners.remove(listener);
}

/**
Expand Down Expand Up @@ -1706,9 +1724,10 @@ protected void onPageScrolled(int position, float offset, int offsetPixels) {
}
}
}

if (mOnPageChangeListener != null) {
mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);
for (OnPageChangeListener eachListener : mOnPageChangeListeners) {
if (eachListener != null) {
eachListener.onPageScrolled(position, offset, offsetPixels);
}
}
if (mInternalPageChangeListener != null) {
mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels);
Expand Down