最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
简单实现Android滚动公告栏
时间:2017-03-05 编辑:简简单单 来源:一聚教程网
实现的效果,是一个滚动的公告栏,是这样的:
可以看到这个公告栏一方面是滚动,另外一方面是可点击。
实现的思路:
1.textView放在ViewFlipper中实现滑动效果(可设置左右、或者上下滚动),很明显这应该是自定义view;
2.利用textView的点击事件即可实现点击;
OK,先看看自定义view的代码:
代码如下 | 复制代码 |
publicclassMarqueeTextViewextendsLinearLayout {
privateContext mContext; privateViewFlipper viewFlipper; privateView marqueeTextView; privateString[] textArrays; privateMarqueeTextViewClickListener marqueeTextViewClickListener;
publicMarqueeTextView(Context context) { super(context); mContext = context; initBasicView(); }
publicMarqueeTextView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; initBasicView(); }
publicvoidsetTextArraysAndClickListener(String[] textArrays, MarqueeTextViewClickListener marqueeTextViewClickListener) {//1.设置数据源;2.设置监听回调(将textView点击事件传递到目标界面进行操作) this.textArrays = textArrays; this.marqueeTextViewClickListener = marqueeTextViewClickListener; initMarqueeTextView(textArrays, marqueeTextViewClickListener); }
publicvoidinitBasicView() {//加载布局,初始化ViewFlipper组件及效果 marqueeTextView = LayoutInflater.from(mContext).inflate(R.layout.marquee_textview_layout,null); LayoutParams layoutParams =newLinearLayout.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(); }
publicvoidinitMarqueeTextView(String[] textArrays, MarqueeTextViewClickListener marqueeTextViewClickListener) { if(textArrays.length ==0) { return; }
inti =0; viewFlipper.removeAllViews(); while(i < textArrays.length) { TextView textView =newTextView(mContext); textView.setText(textArrays[i]); textView.setOnClickListener(marqueeTextViewClickListener); LayoutParams lp =newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); viewFlipper.addView(textView, lp); i++; } }
publicvoidreleaseResources() { if(marqueeTextView !=null) { if(viewFlipper !=null) { viewFlipper.stopFlipping(); viewFlipper.removeAllViews(); viewFlipper =null; } marqueeTextView =null; } }
} |
然后,主Activity异常简单(还是封装得好):
代码如下 | 复制代码 |
publicclassMainActivityextendsAppCompatActivity { privateMarqueeTextView marqueeTv; privateString [] textArrays =newString[]{"this is content No.1","this is content No.2","this is content No.3"};
@Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); marqueeTv = (MarqueeTextView) findViewById(R.id.marqueeTv);
marqueeTv.setTextArraysAndClickListener(textArrays,newMarqueeTextViewClickListener() { @Override publicvoidonClick(View view) { startActivity(newIntent(MainActivity.this,AnotherActivity.class)); } }); }
@Override protectedvoidonDestroy() { marqueeTv.releaseResources(); super.onDestroy(); } } |
-
上一个: 映客直播怎么赚钱?映客直播赚钱方法详解
-
下一个: 百度手机助手app怎么关闭自动安装应用?
相关文章
- sora软件价格介绍 02-22
- sora官网入口地址一览 02-22
- Sora生成的视频使用教程 02-22
- 《梦幻西游》元宵灯谜线索答案大全2024 02-22
- 《原神》有朋自远方来第一天通关攻略 02-22
- 《苏醒之路》四个结局达成攻略 02-22