From 9f0a3744ae47894a3b93ffd64acbb204e7e21fe1 Mon Sep 17 00:00:00 2001 From: qinliyang Date: Thu, 23 May 2019 12:17:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=BB=91=E5=8A=A8=E5=88=B0=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E6=9D=A1=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/weigan/loopview/LoopView.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/loopview/src/main/java/com/weigan/loopview/LoopView.java b/loopview/src/main/java/com/weigan/loopview/LoopView.java index d617a5e..20895ea 100644 --- a/loopview/src/main/java/com/weigan/loopview/LoopView.java +++ b/loopview/src/main/java/com/weigan/loopview/LoopView.java @@ -368,6 +368,32 @@ public void setCurrentPosition(int position) { } } + + /** + * scroll to designated item + * + * @param i + */ + public void smoothScrollToItem(int i) { + if (items == null || items.isEmpty()) { + return; + } + if (i < 0) { + i = 0; + } + if (i > items.size() - 1) { + i = items.size() - 1; + } + cancelFuture(); + float itemHeight = lineSpacingMultiplier * maxTextHeight; + int i1 = i - getSelectedItem(); + if (i1 == 0) + return; + mFuture = mExecutor.scheduleWithFixedDelay(new SmoothScrollTimerTask(this, (int) (itemHeight * i1)), 0, 15, TimeUnit.MILLISECONDS); + invalidate(); + } + + @Override protected void onDraw(Canvas canvas) { if (items == null) {