仿淘寶頭條的實現
在淘寶客戶端中,有一個垂直輪播的廣告條(如下圖),這個是怎麼顯示的呢?其實很簡單,只要用系統提供的控制元件就可以了,三步輕鬆搞定!

image.jpeg
1.設定佈局,輪播控制元件用的是系統的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ViewFlipper android:id="@+id/vf" android:autoStart="true" android:flipInterval="5000" android:inAnimation="@anim/anim_marquee_in" android:outAnimation="@anim/anim_marquee_out" android:layout_width="match_parent" android:layout_height="100dp"/> </LinearLayout>
2.設定滾動動畫,一個是進入的,一個是出去的
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1500" android:fromYDelta="0" android:toYDelta="-100%p" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1500" android:fromYDelta="100%p" android:toYDelta="0"/> </set>
3.新增View
data.forEachIndexed { index, s -> vf.addView(View.inflate(this,R.layout.list_item,null)) }