From 8c44d08598bb4a3099465c8e7e396cee58d8b5e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=A3=E6=B5=A9?= Date: Thu, 21 Sep 2017 17:50:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95=E5=81=9C?= =?UTF-8?q?=E6=AD=A2=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../autoscrolltextviewsample/MainActivity.java | 13 +++++++++++-- .../autoscrolltextview/AutoScrollTextView.java | 18 +++++++++++++++++- .../autoscrolltextview/MarqueeSwitcher.java | 16 +++++++--------- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/haohahu/autoscrolltextviewsample/MainActivity.java b/app/src/main/java/com/haohahu/autoscrolltextviewsample/MainActivity.java index 031512a..da3feec 100644 --- a/app/src/main/java/com/haohahu/autoscrolltextviewsample/MainActivity.java +++ b/app/src/main/java/com/haohahu/autoscrolltextviewsample/MainActivity.java @@ -8,7 +8,6 @@ import com.haohahu.autoscrolltextview.IMarqueeListener; import com.haohahu.autoscrolltextview.MarqueeTextView; import java.util.ArrayList; -import java.util.List; public class MainActivity extends AppCompatActivity { @@ -17,6 +16,8 @@ public class MainActivity extends AppCompatActivity { private MarqueeTextView margueeText2; private AutoScrollTextView autoScrollTextView1; + private ArrayList strings; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -41,6 +42,7 @@ public void onClick(View view) { public void onClick(View view) { margueeText.stopScroll(); margueeText1.stopScroll(); + autoScrollTextView1.stopAutoScroll(); } }); } @@ -91,12 +93,19 @@ public void onFinish() { autoScrollTextView1 = (AutoScrollTextView) findViewById(R.id.main_autoscroll_text1); - List strings = new ArrayList<>(); + strings = new ArrayList<>(); strings.add("0走马灯古称蟠螭灯(秦汉)、仙音烛和转鹭灯(唐)、马骑灯(宋),中国特色工艺品,亦是传统节日玩具之一,属于灯笼的一种。"); strings.add("1灯内点上蜡烛,烛产生的热力造成气流,令轮轴转动。轮轴上有剪纸,烛光将剪纸的影投射在屏上,图象便不断走动。"); strings.add("2因多在灯各个面上绘制古代武将骑马的图画,而灯转动时看起来好像几个人你追我赶一样"); strings.add("3因多在灯各个面上绘制古代武将骑马的图画,而灯转动时看起来好像几个人你追我赶一样"); autoScrollTextView1.setTextList(strings); + + autoScrollTextView1.setOnItemClickListener(new AutoScrollTextView.OnItemClickListener() { + @Override + public void onItemClick(int position) { + Toast.makeText(MainActivity.this, strings.get(position), Toast.LENGTH_SHORT).show(); + } + }); } } diff --git a/library/src/main/java/com/haohahu/autoscrolltextview/AutoScrollTextView.java b/library/src/main/java/com/haohahu/autoscrolltextview/AutoScrollTextView.java index e697447..28868f9 100644 --- a/library/src/main/java/com/haohahu/autoscrolltextview/AutoScrollTextView.java +++ b/library/src/main/java/com/haohahu/autoscrolltextview/AutoScrollTextView.java @@ -33,6 +33,8 @@ public class AutoScrollTextView extends MarqueeSwitcher implements ViewSwitcher. private Handler handler; + private boolean isStop = false; + public AutoScrollTextView(Context context) { this(context, null); } @@ -56,6 +58,7 @@ public void handleMessage(Message msg) { } break; case FLAG_STOP_AUTO_SCROLL: + isStop = true; handler.removeMessages(FLAG_START_AUTO_SCROLL); break; } @@ -89,6 +92,7 @@ public void startAutoScroll() { */ public void stopAutoScroll() { handler.sendEmptyMessage(FLAG_STOP_AUTO_SCROLL); + stopText(); } /** @@ -100,9 +104,17 @@ public void setOnItemClickListener(OnItemClickListener itemClickListener) { @Override public View makeView() { - + // FIXME: 2017/9/21 添加这层RelativeLayout是解决动画默认回到句首的问题 RelativeLayout layout = new RelativeLayout(getContext()); MarqueeTextView textView = new MarqueeTextView(getContext()); + textView.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View view) { + if (itemClickListener != null && textList.size() > 0 && currentId != -1) { + itemClickListener.onItemClick(currentId % textList.size()); + } + } + }); textView.setMarqueeListener(new IMarqueeListener() { @Override public void onStart() { @@ -111,6 +123,10 @@ public void onStart() { @Override public void onFinish() { + if (isStop) { + isStop = false; + return; + } handler.sendMessageDelayed(Message.obtain(handler, FLAG_START_AUTO_SCROLL), 1000); } }); diff --git a/library/src/main/java/com/haohahu/autoscrolltextview/MarqueeSwitcher.java b/library/src/main/java/com/haohahu/autoscrolltextview/MarqueeSwitcher.java index 6154c1e..e0ba112 100644 --- a/library/src/main/java/com/haohahu/autoscrolltextview/MarqueeSwitcher.java +++ b/library/src/main/java/com/haohahu/autoscrolltextview/MarqueeSwitcher.java @@ -8,7 +8,7 @@ import android.widget.ViewSwitcher; /** - * MarqueeSwitcher {@link android.widget.ViewSwitcher} t + * MarqueeSwitcher {@link android.widget.TextSwitcher} t * * @author haohao on 2017/9/21 下午 03:57 * @version v1.0 @@ -70,14 +70,12 @@ public void setText(String text, IMarqueeListener iMarqueeListener) { showNext(); } - /** - * Sets the text of the text view that is currently showing. This does - * not perform the animations. - * - * @param text the new text to display - */ - public void setCurrentText(CharSequence text) { - ((MarqueeTextView) getCurrentView()).setText(text); + public void stopText() { + MarqueeTextView t = getNextView(); + if (t != null) t.stopScroll(); + + MarqueeTextView t1 = getCurrentView(); + if (t != null) t1.stopScroll(); } public MarqueeTextView getCurrentView() {