欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

简单实现Android滚动公告栏

程序员文章站 2024-02-17 18:46:04
实现的效果,是一个滚动的公告栏,是这样的: 可以看到这个公告栏一方面是滚动,另外一方面是可点击。 实现的思路: 1.textview放在viewflipper中实...

实现的效果,是一个滚动的公告栏,是这样的:

简单实现Android滚动公告栏

可以看到这个公告栏一方面是滚动,另外一方面是可点击。

实现的思路:

1.textview放在viewflipper中实现滑动效果(可设置左右、或者上下滚动),很明显这应该是自定义view;

2.利用textview的点击事件即可实现点击;

ok,先看看自定义view的代码:

public class marqueetextview extends linearlayout { 
 
 private context mcontext; 
 private viewflipper viewflipper; 
 private view marqueetextview; 
 private string[] textarrays; 
 private marqueetextviewclicklistener marqueetextviewclicklistener; 
 
 public marqueetextview(context context) { 
 super(context); 
 mcontext = context; 
 initbasicview(); 
 } 
 
 
 public marqueetextview(context context, attributeset attrs) { 
 super(context, attrs); 
 mcontext = context; 
 initbasicview(); 
 } 
 
 public void settextarraysandclicklistener(string[] textarrays, marqueetextviewclicklistener marqueetextviewclicklistener) {//1.设置数据源;2.设置监听回调(将textview点击事件传递到目标界面进行操作) 
 this.textarrays = textarrays; 
 this.marqueetextviewclicklistener = marqueetextviewclicklistener; 
 initmarqueetextview(textarrays, marqueetextviewclicklistener); 
 } 
 
 public void initbasicview() {//加载布局,初始化viewflipper组件及效果 
 marqueetextview = layoutinflater.from(mcontext).inflate(r.layout.marquee_textview_layout, null); 
 layoutparams layoutparams = new linearlayout.layoutparams(layoutparams.match_parent, layoutparams.wrap_content); 
 addview(marqueetextview, layoutparams); 
 viewflipper = (viewflipper) marqueetextview.findviewbyid(r.id.viewflipper); 
 viewflipper.setinanimation(animationutils.loadanimation(mcontext, r.anim.slide_in_bottom));//设置上下的动画效果(自定义动画,所以改左右也很简单) 
 viewflipper.setoutanimation(animationutils.loadanimation(mcontext, r.anim.slide_out_top)); 
 viewflipper.startflipping(); 
 } 
 
 public void initmarqueetextview(string[] textarrays, marqueetextviewclicklistener marqueetextviewclicklistener) { 
 if (textarrays.length == 0) { 
  return; 
 } 
 
 int i = 0; 
 viewflipper.removeallviews(); 
 while (i < textarrays.length) { 
  textview textview = new textview(mcontext); 
  textview.settext(textarrays[i]); 
  textview.setonclicklistener(marqueetextviewclicklistener); 
  layoutparams lp = new linearlayout.layoutparams(layoutparams.match_parent, layoutparams.wrap_content); 
  viewflipper.addview(textview, lp); 
  i++; 
 } 
 } 
 
 public void releaseresources() { 
 if (marqueetextview != null) { 
  if (viewflipper != null) { 
  viewflipper.stopflipping(); 
  viewflipper.removeallviews(); 
  viewflipper = null; 
  } 
  marqueetextview = null; 
 } 
 } 
 
} 

然后,主activity异常简单(还是封装得好):

public class mainactivity extends appcompatactivity { 
 private marqueetextview marqueetv; 
 private string [] textarrays = new string[]{"this is content no.1","this is content no.2","this is content no.3"}; 
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 setcontentview(r.layout.activity_main); 
 marqueetv = (marqueetextview) findviewbyid(r.id.marqueetv); 
 
 marqueetv.settextarraysandclicklistener(textarrays, new marqueetextviewclicklistener() { 
  @override 
  public void onclick(view view) { 
  startactivity(new intent(mainactivity.this,anotheractivity.class)); 
  } 
 }); 
 } 
 
 @override 
 protected void ondestroy() { 
 marqueetv.releaseresources(); 
 super.ondestroy(); 
 } 
} 

git地址>>https://github.com/ganshenml/marqueetextviewapp

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。