scrolling-demo icon indicating copy to clipboard operation
scrolling-demo copied to clipboard

Java version?

Open AndroidDeveloperLB opened this issue 7 years ago • 6 comments

I don't know Kotlin well. I tried to convert the code to Java. Is the next conversion ok ?

/**
 * code based on : https://github.com/manidesto/scrolling-demo
 */
public class RecyclerViewEx extends RecyclerView {
    private static final int INVALID_POINTER = -1;
    private int scrollPointerId = INVALID_POINTER;
    private int initialTouchX = 0;
    private int initialTouchY = 0;
    private int touchSlop;

    public RecyclerViewEx(final Context context) {
        this(context, null);
    }

    public RecyclerViewEx(final Context context, @Nullable final AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RecyclerViewEx(final Context context, @Nullable final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        ViewConfiguration vc = ViewConfiguration.get(context);
        touchSlop = vc.getScaledTouchSlop();
    }

    @Override
    public void setScrollingTouchSlop(final int slopConstant) {
        super.setScrollingTouchSlop(slopConstant);
        ViewConfiguration vc = ViewConfiguration.get(getContext());
        switch (slopConstant) {
            case TOUCH_SLOP_DEFAULT:
                touchSlop = vc.getScaledTouchSlop();
                break;
            case TOUCH_SLOP_PAGING:
                touchSlop = vc.getScaledPagingTouchSlop();
                break;
        }
    }

    @Override
    public boolean onInterceptTouchEvent(final MotionEvent e) {
        if (e == null)
            return false;
        int action = MotionEventCompat.getActionMasked(e);
        int actionIndex = MotionEventCompat.getActionIndex(e);
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                scrollPointerId = e.getPointerId(0);
                initialTouchX = Math.round(e.getX() + 0.5f);
                initialTouchY = Math.round(e.getY() + 0.5f);
                return super.onInterceptTouchEvent(e);
            case MotionEvent.ACTION_POINTER_DOWN:
                scrollPointerId = e.getPointerId(actionIndex);
                initialTouchX = Math.round(e.getX(actionIndex) + 0.5f);
                initialTouchY = Math.round(e.getY(actionIndex) + 0.5f);
                return super.onInterceptTouchEvent(e);
            case MotionEvent.ACTION_MOVE:
                int index = e.findPointerIndex(scrollPointerId);
                if (index < 0)
                    return false;
                int x = Math.round(e.getX(index) + 0.5f);
                int y = Math.round(e.getY(index) + 0.5f);
                if (getScrollState() != SCROLL_STATE_DRAGGING) {
                    int dx = x - initialTouchX;
                    int dy = y - initialTouchY;
                    boolean startScroll = false;
                    if (getLayoutManager().canScrollHorizontally() && Math.abs(dx) > touchSlop
                            && (getLayoutManager().canScrollVertically() || Math.abs(dx) > Math.abs(dy)))
                        startScroll = true;

                    if (getLayoutManager().canScrollVertically() && Math.abs(dy) > touchSlop
                            && (getLayoutManager().canScrollHorizontally() || Math.abs(dy) > Math.abs(dx)))
                        startScroll = true;
                    return startScroll && super.onInterceptTouchEvent(e);
                }
                return super.onInterceptTouchEvent(e);
            default:
                return super.onInterceptTouchEvent(e);
        }
    }

    @Override
    public void requestDisallowInterceptTouchEvent(final boolean disallowIntercept) {
        //super.requestDisallowInterceptTouchEvent(disallowIntercept);
        // do nothing
    }
}

This is for when both checkboxes are enabled in the sample. That's the only code which is needed, right? There is nothing more that I can find that's really relevant.

AndroidDeveloperLB avatar Sep 12 '16 12:09 AndroidDeveloperLB

The corresponding java code for BetterRecyclerView. The code for FeedRootRecyclerView is present in blog post

okmanideep avatar Sep 15 '16 08:09 okmanideep

I think your link to the blog post isn't the correct one :)

AndroidDeveloperLB avatar Sep 15 '16 08:09 AndroidDeveloperLB

Have corrected it. Can you check now ? :P

okmanideep avatar Sep 15 '16 08:09 okmanideep

Oh that's how i got to here . I even wrote there about needing to remove Twitter stuff.... :)

Thanks.

AndroidDeveloperLB avatar Sep 15 '16 08:09 AndroidDeveloperLB

Hi, The blog post link you mentioned doesn't exist any more. Could you please provide updated link.

dubeyankit25 avatar Jun 05 '18 05:06 dubeyankit25

Try Wayback machine website

AndroidDeveloperLB avatar Jun 05 '18 05:06 AndroidDeveloperLB