ViewFlipper實現輪播滾動公告
阿新 • • 發佈:2019-02-05
對於ViewFlipper的使用還是很簡單的,有感興趣的可以檢視下原始碼。以下實現的是垂直翻滾,如果想要修改的話,改一下動畫效果即可。
1.直接上程式碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical"> <TextView android:id="@+id/title_name" android:layout_width="match_parent" android:layout_height="46dp" android:background="@color/title" android:gravity="center" android:text="android快速開發工具類" android:textColor="@color/white" android:textSize="16sp" /> <ViewFlipper android:id="@+id/marquee_viewflipper" android:layout_width="match_parent" android:layout_height="30dp" android:background="#e8e8e8" android:autoStart="true" android:flipInterval="2000" android:inAnimation="@anim/anim_in"android:outAnimation="@anim/anim_out" > </ViewFlipper> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
1.autoStart 是否自動開啟輪播,這個方法設定為true的時候在原始碼中,也可以呼叫java程式碼setAutoStart(boolean autoStart)
2.flipInterval 輪播時間
3.inAnimation ViewFlipper中子View進入時的動畫
4.outAnimation ViewFlipper中子View離開時的動畫
2.
mViewFlipper = (ViewFlipper)findViewById(R.id.marquee_viewflipper); LinearLayout shenzhenIndexLayout = (LinearLayout) View.inflate(this, R.layout.marquee_layout_item1, null); LinearLayout shangzhenIndexLayout = (LinearLayout) View.inflate(this, R.layout.marquee_layout_item2, null); LinearLayout cyIndexLayout = (LinearLayout) View.inflate(this, R.layout.marquee_layout_item3, null); mViewFlipper.addView(shenzhenIndexLayout); mViewFlipper.addView(shangzhenIndexLayout); mViewFlipper.addView(cyIndexLayout);
3. anim_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration = "1000" android:fromYDelta = "100%p" android:toYDelta = "0" /> </set>
4.anim_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1000" android:fromYDelta="0" android:toYDelta="-100%p" /> </set>
5.marquee_layout_item1.xml marquee_layout_item2.xml marquee_layout_item3.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="公告1" /> </LinearLayout>
以上三個佈局檔案類似。