最新的项目有个新需求,就是要去垂直滚动去展示最新发布的消息,类似淘宝头条的那种
1.功能实现其实很简单,就用到Android 的原生控件ViewFlipper
<ViewFlipper
android:id="@+id/vf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:autoStart="true"
android:flipInterval="2000" 滚动的时间间隔,这里我是写的两秒
android:inAnimation="@anim/anim_marquee_in" 进入动画
android:outAnimation="@anim/anim_marquee_out" 退出动画/>
2.Activity代码实现
View view = LayoutInflater.from(this).inflate(R.layout.childviewflipper,null);
txtContent = view.findViewById(R.id.txt_content);
txtContent.setText(dataList.get(i).toString());
viewFlipper.addView(view);
viewFlipper.startFlipping();
效果图:
代码很少,比较简单,如果项目要源码的 可以去我的GitHub上下载,https://github.com/xiao-er/ViewFlipper