Friday, September 2, 2011

[android-developers] Current item from PageView

I'd like to be able to get the current visible view from my ViewPager,
but the index reported to my OnPageChangeListener doesn't seem to
always be correct. (i.e. sometimes I get a null View, or a view other
than the one that's centered).

Here's some code:

public class ReaderViewPager extends ViewPager {
private int focusedIndex;
private int newFocusedIndex;
private Direction dir;

private final ViewPager.OnPageChangeListener mPageChangeListener =
new ViewPager.OnPageChangeListener() {

public void onPageSelected(int index) {
Log.d(TAG, "SELECTED "+index);
newFocusedIndex = index;
}

public void onPageScrolled(int arg0, float arg1, int arg2) {
Log.d(TAG, "onPageScrolled has happened");
}

public void onPageScrollStateChanged(int state) {
Log.d(TAG, "onPageScrollStateChanged has happened");
if (state == ViewPager.SCROLL_STATE_IDLE) {
focusedIndex = newFocusedIndex;
}
}
};


public ReaderViewPager(Context context) {
super(context);
initReaderViewPager(context);
}

public ReaderViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
initReaderViewPager(context);
}

private void initReaderViewPager(Context context){
focusedIndex = 0;
setOnPageChangeListener(mPageChangeListener);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
PageView view = (PageView) this.getChildAt(focusedIndex);
if (view != null && !view.hasNextViewport(Direction.RIGHT))
{
return super.onInterceptTouchEvent(arg0);
}
return false;
}

In the end, I'd like to be able to intercept touches from with my
child views before the parent container sees them. Suggestions etc?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment