• Android开发之ViewPager图片无限轮播


    主代码:

    public class Showactivity extends Activity implements OnPageChangeListener {
    
        private List<Jsonimage> listimage;
        private ViewPager vpager;
        private LinearLayout llayout;
        private int count = 0;
        private BitmapUtils bitmapUtils;
        private List<ImageView> imagelist;
        private int lastindex = 0;
        @SuppressLint("HandlerLeak")
        Handler h = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                vpager.setCurrentItem(count);
            }
        };
    
        @SuppressWarnings("unchecked")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.showactivity);
    
            vpager = (ViewPager) findViewById(R.id.viewpager);
            llayout = (LinearLayout) findViewById(R.id.ll_layout);
            bitmapUtils = new BitmapUtils(getApplicationContext());
            imagelist = new ArrayList<ImageView>();
            Intent intent = getIntent();
            listimage = (List<Jsonimage>) intent.getSerializableExtra("ls");
            // Log.i("TAG", listimage.toString());
    
            for (int i = 0; i < listimage.size(); i++) {
                ImageView imageView = new ImageView(getApplicationContext());
                bitmapUtils.display(imageView, listimage.get(i).getHead_img());
                imagelist.add(imageView);
    
                ImageView imageView2 = new ImageView(getApplicationContext());
                imageView2.setBackgroundResource(R.drawable.shape);
                LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
                layoutParams.leftMargin = 15;
    
                imageView2.setLayoutParams(layoutParams);
    
                llayout.addView(imageView2);
    
                if (i == 0) {
                    imageView2.setEnabled(true);
    
                } else {
                    imageView2.setEnabled(false);
                }
    
            }
    
            vpager.setAdapter(new Mypageadapter(getApplicationContext(), imagelist));
    
            Timer t = new Timer();
            t.schedule(new TimerTask() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    count++;
                    if (count == listimage.size()) {
                        count = 0;
                    }
                    h.sendEmptyMessage(0);
                }
            }, 1000, 1000);
            vpager.setOnPageChangeListener(this);
    
        }
    
        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            int index = arg0 % imagelist.size();
            // 将当前指示灯改为true
            llayout.getChildAt(index).setEnabled(true);
            llayout.getChildAt(lastindex).setEnabled(false);
            lastindex = index;
        }
    }

    自定义PagerAdapter:

    public class Mypageadapter extends PagerAdapter {
        Context context;
        List<ImageView> list;
    
        public Mypageadapter(Context context, List<ImageView> list) {
            super();
            this.context = context;
            this.list = list;
        }
    
        // 得到图片的数量
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }
    
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return (arg0 == arg1);
        }
    
        // 将图片添加到View
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }
    
        // 销毁
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // 销毁对应位置上的Object
            // super.destroyItem(container, position, object);
            container.removeView(list.get(position));
        }
    }

     

    在res中创建drawble文件

    dot_focus.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
        android:shape="oval">
       <size android:width="10dp" android:height="10dp"/>
        <solid android:color="#FF0000"/>
    </shape>
    
    dot_nomal.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval" >
    
        <size
            android:height="10dp"
            android:width="10dp" />
    
        <solid android:color="#808080" />
    
    </shape>
    
    
    shape.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/dot_focus" android:state_enabled="true"></item>
        <item android:drawable="@drawable/dot_nomal" android:state_enabled="false"></item>
    
    </selector>

    XML布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        
          <LinearLayout
            android:id="@+id/ll_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_margin="5dp" >
        </LinearLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/ll_layout" >
        </android.support.v4.view.ViewPager>
    </RelativeLayout>
  • 相关阅读:
    设置了透明以后,会有严重残影
    “真正的工作不是说的天花乱坠”,Torvalds 说, “而是在于细节”(Torvalds 认为成功的项目都是99%的汗水和1%的创新)
    iOS和Android使用MQTT协议实现消息推送和即时通讯(替代XMPP,已经有TMS MQTT)
    avalonjs1.5 入门教程
    Grunt 之 Connect
    性能
    JQUERY省、市、县城市联动选择
    Meteor全栈开发平台
    微型工作流引擎设计
    微型工作流引擎-功能设计
  • 原文地址:https://www.cnblogs.com/bokeyuan007/p/5226300.html
Copyright © 2020-2023  润新知