码迷,mamicode.com
首页 > 其他好文 > 详细

ViewPager 滑动页(三)

时间:2014-09-09 15:15:58      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   os   io   使用   java   

需求:滑动展示页,能够使用本地数据,及获取服务器数据进行刷新操作,当滑动到最后一页时,结束当前activity,进入下一个activity;

效果图:

bubuko.com,布布扣bubuko.com,布布扣

 

实现分析:

1.目录结构:

bubuko.com,布布扣

bubuko.com,布布扣

代码实现:

1.PosterView.java

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;
    }
    
}

项目总结:

  ViewPager的onPageChangeListener监听获取,当前的滑动状态;

  http://www.cnblogs.com/sishuiliuyun/p/3962347.html

 

代码:http://pan.baidu.com/s/1gdBZTGv

 

ViewPager 滑动页(三)

标签:android   style   blog   http   color   os   io   使用   java   

原文地址:http://www.cnblogs.com/sishuiliuyun/p/3962362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!