• Viewpager制作自动左右循环滑动


    1、布局文件,framlaout包含两层:viewpager+小点点

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="120dp" >
            <android.support.v4.view.ViewPager
                android:id="@+id/Pager"
                android:layout_width="fill_parent"
                android:layout_height="120dp"
                android:layout_gravity="center"
                android:flipInterval="30"
                android:persistentDrawingCache="animation" />
            <LinearLayout
                android:id="@+id/viewGroup"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:orientation="horizontal" >
            </LinearLayout>
    
        </FrameLayout>
    
    </RelativeLayout>

    2、主代码

        // Views动态数组,装载图片,views.add(imaV);
        private List<View> views;
        // 滑动控件
        private ViewPager pager;
         // 装载小点指示器
        private ViewGroup group;
    .................................
            initViewPager();
            setpagerListener();
    ................................
    private void initViewPager() {
            // TODO Auto-generated method stub
            pager = (ViewPager) view.findViewById(R.id.Pager);
            group = (ViewGroup) view.findViewById(R.id.viewGroup);
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
            ImageView imaV;
            int[] ids = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3 };
            views = new ArrayList<View>();
            for (int i = 0; i < ids.length; i++) {
                imaV = new ImageView(getActivity());
                imaV.setImageResource(ids[i]);
    
                imaV.setLayoutParams(params);
                imaV.setScaleType(ScaleType.FIT_XY);
                views.add(imaV);
            }
            for (int i = 0; i < ids.length; i++) {
                imaV = new ImageView(getActivity());
                imaV.setLayoutParams(new LayoutParams(12, 12));
                if (i == 0) {
                    imaV.setBackgroundResource(R.drawable.page_indicator_focused);
                } else {
                    imaV.setBackgroundResource(R.drawable.page_indicator_unfocused);
                }
                group.addView(imaV);
            }
            pager.setAdapter(new MyAdapter(views));
            //当前页设置为很大才可以左滑
            pager.setCurrentItem((views.size()) * 100);
        }
    
        /**
         * 设置选中的tip的背景
         * 
         * @param selectItems
         */
        private void setImageBackground(int selectItems) {
            for (int i = 0; i < group.getChildCount(); i++) {
                if (i == selectItems) {
                    group.getChildAt(i).setBackgroundResource(
                            R.drawable.page_indicator_focused);
                } else {
                    group.getChildAt(i).setBackgroundResource(
                            R.drawable.page_indicator_unfocused);
                }
            }
        }
    .............................................
    private void setpagerListener() {
            // TODO Auto-generated method stub
            pager.setOnPageChangeListener(new OnPageChangeListener() {
                /**
                 * onPageSelected:每次切换页面就会执行 在此实现圆点指示器以及在最后页面显示button
                 */
                @Override
                public void onPageSelected(int arg0) {
                    // TODO Auto-generated method stub
                    setImageBackground(arg0 % views.size());
                }
    
                /**
                 * 当页面在滑动的时候会调用此方法,在滑动被停止之前,此方法回得到调用。 其中三个参数的含义分别为:
                 * 
                 * arg0 :当前页面,及你点击滑动的页面
                 * 
                 * arg1:当前页面偏移的百分比
                 * 
                 * arg2:当前页面偏移的像素位置
                 */
                @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
                }
            });
    
        }

    3、适配器

    import java.util.List;
    
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.util.Log;
    import android.view.View;
    
    public class MyAdapter extends PagerAdapter {
    
        private List<View> views;
    
        public MyAdapter(List<View> views) {
            this.views = views;
        }
    
        // destroyItem,getCount,instantiateItem实现循环滑动
        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            // ((ViewPager) arg0).removeView(views.get(arg1 % views.size()));
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see android.support.v4.view.PagerAdapter#getCount() 这样才可以一直滑
         */
        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see
         * android.support.v4.view.PagerAdapter#instantiateItem(android.view.View,
         * int) 如果出现错误不会fc,而是在catch里面处理,实现左滑
         */
        @Override
        public Object instantiateItem(View arg0, int arg1) {
            Log.e("tag", "instantiateItem = " + arg1);
            try {
                ((ViewPager) arg0).addView(views.get(arg1 % views.size()), 0);
            } catch (Exception e) {
                // handler something
            }
            return views.get(arg1 % views.size());
        }
    
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return false;
        }
    
    }

    4、实现自动切换图片.

        ScheduledExecutorService scheduledExecutorService;
    ................................
        @Override
        public void onStart() {
            // TODO Auto-generated method stub
            scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
            // 当Activity显示出来后,每6秒切换一次图片
            // scheduleAtFixedRate安排所提交的Runnable任务按指定的间隔重复执行
            scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 1, 6,
                    TimeUnit.SECONDS);
            super.onStart();
        }
    
        @Override
        public void onStop() {
            // 当Activity不可见的时候停止切换
            scheduledExecutorService.shutdown();
            super.onStop();
        }

    Done!

  • 相关阅读:
    设计模式---工厂模式和抽象工厂模式
    设计模式---简单工厂模式
    设计模式---设计模式的分类及六大原则
    分布式---Raft算法
    分布式---Paxos算法
    分布式---CAP和BASE理论
    分布式---分布式事务
    分布式---分布式锁
    css
    react生命周期
  • 原文地址:https://www.cnblogs.com/xingyyy/p/3696142.html
Copyright © 2020-2023  润新知