关闭

ViewPager轮播

34人阅读 评论(0) 收藏 举报
分类:

package com.rong.viewpager;

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

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {

private ViewPager vp;
private LinearLayout ll;
private int[] images={R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5};

private Handler handler=new Handler(){

    @Override
    public void handleMessage(Message msg) {
        if(msg.what==0){
            int currentItem = vp.getCurrentItem();
            vp.setCurrentItem(++currentItem);
            handler.sendEmptyMessageDelayed(0, 2000);
        }
    }


};
private List<ImageView> list_oval;
int lastIndex;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    vp = (ViewPager) findViewById(R.id.vp);
    ll = (LinearLayout) findViewById(R.id.ll);
    initoval();
    vp.setAdapter(new PagerAdapter() {

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0==arg1;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return Integer.MAX_VALUE;
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                Object object) {

            container.removeView((View) object);

        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView image = new ImageView(MainActivity.this);
            image.setBackgroundResource(images[position%images.length]);
            container.addView(image);
            return image;
        }

    });
    //发送消息给handler
    handler.sendEmptyMessageDelayed(0, 2000);

    vp.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            for (int i = 0; i < list_oval.size(); i++) {
                if(i==arg0%images.length){
                    list_oval.get(i).setImageResource(R.drawable.ischeck);
                }else{
                    list_oval.get(i).setImageResource(R.drawable.uncheck);
                }
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });

}

/**
 * 自定义小圆点
 */
private void initoval(){
    list_oval=new ArrayList<ImageView>();
    for (int i = 0; i < images.length; i++) {
        ImageView image=new ImageView(MainActivity.this);
        if(i==0){
            image.setImageResource(R.drawable.ischeck);
        }else{
            image.setImageResource(R.drawable.uncheck);
        }

        LayoutParams layoutParams=new LayoutParams(20, 20);
        layoutParams.setMargins(5, 0, 5, 0);
        ll.addView(image, layoutParams);
        list_oval.add(image);
    }
}

}

/**
适配器Adapters
*/
package com.bwie.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class MyVp_FragMyShow_Adapter extends PagerAdapter {

private int [] int_image;
private Context context;

public MyVp_FragMyShow_Adapter(int[] int_image, Context context) {
    super();
    this.int_image = int_image;
    this.context = context;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return Integer.MAX_VALUE;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    // TODO Auto-generated method stub
    return arg0==arg1;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {

    container.removeView((View) object);

}

@Override
public Object instantiateItem(ViewGroup container, int position) {

        //创建一个ImageView
        ImageView image=new ImageView(context);
        //加载图片
        image.setBackgroundResource(int_image[position%int_image.length]);
        container.addView(image);
        return image;
}

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:754次
    • 积分:161
    • 等级:
    • 排名:千里之外
    • 原创:16篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档