zoukankan      html  css  js  c++  java
  • 轮播

    //-------------onCreate中------------------------
    //找到控件
    view_pager = (ViewPager) findViewById(R.id.view_pager);
    //设置适配器
    adapter = new BannerAdapter(views);
    view_pager.setAdapter(adapter);
    //初始化view
    initView();
    //轮播
    lunbo();
    //设置默认页
    view_pager.setCurrentItem(0);


    //---------------轮播------------------------
    //轮播
    private void lunbo() {
    //使用Timer发送延迟消息
    Timer timer=new Timer();
    TimerTask task=new TimerTask() {
    int count=0;
    @Override
    public void run() {
    int a=count++;
    handler.sendEmptyMessage(a);
    }
    };
    timer.schedule(task,1000,1000);
    }
    //---------------初始化view----------------------
    //初始化view
    private void initView() {
    View view1=View.inflate(this,R.layout.item_pager1,null);
    View view2=View.inflate(this,R.layout.item_pager2,null);
    View view3=View.inflate(this,R.layout.item_pager3,null);
    View view4=View.inflate(this,R.layout.item_pager4,null);
    views.add(view1);
    views.add(view2);
    views.add(view3);
    views.add(view4);

    }

    //--------------Handler---------------------
    private Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
    view_pager.setCurrentItem(msg.what);

    }
    };
    //===============创建一个类继承pageradapter====并实现以下方法================
    public class BannerAdapter extends PagerAdapter{
    private List<View> views;
    public BannerAdapter(List<View> views) {
    this.views = views;
    }

    @Override
    public int getCount() {
    return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
    return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
    View view=views.get(position%views.size());
    container.addView(view);
    return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
    }
     
     
  • 相关阅读:
    Spring boot返回值封装与业务异常
    Spring boot日志
    Rocket项目启动
    抉择
    C# 字符转ASCII码,ASCII码转字符 [转一下]
    xcode 学习笔记2:动态添加view
    unrecognized selector sent to instance 0x1b97e0' 问题解决记录
    xcode 学习笔记1:Interface Builder的使用
    xcode 学习笔记4:给WindowBase程序添加view
    xcode 学习笔记3:xcode目录结构
  • 原文地址:https://www.cnblogs.com/changyiqiang/p/5732398.html
Copyright © 2011-2022 走看看