Scroll gallery to next page

Using widget.Gallery to display a list of horizontal elements. I implemented paging in a gallery with what seems like a standard method: subclass Gallery and implementation:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (velocityX>0) {
            onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
        } else {
            onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
        }
}

When you click on the gallery, I remove the next and previous buttons of the image. When I click on them, I want the gallery to animate on the next / previous page, respectively. I tried calling onKeyDown from my next button handler, but, strangely enough, this has no effect. AbsSpinner has setSelection(int position, boolean animate), but the animation is ignored in the Gallery.

+3
source share
3 answers

Exactly what the toucan said, but for further clarification (comment restrictions are too short):

, Gallery , . scrollToChild() :

private boolean scrollToChild(int childPosition) {
    View child = getChildAt(childPosition);

    if (child != null) {
        int distance = getCenterOfGallery() - getCenterOfView(child);
        mFlingRunnable.startUsingDistance(distance);
        return true;
    }

    return false;
}

, , . , ( ), , onKeyDown, - .

, , . - setSpacing(-1) - , , .

, , ( Android, ). , .

( 2012):. ( , , ), Android ViewPager class. , , ( , ). , Android 2.x +, .

+2

2 : nextbtn, backbtn gall

nextbtn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        gall.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
    }
});

backbtn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        gall.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
    }
});         
+2
    String strMethod = velocityX > 0 ? "movePrevious" : "moveNext";
    try {
        Method method = Gallery.class.getDeclaredMethod(strMethod, null);
        method.setAccessible(true);
        method.invoke(this, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
0
source

Source: https://habr.com/ru/post/1793128/


All Articles