代碼以下java
MainActivity.javaandroid
package com.example.vp_demo2;app
import java.util.ArrayList;ide
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;佈局
import com.example.vp_demo2.adapter.MyPagerAdapter;
import com.example.vp_demo2.bean.News;this
public class MainActivity extends Activity {
ArrayList<News> newsList = new ArrayList<News>();
private static final int FIRST_PAGE = 1;
public static final int AUTO_SCROLL = 0;
private int currentPosition;
private LinearLayout ll_dots;
private ViewPager viewPager;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == AUTO_SCROLL) {
// 獲取當前viewPager所在索引值
int currentItem = viewPager.getCurrentItem();
// 讓索引++
currentItem++;
// 從新設置給viewPager
viewPager.setCurrentItem(currentItem);
// 從新調用發送消息的方法
handler.sendEmptyMessageDelayed(AUTO_SCROLL, 2000);
}
};
};spa
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
ll_dots = (LinearLayout) findViewById(R.id.ll_dots);
// 初始化數據
initData();
// 初始化小點
initDots();
// 設置數據適配器
viewPager.setAdapter(new MyPagerAdapter(this, newsList,handler));
// 設置當前展現的位置爲1 setCurrentItem 設置當前要輪播的viewpager
viewPager.setCurrentItem(FIRST_PAGE);
// 設置viewPager的監聽事件
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
// 滑動狀態改變的方法 state :draaging 拖拽 idle 靜止 settling 慣性過程
@Override
public void onPageScrollStateChanged(int state) {
// 若是是靜止狀態,將當前頁進行替換
if (state == ViewPager.SCROLL_STATE_IDLE)
// 設置當前頁,smoothScroll 平穩滑動
viewPager.setCurrentItem(currentPosition, false);
}.net
// 滑動過程當中的方法 position 索引值
// positionOffset 0-1
// positionOffsetPixels 偏移像素值
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}xml
// 選中某一頁的監聽
@Override
public void onPageSelected(int position) {
if (position == newsList.size() - 1) {
// 設置當前值爲1
currentPosition = FIRST_PAGE;
} else if (position == 0) {
// 若是索引值爲0了,就設置索引值爲倒數第二個
currentPosition = newsList.size() - 2;
} else {
currentPosition = position;
}
// 設置小點監聽
changeDots();
}
});blog
// 使用handler發送延時消息
handler.sendEmptyMessageDelayed(AUTO_SCROLL, 2000);
}
//
@Override
protected void onDestroy() {
super.onDestroy();
// 移除全部回調和消息
handler.removeCallbacksAndMessages(null);
}
private void changeDots() {
int dotPosition = currentPosition - 1;
// 設置小點監聽
for (int i = 0; i < ll_dots.getChildCount(); i++) {
ImageView imageView = (ImageView) ll_dots.getChildAt(i);
if (i == dotPosition) {
// 從線性佈局中取出對應小點,設置點亮
imageView.setImageResource(R.drawable.image_select);
} else {
imageView.setImageResource(R.drawable.image);
}
}
}
private void initDots() {
for (int i = 0; i < newsList.size() - 2; i++) {
// 實例化ImageView
ImageView imageView = new ImageView(this);
// 若是是第一頁,設置爲選中圖片
if (i == 0) {
imageView.setImageResource(R.drawable.image_select);
} else {
imageView.setImageResource(R.drawable.image);
}
// 代碼中設置大小是指相對像素---px
LayoutParams params = new LayoutParams(15, 15);
params.setMargins(5, 5, 5, 5);
// 添加到線性佈局中
ll_dots.addView(imageView, params);
}
}
private void initData() {
newsList.add(new News("我是第四個美女", R.drawable.a3)); // 0
newsList.add(new News("我是第一個美女", R.drawable.a)); // 1
newsList.add(new News("我是第二個美女", R.drawable.a1)); // 2
newsList.add(new News("我是第三個美女", R.drawable.a2)); // 3
newsList.add(new News("我是第四個美女", R.drawable.a3)); // 4
newsList.add(new News("我是第一個美女", R.drawable.a)); // 5
}
}
-----------------------------------------------------------------------------------------
MyPagerAdapter.java 代碼
package com.example.vp_demo2.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.vp_demo2.MainActivity;
import com.example.vp_demo2.R;
import com.example.vp_demo2.bean.News;
public class MyPagerAdapter extends PagerAdapter {
private final Context context;
private final ArrayList<News> newsList;
private final Handler handler;
//構造方法
public MyPagerAdapter(Context context, ArrayList<News> newsList,
Handler handler) {
this.handler = handler;
this.context = context;
this.newsList = newsList;
}
//統計循環的次數
@Override
public int getCount() {
return newsList.size();
}
//當前的視圖是否和instantiateItem所返回key對應的view是否一致
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
//初始化佈局方法
@Override
public Object instantiateItem(ViewGroup container, int position) {
// getView
View view = View.inflate(context, R.layout.vp_item, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
// 對ImageView設置觸摸監聽
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
// 按下
case MotionEvent.ACTION_DOWN:
// 取消handler任務
handler.removeCallbacksAndMessages(null);
break;
// case MotionEvent.ACTION_MOVE:
//
// break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
//從新執行handler輪播任務
handler.sendEmptyMessageDelayed(MainActivity.AUTO_SCROLL,
2000);
break;
default:
break;
}
// true 表明是消費該事件
// false 不消費
return true;
}
});
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
imageView.setImageResource(newsList.get(position).getResId());
tv_title.setText(newsList.get(position).getTitle());
// 添加到容器中
container.addView(view);
return view;
}
/**
* 銷燬條目
* object 看instantiateItem中所返回的是否是view,若是是,能夠直接移除object
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
-----------------------------------------------------------------------------------------------------
News.java
package com.example.vp_demo2.bean;
public class News {
private String title;
private int resId;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getResId() {
return resId;
}
public void setResId(int resId) {
this.resId = resId;
}
public News(String title, int resId) {
super();
this.title = title;
this.resId = resId;
}
public News() {
super();
// TODO Auto-generated constructor stub
}
}
---------------------------------------------------------------------------------------------------------------------
activity_main.xml
<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" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/ll_dots"
android:gravity="center_horizontal"
android:layout_alignBottom="@id/viewPager"
android:orientation="horizontal"
android:layout_alignRight="@id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</RelativeLayout>
----------------------------------------------------------
vp_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" />
</RelativeLayout>