当前位置: 首页 > news >正文

Android消息公告上下滚动切换轮播实现

自定义控件

通过继承TextSwitcher实现

直接上代码

public class NoticesSwitcher extends TextSwitcher implements ViewSwitcher.ViewFactory {private Context mContext;private List<Notice> mData;private final long DEFAULT_TIME_SWITCH_INTERVAL = 1000;//1sprivate long mTimeInterval = DEFAULT_TIME_SWITCH_INTERVAL;private int mCurrentIndex = 0;private Notice curNotice;public NoticesSwitcher(Context context, AttributeSet attrs) {super(context, attrs);this.mContext = context;setFactory(this);}public NoticesSwitcher setInAnimation(int animationResId){Animation animation = AnimationUtils.loadAnimation(this.mContext, animationResId);setInAnimation(animation);return this;}public NoticesSwitcher setOutAnimation(int animationResId){Animation animation = AnimationUtils.loadAnimation(this.mContext, animationResId);setOutAnimation(animation);return this;}/*** 通知/公告数据绑定* @param data* @return*/public NoticesSwitcher bindData(List<Notice> data) {this.mData = data;return this;}public void startSwitch(long timeInterval){this.mTimeInterval = timeInterval;mSwitchHandler.removeMessages(0);if (mData != null && mData.size() > 0) {mSwitchHandler.sendEmptyMessage(0);}else{throw new RuntimeException("data is empty");}}/*** 工厂类中创建TextView供给TextSwitcher使用* @return*/@Overridepublic View makeView() {LayoutInflater inflater = LayoutInflater.from(mContext);TextView view = (TextView) inflater.inflate(R.layout.item_notice_switcher, null);view.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (curNotice != null) {// 跳转详情}}});return view;}private Handler mSwitchHandler = new Handler(){@Overridepublic void dispatchMessage(Message msg) {super.dispatchMessage(msg);int index = mCurrentIndex % mData.size();curNotice = mData.get(index);setText(mData.get(index).title);mCurrentIndex++;if (mData.size() > 1) {sendEmptyMessageDelayed(0, mTimeInterval);}}};}
public class Notice {public String title;public Notice() {}public Notice(String title) {this.title = title;}
}

使用

<com.anyixing.etc.staff.view.NoticesSwitcherandroid:id="@+id/notices_switcher"android:layout_width="match_parent"android:layout_height="wrap_content"/>
List<Notice> result = new ArrayList<>();
result.add(new Notice("两部门向灾区预拨2亿元救灾资金"));
result.add(new Notice("特朗普被裁定不具备总统参选资格"));
result.add(new Notice("泰国瑞幸向中国瑞幸索赔20亿元"));
binding.layoutNotices.setVisibility(View.VISIBLE);
binding.noticesSwitcher.setInAnimation(R.anim.anim_notice_in).setOutAnimation(R.anim.anim_notice_out).bindData(result).startSwitch(2500);

完了,就这么简单

http://www.lryc.cn/news/263590.html

相关文章:

  • tensorflow入门 自定义模型
  • 虚拟机启动 I/O error in “xfs_read_agi+0x95“
  • 【MYSQL】-库的操作
  • 网络协议小记
  • STM32-I2C通讯-AHT20温湿度检测
  • 【机器学习】043_准确率、精确率、召回率
  • 【Qt开发流程】之文件目录、文件、输入和输出
  • CSS的基本选择器及高级选择器(附详细示例以及效果图)
  • 股票价格预测 | Python实现基于Stacked-LSTM的股票预测模型,可预测未来(keras)
  • 数据可视化---离群值展示
  • LeetCode Hot100 51.N皇后
  • 机器学习 | 贝叶斯方法
  • 缓存的定义及重要知识点
  • TrustZone之顶层软件架构
  • SpringBoot Whitelabel Error Page 报错--【已解决】
  • 02.Git常用基本操作
  • 黑盒测试中关键截图如何打点
  • 画图之C4架构图idea和vscode环境搭建篇
  • 安卓小练习-校园闲置交易APP(SQLite+SimpleCursorAdapter适配器)
  • Pycharm 如何更改成中文版| Python循环语句| for 和 else 的搭配使用
  • 智合同是怎么审合同的?
  • 使用Httpclient来替代客户端的jsonp跨域解决方案
  • 测试工具Jmeter:设置中文界面
  • K8s攻击案例:RBAC配置不当导致集群接管
  • 运行hive的beelin2时候going to print operations logs printed operations logs
  • 从 MySQL 到 DolphinDB,Debezium + Kafka 数据同步实战
  • 六.聚合函数
  • Eclipse_03_如何加快index速度
  • scrapy的入门和使用
  • yolov5单目测距+速度测量+目标跟踪(算法介绍和代码)