Skip to content

Commit

Permalink
修复无法停止的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ronghao committed Sep 21, 2017
1 parent db9437f commit 8c44d08
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -17,6 +16,8 @@ public class MainActivity extends AppCompatActivity {
private MarqueeTextView margueeText2;
private AutoScrollTextView autoScrollTextView1;

private ArrayList<String> strings;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -41,6 +42,7 @@ public void onClick(View view) {
public void onClick(View view) {
margueeText.stopScroll();
margueeText1.stopScroll();
autoScrollTextView1.stopAutoScroll();
}
});
}
Expand Down Expand Up @@ -91,12 +93,19 @@ public void onFinish() {

autoScrollTextView1 = (AutoScrollTextView) findViewById(R.id.main_autoscroll_text1);

List<String> 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();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -56,6 +58,7 @@ public void handleMessage(Message msg) {
}
break;
case FLAG_STOP_AUTO_SCROLL:
isStop = true;
handler.removeMessages(FLAG_START_AUTO_SCROLL);
break;
}
Expand Down Expand Up @@ -89,6 +92,7 @@ public void startAutoScroll() {
*/
public void stopAutoScroll() {
handler.sendEmptyMessage(FLAG_STOP_AUTO_SCROLL);
stopText();
}

/**
Expand All @@ -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() {
Expand All @@ -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);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 8c44d08

Please sign in to comment.