`
flueky
  • 浏览: 1132 次
社区版块
存档分类
最新评论

Android TextView 垂直滚动

阅读更多

最近工作中遇到需要TextView垂直滚动显示,看过N多帖子,方法不外乎两种。自定义TextView和用canvas绘制。这对于小编这个刚Android开发刚入门的菜鸟来说,无疑是高大上的方法。综合了这些帖子中的优点,总结出最简便的TextView方法。

 

       int showContent = 0;// 显示的空间
       public void handler() {
		hyhandler.removeCallbacks(runnable);
		currentIndex = currentIndex % noticeList.size();
		//设置两行数据,预先显示下一条内容的第一行数据,增加滚动的连贯性
                //noticeList保存多条内容
		text_notice.setText(noticeList.get(currentIndex).getMESSAGEINFO()
				+ "\n"
				+ noticeList.get((currentIndex + 1) % noticeList.size())
						.getMESSAGEINFO());
		currentIndex++;
                //在项目中,noticeList是请求过来的数据,如果有数据,haveNotice为真,开始滚动
		if (haveNotice) {
			// 有公告便开始滚动
			hyhandler.postDelayed(runnable, scroolTime);
		}
	}

	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			int lineHeight = text_notice.getLineHeight(); // 每一行的高度
			int lineCount = text_notice.getLineCount() / 2;// 总行数
			showContent += 1;
			text_notice.scrollBy(0, 1);
			hyhandler.postDelayed(this, scroolTime);
			if (showContent >= lineCount * lineHeight) {
                        //滚动坐标从TextView左上角开始计算,当显示玩下一条数据的第一行时,重置TextView数据以及会滚到最顶部 *-1表示回滚
				handler();
				text_notice.scrollBy(0, (lineCount * lineHeight) * -1);
				showContent = 0;
			}
		}
	};

 方法入口,调用handler方法。

 别忘了给TextView控件添加手动滑动效果

text_notice.setMovementMethod(ScrollingMovementMethod.getInstance());

在xml布局时需注意:

                android:maxLines="1"

                android:scrollbars="none"

                android:singleLine="false"

maxLines为滚动区域的行数。

由于实际问题,目前只验证了maxLines=1时,连续滚动有保证。maxLines值改变时,待验证,只需在run方法中修改即可

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics