ViewPager无限自动轮播小圆点+网络加载图片+活动倒计时

 代码;
//网络请求的图片
private String[] image={"http://l2.51fanli.net//tuan//images//1//5806eac956808.jpg","http://l2.51fanli.net//tuan//images//b//580991bb30560.jpg","http://l0.51fanli.net//tuan//images//b//58115f2593dc3.jpg","http://l2.51fanli.net//tuan//images//0//57923840b054d.jpg","http://l2.51fanli.net//tuan//images//e//58101e11ab164.jpg"};
    private ViewPager vp;
    //小圆点的数组
    private  ImageView[] point=new ImageView[5];
    //gridview需要加载的图片
    private List<Integer> list=new ArrayList<Integer>();
    private  int time=5;
    private TextView tvtime1,tvtime3,tvtime2;
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            int item = vp.getCurrentItem();
            item++;
            vp.setCurrentItem(item);
            time--;
            String formatLongToTimeStr = formatLongToTimeStr(time);
            String[] split = formatLongToTimeStr.split("");
            for (int i = 0; i < split.length; i++) {
                if(i==0){
                    tvtime1.setText(split[0]);
                }
                if(i==1){
                    tvtime2.setText(split[1]);
                }
                if(i==2){
                    tvtime3.setText(split[2]);
                }

            }
            if(time>0){
                handler.postDelayed(this, 1000);
            }else{
               time=0+1;
                handler.postDelayed(this, 1000);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //获取小圆点的Id
        point[0]=(ImageView)findViewById(R.id.point1);
        point[1]=(ImageView)findViewById(R.id.point2);
        point[2]=(ImageView)findViewById(R.id.point3);
        point[3]=(ImageView)findViewById(R.id.point4);
        point[4]=(ImageView)findViewById(R.id.point5);
        GridView gv= (GridView) findViewById(R.id.gdv);
        for (int i = 0; i <20 ; i++) {
            list.add(R.drawable.a1);
            list.add(R.drawable.a2);
            list.add(R.drawable.a3);
            list.add(R.drawable.a4);
            list.add(R.drawable.a5);
        }
        Myadapter gvadapter=new Myadapter(this,list);
        gv.setAdapter(gvadapter);
    }

    private void initView() {
        tvtime1= (TextView) findViewById(R.id.tvtime1);
        tvtime2= (TextView) findViewById(R.id.tvtime2);
        tvtime3= (TextView) findViewById(R.id.tvtime3);
        vp = (ViewPager) findViewById(R.id.vp);
        vp.setAdapter(new ImageAdapter());
        vp.setCurrentItem(3000);
        //添加页面状态监听
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
           updatePoint(position%image.length);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        handler.postDelayed(runnable, 1000);
    }

    private void updatePoint(int position) {
        //先把所有点初始化原来的样式
        for(ImageView image:point){
            image.setImageResource(R.drawable.nor);
        }
        point[position].setImageResource(R.drawable.sel);
    }
            //适配器
    class ImageAdapter extends PagerAdapter{
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //加载图片
            ImageView imag=new ImageView(MainActivity.this);
            Glide.with(MainActivity.this).load(image[position%image.length]).placeholder(R.drawable.zh).into(imag);
            container.addView(imag);
            return imag;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

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

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
    }
    //时间转换方法
    public  String formatLongToTimeStr(int l) {
        int hour = 0;
        int minute = 0;
        int second = 0;
        second = l ;
        //当秒大于60转换成分钟,零头转换成秒
        if (second > 60) {
            minute = second / 60;         //取整
            second = second % 60;         //取余
        }
        //当分钟大于60转换成小时,零头转换成分钟
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        //拼接字符串
        String strtime = hour+""+minute+""+second;
        return strtime;

    }

    @Override
    protected void onStop() {
        super.onStop();
            //防止内存泄漏
        handler.removeCallbacksAndMessages(null);

    }
 布局xml:
  
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="week2.com.example.viewpagernetpicture.MainActivity">

    <LinearLayout
        android:id="@+id/llt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/vp"
        android:gravity="center">
        <ImageView
            android:id="@+id/point1"
            android:tag="0"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginRight="5dp"/>
        <ImageView
            android:id="@+id/point2"
            android:tag="1"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginRight="5dp"/>
        <ImageView
            android:id="@+id/point3"
            android:tag="2"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginRight="5dp"/>
        <ImageView
            android:id="@+id/point4"
            android:tag="3"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginRight="5dp"/>
        <ImageView
            android:id="@+id/point5"
            android:tag="4"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginRight="5dp"/>
    </LinearLayout
        >

    <TextView
        android:text="京东秒杀"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="17dp"
        android:textSize="15sp"
        android:textColor="#f00"
        android:id="@+id/textView" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:id="@+id/textView3"
        android:layout_toEndOf="@+id/textView2"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tvtime1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:background="#3B3B3B"
            android:text="02"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tvtime2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:text="59"
            android:textColor="#FFFFFF"
            android:background="#3B3B3B"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tvtime3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:background="#3B3B3B"
            android:textColor="#FFFFFF"
            android:text="59"
            android:textSize="15sp" />

    </LinearLayout>

    <TextView
        android:text="20点场"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:textSize="15sp"
        android:layout_alignBottom="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:layout_marginStart="15dp" />

    <TextView
        android:text="海美迪直播玩电视"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textColor="#f00"
        android:id="@+id/textView4"
        android:layout_marginStart="55dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"/>

    <GridView
        android:id="@+id/gdv"
        android:layout_width="match_parent"
        android:layout_height="270dp"
        android:numColumns="5"
        android:layout_above="@+id/textView"
        android:layout_below="@id/llt"
        android:layout_alignParentStart="true">
    </GridView>

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">
    </android.support.v4.view.ViewPager>

</RelativeLayout>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现ViewPager的自动轮播,你可以按照以下步骤进行操作: 1. 首先,在布局文件中添加ViewPager控件,并设置其相关属性,如布局宽高、指示器等。 2. 在代码中,创建一个Handler对象和一个Runnable对象,用于实现自动轮播的定时任务。 ```java private ViewPager viewPager; private int currentPage = 0; private Timer timer; private final long DELAY_MS = 500; // 延迟时间 private final long PERIOD_MS = 3000; // 滚动间隔时间 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = findViewById(R.id.viewPager); // 设置ViewPager的Adapter以及其他属性 final Handler handler = new Handler(); final Runnable runnable = new Runnable() { public void run() { if (currentPage == NUM_PAGES) { currentPage = 0; } viewPager.setCurrentItem(currentPage++, true); } }; timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { handler.post(runnable); } }, DELAY_MS, PERIOD_MS); } ``` 3. 在Activity的`onDestroy`方法中,取消定时任务,避免内存泄漏。 ```java @Override protected void onDestroy() { super.onDestroy(); if (timer != null) { timer.cancel(); timer = null; } } ``` 这样,ViewPager就会自动进行轮播了。注意,上述代码中的`NUM_PAGES`是ViewPager中页面的数量,你需要根据实际情况进行替换。另外,你还可以根据需求添加页面切换的动画效果、手势滑动等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值