ViewFlipper 非常適合實現首頁的消息上下滾動或是左右滾動,而且使用也是非常的方便
首先在佈局中
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vf_msg"
android:flipInterval="2500"
android:inAnimation="@anim/vf_anim_in"
android:outAnimation="@anim/vf_anim_out"
android:layout_marginLeft="@dimen/dp_12"
/>
filpinterval 是兩個view之間切換的間隔 單位ms
還有一個自動開始滾動的屬性
android:autoStart=“true”
inAnimation 和outAnimation 就是我們主要來實現兩個view之間切換的動畫
可以上下,可以左右
如下:
進入動畫
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800">
<translate android:fromYDelta="100%p" android:toYDelta="0"/>
</set>
出動畫
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800">
<translate android:fromYDelta="0" android:toYDelta="-100%p"/>
</set>
這樣就是從下往上滾動的一個動畫效果
從接口獲取數據調用
List<String> bodyList = vfMsgBean.getBodyList();
if (bodyList != null && bodyList.size() > 0){
for (int i = 0; i < 3; i++) {
String item = bodyList.get(i);
View view = inflater.inflate(R.layout.main_rec_and_wanted_item_layout,vfJob,false);
TextView textView = view.findViewById(R.id.item_tv);
textView.setTextColor(getResources().getColor(R.color.red));
textView.setText(item);
vfMsg.addView(view);
}
vfMsg.startFlipping();
}
這樣就完成了