activity_main.xmljava
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="200dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBottom="@id/viewpager" android:background="#33000000" android:orientation="vertical" android:padding="5dp"> <TextView android:id="@+id/tv_image_description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="廣告測試" android:textColor="@android:color/white" /> <LinearLayout android:id="@+id/ll_point_group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5dp" android:orientation="horizontal"/> </LinearLayout> </RelativeLayout>
point_background.xmlandroid
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/point_bg_enable" android:state_enabled="true"/> <item android:drawable="@drawable/point_bg_normal" android:state_enabled="false"/> </selector>
point_bg_enable.xmlapp
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <corners android:radius="0.5dp" /> <solid android:color="#AAFFFFFF" /> </shape>
point_bg_normal.xmlide
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <corners android:radius="0.5dp" /> <solid android:color="#55000000" /> </shape>
MainActivity測試
package cn.seanlou.viewpagedemo; import android.app.Activity; import android.graphics.drawable.GradientDrawable; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.provider.ContactsContract; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity { private ViewPager mViewPager; private TextView tvDescription; private LinearLayout llPointGroup; /** * 廣告條正下方的標語 */ private String[] imageDescriptionArray = {"廣告1", "廣告2", "廣告3", "廣告4", "廣告5"}; /** * 記錄上一次點的位置,默認爲0 */ private int previousPointEnale = 0; private List<ImageView> mImageList = new ArrayList<ImageView>(); int[] imageIds = new int[]{R.mipmap.a, R.mipmap.b, R.mipmap.c, R.mipmap.d, R.mipmap.e}; /** * 記錄是否中止循環播放 */ private boolean isStop = false; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1); super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findView(); testAD(); } /** * 測試廣告 */ private void testAD() { // TODO:廣告 mImageList.clear(); llPointGroup.removeAllViews(); // 初始化廣告條資源 for (final int id : imageIds) { ImageView mImageView = new ImageView(this); mImageView.setBackgroundResource(id); mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, id + "", Toast.LENGTH_SHORT).show(); } }); mImageList.add(mImageView); // 初始化廣告條正下方的"點" View dot = new View(this); dot.setBackgroundResource(R.drawable.point_background); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(5, 5); params.leftMargin = 10; dot.setLayoutParams(params); dot.setEnabled(false); llPointGroup.addView(dot); } mViewPager.setAdapter(new ADsAdapter()); // 設置廣告條跳轉時,廣告語和狀態語的變化 mViewPager.setOnPageChangeListener(new MyOnPageChangeListener()); // 初始化廣告條,當前索引Integer.MAX_VALUE的一半 int index = (Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2 % mImageList.size()); mViewPager.setCurrentItem(index); // 設置當前選中的Page,會觸發onPageChangListener.onPageSelected方法 } private void findView() { mViewPager = (ViewPager) findViewById(R.id.viewpager); tvDescription = (TextView) findViewById(R.id.tv_image_description); llPointGroup = (LinearLayout) findViewById(R.id.ll_point_group); } /** * 滑動監聽 */ private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener { @Override public void onPageScrolled(int i, float v, int i2) { } @Override public void onPageSelected(int i) { //獲取新的位置 int newPosition = i % imageDescriptionArray.length; //設置廣告標語 tvDescription.setText(imageDescriptionArray[newPosition]); //消除上一次的狀態點 llPointGroup.getChildAt(previousPointEnale).setEnabled(false); //設置當前的狀態點 llPointGroup.getChildAt(newPosition).setEnabled(true); //記錄新的位置 previousPointEnale = newPosition; } @Override public void onPageScrollStateChanged(int i) { } } private class ADsAdapter extends PagerAdapter { @Override public int getCount() { // 將viewpager頁數設置成Integer.MAX_VALUE,能夠模擬無限循環 return Integer.MAX_VALUE; } /** * 複用對象 true 複用view false 複用的是Object */ @Override public boolean isViewFromObject(View view, Object o) { return view == o; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(mImageList.get(position % mImageList.size())); return mImageList.get(position % mImageList.size()); } /** * 銷燬對象 * * @param position 被銷燬對象的索引位置 */ @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(mImageList.get(position % mImageList.size())); } } @Override public void onResume() { // 開啓子線程,讓廣告條以3秒的頻率循環播放 isStop = false; new Thread(new Runnable() { @Override public void run() { while (!isStop) { SystemClock.sleep(3000); handler.sendEmptyMessage(-1); } } }).start(); super.onResume(); } @Override public void onPause() { // FragmentHome銷燬時候,關閉循環播放 isStop = true; super.onPause(); } }