android 公告 滚动 (跑马灯效果)

1
notice_vf 为 ViewFlipper<br><br>xml:
1
2
3
4
5
6
7
8
<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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    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
    }

  

  

  

posted @   IT_lss  阅读(1846)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示