动画:
top.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" android:shareInterpolator="false" android:zAdjustment="top"> <translate android:duration="1000" android:fromYDelta="0" android:toYDelta="-100%p" /> </set>
bottom.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" android:shareInterpolator="false" android:zAdjustment="top"> <translate android:duration="1000" android:fromYDelta="100%" android:toYDelta="0" /> </set> 布局:
<TextSwitcher android:id="@+id/frag1_adapter1_tsw" android:layout_width="match_parent" android:layout_height="60dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:inAnimation="@anim/bottom" android:outAnimation="@anim/top" /> 这是在适配器里的代码:
private TextView textView; private String[] strs; private int index = 0;
//京东快报 // 要显示的文本 strs = new String[]{"双11回馈活动产品利率增长0.05%", "国家大数据发展纲要", "郑重公告", "某某网站会员须知", "网站维护公告"}; holder.getTsw().setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { //new 一个TextView textView = new TextView(context); textView.setTextSize(20); textView.setText("京东双11优惠活动!!!"); return textView; } }); //new一个Handler final android.os.Handler handler = new android.os.Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: index++; holder.getTsw().setText(strs[index % strs.length]); if (index == strs.length) { index = 0; } break; } } }; //new一个线程 new Thread(new Runnable() { @Override public void run() { while (index < strs.length) { synchronized (this) { SystemClock.sleep(5000);//每隔4秒滚动一次 handler.sendEmptyMessage(0); } } } }).start();