ViewPager 滑動頁(二)

需求:滑動展現頁,可以使用本地數據,及獲取服務器數據進行刷新操做,並實現頁面自動切換java

效果圖:android

實現分析:服務器

1.目錄結構:ide

代碼實現:oop

1.PosterView.javapost

package com.jjc.demo;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
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;

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;
    
    private Handler handler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            if (points!= null && points.length >0) {
                nextIndex = (currentIndex + 1) % points.length;
                // 設置當前頁面
                setCurDot(nextIndex);
                setCurView(nextIndex);
                handler.removeMessages(0);
                handler.sendEmptyMessageDelayed(0, 5000);
            }

        }
    };
    
    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(){
        handler.removeMessages(0);
        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();
        
        handler.removeMessages(0);
        handler.sendEmptyMessageDelayed(0, 5000);
    }
    
    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 {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int position) {
            // 設置底部小點選中狀態
            setCurDot(position);
        }
    }
    
    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;
    }
    
}

 

項目總結:this

  對於頁面動態切換,採用handle進行切換;spa

 

代碼:http://pan.baidu.com/s/1jJH1O3d

相關文章
相關標籤/搜索