Gallery + GridView 实现图片与小圆点同步变化

直接上代码吧:

添加简单的自动播放功能:

        private void autoPlay(){

                //设置间隔5秒自动播放的功能

                new Thread(new Runnable(){

 

                        @Override

                        public void run() {

                                // TODO Auto-generated method stub

                                while(isAlive_){

                                        currentIndex_ = currentIndex_ % infoIds.length;

                                        infoshow_gallery.post(new Runnable(){

 

                                                @Override

                                                public void run() {

                                                        // TODO Auto-generated method stub

                                                        infoshow_gallery.setSelection(currentIndex_);

                                                }

                                                 

                                        });

                                        //更新时间间隔 5秒

                                        try {

                                                Thread.sleep(5000);

                                        } catch (InterruptedException e) {

                                                // TODO Auto-generated catch block

                                                e.printStackTrace();

                                        }

                                        currentIndex_++;

                                }

                        }

                         

                }).start();

        }

主要代码:

                infoshow_gallery.setOnItemSelectedListener(new Gallery.OnItemSelectedListener(){



                        @Override

                        public void onItemSelected(AdapterView<?> parent, View view,

                                        int position, long id) {

                                // TODO Auto-generated method stub

                                //设置当前选中的Index

                                currentIndex_ = position;

                                //改变GridView显示

                                gridviewAdapter.notifyDataSetInvalidated();

                        }



                        @Override

                        public void onNothingSelected(AdapterView<?> arg0) {

                                // TODO Auto-generated method stub

                                

                        }

                        

                });

                // Gallery OnItemClick

                infoshow_gridview.setOnItemClickListener(new OnItemClickListener() {



                        @Override

                        public void onItemClick(AdapterView<?> parent, View view, int position,

                                        long id) {

                                // TODO Auto-generated method stub

                                //设置当前选中的Index

                                currentIndex_ = position;

                                //改变GridView显示

                                gridviewAdapter.notifyDataSetInvalidated();

                                //改变Gallery显示

                                infoshow_gallery.setSelection(currentIndex_);

                        }

                        

                });

GridView适配器:

                public View getView(int position, View convertView, ViewGroup parent) {

                        // TODO Auto-generated method stub

                        ImageView img = new ImageView(context_);

                        img.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, 

                                        GridView.LayoutParams.WRAP_CONTENT));

                        //红球表示选中状态

                        if(position == currentIndex_){

                                img.setImageResource(thumbIds[0]);

                        }else{

                                img.setImageResource(thumbIds[1]);

                        }

                        img.setScaleType(ScaleType.FIT_CENTER);

                        return img;

                }

Gallery适配器:

        public View getView(int position, View convertView, ViewGroup parent) {

                        // TODO Auto-generated method stub

                        ImageView img = new ImageView(context_);

                        //此处每个ImageView都要占全部空间

                        img.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, 

                                        Gallery.LayoutParams.FILL_PARENT));

                        img.setImageResource(infoIds[position]);

                        img.setScaleType(ScaleType.FIT_XY);

                        return img;

                }

效果图:

Gallery + GridView 实现图片与小圆点同步变化

 

源码下载

 

你可能感兴趣的:(GridView)