viewpager 自动滑动

精贴 置顶
3675 0

public class MainActivity extends Activity {
        ArrayList<View> list;
        ImageView[] imageViews;
        ViewGroup main, group;
        ImageView imageView;
        ViewPager viewPager;
        boolean is = true;
        Handler handler;
        Message ms;
        int i = 0;
 
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                this.requestWindowFeature(Window.FEATURE_NO_TITLE);
                LayoutInflater inflater = getLayoutInflater();
 
                list = new ArrayList<View>();
 
                list.add(inflater.inflate(R.layout.item1, null));
                list.add(inflater.inflate(R.layout.item2, null));
                list.add(inflater.inflate(R.layout.item3, null));
                imageViews = new ImageView[list.size()];
                main = (ViewGroup) inflater.inflate(R.layout.activity_main, null);
                group = (ViewGroup) main.findViewById(R.id.viewgroup);
                viewPager = (ViewPager) main.findViewById(R.id.viewpager);
                for (int i = 0; i < list.size(); i++) {
                        imageView = new ImageView(MainActivity.this);
                        imageView.setLayoutParams(new LayoutParams(20, 20));
                        // imageView.setLayoutParams(new LayoutParams(
                        // LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                        imageView.setPadding(10, 0, 10, 0);
                        imageViews<I><I> = imageView;
                        if (i == 0) {
                                imageViews
                                                .setBackgroundResource(R.drawable.page_indicator_focused);
                        } else {
                                imageViews.setBackgroundResource(R.drawable.page_indicator);
                        }
 
                        group.addView(imageView);
 
                }
                setContentView(main);
                handler = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                                Log.e("", "" + msg.arg1);
                                viewPager.setCurrentItem(msg.arg1);
 
                        }
                };
                viewPager.setAdapter(new MyAdapter());
                new Thread(new Runnable() {
 
                        @Override
                        public void run() {
                                while (is) {
                                        try {
                                                ms = handler.obtainMessage();
                                                ms.arg1 = i;
                                                handler.sendMessage(ms);
 
                                                Thread.sleep(2000);
                                                i++;
                                                if (i > 2) {
                                                        i = 0;
 
                                                }
                                        } catch (InterruptedException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                        }
 
                                }
 
                        }
                }).start();
                viewPager.setOnPageChangeListener(new MyListener());
                       
 
        class MyAdapter extends PagerAdapter {
 
                @Override
                public int getCount() {
 
                        return list.size();
                }
 
                @Override
                // 判断实View和Object是否为同一个View
                public boolean isViewFromObject(View arg0, Object arg1) {
 
                        return arg0 == arg1;
                }
 
                @Override//这个方法用来得到每个View
                public Object instantiateItem(ViewGroup container, int position) {
                        container.addView(list.get(position));
                        return list.get(position);
                }
 
                @Override//用以控制当某个View不需要的时候的回收处理
                public void destroyItem(ViewGroup container, int position, Object object) {
 
                        container.removeView(list.get(position));
                }
 
        }
//viewpager滑动监听方法
        class MyListener implements OnPageChangeListener {
 
                @Override//状态有三个0空闲,1是增在滑行中,2目标加载完毕 
                public void onPageScrollStateChanged(int arg0) {
                        // TODO Auto-generated method stub
 
                }
 
                @Override//从1到2滑动,在1滑动前调用 
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                        // TODO Auto-generated method stub
 
                }
 
                @Override//当viewpager页面被选择时调用的方法
                public void onPageSelected(int position) {
                        for (int i = 0; i < imageViews.length; i++) {
                                imageViews[position]
                                                .setBackgroundResource(R.drawable.page_indicator_focused);
                                if (position != i) {
                                        imageViews
                                                        .setBackgroundResource(R.drawable.page_indicator);
                                }
                        }
 
                }
        }
 
        @Override
        protected void onDestroy() {
 
                super.onDestroy();
                is = false;
        }
 
}

  • 没有任何评论
今日天气 ···

···

···

···

热门排行
CSS cursor鼠标样式一览表 2012-06-25
.NET后台写JS代码 2011-11-03
网站地址多出jdfwkey的问题解析及... 2010-08-25
数据库xxx的日志已满,请备份该数... 2013-07-19
百度、google、Yahoo网站地图制作... 2011-01-30
<a>标签的伪类书写顺序问题... 2010-09-04
什么是长尾关键词? 2010-09-24
CEO名言 2010-08-31
Mysql 主从数据库同步 2010-09-12
用ASP实现网页BBS 2010-11-01
博主推荐
本个人博客微信公众平台上线啦~~... 2013-10-13
饼哥网络互联上线啦~~要买域名空... 2013-09-20
网站title标题如何正确修改不会被... 2013-08-26
饼哥通讯录系统上线啦,欢迎大家... 2013-08-24
ASP .NET MYSQL 的简单分页 并不... 2013-08-19
网站有弹窗广告这样的站点,百度... 2013-08-17
做淘宝SEO优化需要注意的8大问题 2013-08-17
淘宝网怎么做SEO优化 2013-08-17
站长们要学习的“苍井空精神” 2013-08-17
苹果公司今日发布了iOS 7第五个开... 2013-08-07
随便看看
使用CSS设置网页图片透明度 2013-11-09
字符型数据 2010-10-16
去哪儿购买域名和域名注册 2013-06-23
Xenu-死链接检测工具 2010-09-26
站长们要学习的“苍井空精神” 2013-08-17
精彩实用:Win XP热门技巧十招 2010-11-07
布尔逻辑运算符 2010-10-18
关于URL组成部分的指南 2010-09-23
你说这个世界是大还是小呢? 2013-06-17
js怎么输出30-50之间的随机数 2010-09-12
RSS新闻
传媒新闻
CSDN
八卦新闻
女性新闻
台湾新闻
互联网
军事-新浪博客
IT-新浪博客
汽车新闻
游戏新闻
国际新闻
国内新闻
体育新闻
我的微博
北京 上海 杭州 深圳 广州 成都