简单实现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

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

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。