Gallery + GridView 實現圖片與小圓點同步變化

來源:互聯網
上載者:User

直接上代碼吧:

添加簡單的自動播放功能:

        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;                }

 

源碼下載

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.