需求:滑動展現頁,可以使用本地數據,及獲取服務器數據進行刷新操做,當滑動到最後一頁時,結束當前activity,進入下一個activity;html
效果圖:java
實現分析:android
1.目錄結構:服務器
代碼實現:app
1.PosterView.javaide
package com.jjc.demo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; public class PosterView extends FrameLayout{ private Context context; private ViewPager viewPager; private LinearLayout container; private PosterAdapter adapter; private List<Integer> posterList = new ArrayList<Integer>(); private ImageView imageView; // 底部小點的圖片 private ImageView[] points; // 記錄當前選中位置 private int currentIndex = 0; private int nextIndex; private int pageNum = 1; public PosterView(Context context) { this(context, null); } public PosterView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; LayoutInflater.from(context).inflate(R.layout.view_poster, this, true); initView(); } @Override protected void onDetachedFromWindow(){ super.onDetachedFromWindow(); } private void initView(){ container = (LinearLayout) findViewById(R.id.container); viewPager = (ViewPager)findViewById(R.id.viewpager); viewPager.setOnPageChangeListener(new viewpagerListener()); adapter = new PosterAdapter(context, posterList); viewPager.setAdapter(adapter); } /** * 刷新海報頁數據; * @param list */ public void refreshData(List<Integer> list){ if(list == null || list.isEmpty()){ return; } initPoint(list.size()); posterList.clear(); posterList.addAll(list); adapter.notifyDataSetChanged(); } private void initPoint(int num){ pageNum = num; container.removeAllViews(); if(num == 0){ return; } points = new ImageView[num]; for (int i = 0; i < num; i++) { imageView = new ImageView(context); imageView.setBackgroundResource(R.drawable.poster_point_selector); LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(12, 12); mLayoutParams.rightMargin = 8; mLayoutParams.leftMargin = 8; mLayoutParams.topMargin = 24; mLayoutParams.bottomMargin = 28; points[i] = imageView; // 設置位置tag,方便取出與當前位置對應 points[i].setTag(i); // 默認都設爲灰色 points[i].setEnabled(false); // 給每一個小點設置監聽 points[i].setOnClickListener(new pointListener()); container.addView(points[i], mLayoutParams); } // 設置爲白色,即選中狀態 points[currentIndex].setEnabled(true); } private class viewpagerListener implements OnPageChangeListener { int pageState = 100; @Override public void onPageScrollStateChanged(int arg0) { /** * arg0:什麼都沒作 * arg1:正在滑動 * arg2:滑動完畢 */ pageState = arg0; } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { if(!posterList.isEmpty() && arg0 == (posterList.size()-1) && pageState == 1){ Toast.makeText(context, "最後一頁", Toast.LENGTH_SHORT).show(); ((Activity)context).finish(); } } @Override public void onPageSelected(int state) { setCurDot(state); } } private class pointListener implements OnClickListener { @Override public void onClick(View v) { int position = (Integer) v.getTag(); setCurView(position); setCurDot(position); } } /** * 設置當前頁面的位置 */ private void setCurView(int position) { if (position < 0 || position >= pageNum) { return; } viewPager.setCurrentItem(position); } /** * 設置當前的小點的位置 */ private void setCurDot(int positon) { if (positon < 0 || positon > pageNum - 1 || currentIndex == positon) { return; } points[positon].setEnabled(true); points[currentIndex].setEnabled(false); currentIndex = positon; } }
項目總結:post
ViewPager的onPageChangeListener監聽獲取,當前的滑動狀態;ui
http://www.cnblogs.com/sishuiliuyun/p/3962347.htmlthis