notice_vf 为 ViewFlipper
xml:
<ViewFlipper android:id="@+id/notice_vf" android:layout_width="0dp" android:layout_marginStart="16dp" android:layout_height="match_parent" android:layout_weight="1"> </ViewFlipper>
activity:
private var mCurrPos = -1 private var mNoticeListData: MutableList<NoticeListInfo>? = ArrayList() private var mTimer : Timer? = null private var mTimerTask : TimerTask? = null //开启公告通知滚动 private fun startNoticeRoll() { mNoticeListData!!.add(NoticeListInfo(12345)) mNoticeListData!!.add(NoticeListInfo(23456)) mNoticeListData!!.add(NoticeListInfo(34567)) mTimerTask = object : TimerTask() { override fun run() { activity!!.runOnUiThread { noticeMoveToNext() } } } mTimer = Timer() mTimer!!.schedule(mTimerTask,0,5000) } private fun noticeMoveToNext() { setNoticeView(mCurrPos, mCurrPos + 1) notice_vf.setInAnimation(context,R.anim.in_bottomtop) notice_vf.setOutAnimation(context,R.anim.out_topbottom) notice_vf.showNext() } private fun setNoticeView(current: Int, nextNum: Int) { var next = nextNum val noticeView = layoutInflater.inflate(R.layout.item_notice_roll, null) val noticeContent = noticeView.findViewById<TextView>(R.id.tv_notice_content) if ((current < next) && (next > (mNoticeListData!!.size - 1))) { next = 0 } else if ((current > next) && (next < 0)) { next = mNoticeListData!!.size - 1 } noticeContent.text = mNoticeListData!![next].id.toString() noticeContent.setOnClickListener { //跳转到公告详情页 } if (notice_vf.childCount > 1) { notice_vf.removeViewAt(0) } notice_vf.addView(noticeView, notice_vf.childCount) mCurrPos = next }