前言
只為您設(shè)計(jì)更接底氣、較有營(yíng)銷(xiāo)力的好網(wǎng)站,將營(yíng)銷(xiāo)策劃與網(wǎng)頁(yè)設(shè)計(jì)互相結(jié)合的專(zhuān)業(yè)機(jī)構(gòu),全網(wǎng)整合營(yíng)銷(xiāo)推廣公司中較早掌握H5響應(yīng)式網(wǎng)站技術(shù)的機(jī)構(gòu)。一個(gè)好的品牌網(wǎng)站制作,不能只是一張名片,茫茫網(wǎng)海,想要快速吸引到您客戶(hù)的眼球,必須全方位的展現(xiàn)出企業(yè)突出的優(yōu)勢(shì),以求達(dá)到主動(dòng)營(yíng)銷(xiāo)的效果,最終促成成交!網(wǎng)上有很多使用ViewPager實(shí)現(xiàn)左右滑動(dòng)這一效果的資料,這些資料大多數(shù)都是將PagerAdapter中g(shù)etCount()方法的返回值設(shè)為Integer.MAX_VALUE使用戶(hù)看不到邊界,然后在instantiateItem()方法中通過(guò)position%(要循環(huán)顯示的數(shù)據(jù)集的長(zhǎng)度)的方式取得對(duì)應(yīng)的數(shù)據(jù)集。這樣雖然可以做到無(wú)限循環(huán),但是會(huì)有兩個(gè)弊端:首先會(huì)創(chuàng)建大量對(duì)象,容易引起內(nèi)存溢出(循環(huán)加載圖片)從而影響性能;其次從第一頁(yè)向右滑動(dòng)的時(shí)候是無(wú)法滑動(dòng)的。
今天給大家分享的是另一種實(shí)現(xiàn)方式:創(chuàng)建三個(gè)圖片視圖放入ViewPager中默認(rèn)選中第二頁(yè),在接下來(lái)的滑動(dòng)中每次滑動(dòng)結(jié)束之后都將當(dāng)前頁(yè)碼設(shè)置為第二頁(yè),然后通過(guò)判斷是向左向右滑動(dòng)來(lái)設(shè)置視圖中的數(shù)據(jù)集,這樣就可以實(shí)現(xiàn)無(wú)限循環(huán)了,而且只會(huì)創(chuàng)建三個(gè)圖片視圖對(duì)象。
示例源碼
package com.viewpager; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.app.Activity; public class MainActivity01 extends Activity implements OnPageChangeListener { private ViewPager viewPager; static final int arrays[] = { R.drawable.guide1, R.drawable.guide2, R.drawable.guide3, R.drawable.guide4, R.drawable.guide5 }; private List<ImageView> views; private int currentPage = 0; private ImageView imageView; private MyViewPagerAdapter viewPagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initWidget(); } private void initWidget() { viewPager = (ViewPager) findViewById(R.id.viewpager); views = new ArrayList<ImageView>(); for (int i = 0; i < 3; i++) { imageView = new ImageView(this); imageView.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); views.add(imageView); } initImageData(); viewPagerAdapter = new MyViewPagerAdapter(); viewPager.setAdapter(viewPagerAdapter); viewPager.setCurrentItem(1); viewPager.setOnPageChangeListener(this); } private void initImageData() { for (int i = 0; i < 3; i++) { imageView = views.get(i); if (i == 0) { imageView.setImageResource(arrays[arrays.length - 1]); } else { imageView.setImageResource(arrays[i - 1]); } } } class MyViewPagerAdapter extends PagerAdapter { @Override public int getCount() { // TODO Auto-generated method stub return views.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { imageView = views.get(position); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView((View) object); } } // 當(dāng)滑動(dòng)狀態(tài)改變時(shí)調(diào)用 @Override public void onPageScrollStateChanged(int state) { // TODO Auto-generated method stub System.out.println("--onPageScrollStateChanged--state--:" + state); switch (state) { // 在滾動(dòng)完成后 case ViewPager.SCROLL_STATE_IDLE: int currentItem = viewPager.getCurrentItem(); System.out.println("--currentItem--00--:" + currentItem); System.out.println("--currentPage--00--:" + currentPage); if (viewPager.getCurrentItem() == 1) { // 如果位置沒(méi)有變終止循環(huán) break; } if (viewPager.getCurrentItem() > 1) { currentPage++; } else { currentPage--; } System.out.println("--currentPage--11--:" + currentPage); if (currentPage == arrays.length) { currentPage = 0; } if (currentPage == -1) { currentPage = arrays.length - 1; } System.out.println("--currentPage--22--:" + currentPage); if (currentPage == 0) { views.get(0).setImageResource(arrays[arrays.length - 1]); } else { views.get(0).setImageResource(arrays[currentPage - 1]); } views.get(1).setImageResource(arrays[currentPage]); if (currentPage == arrays.length - 1) { views.get(2).setImageResource(arrays[0]); } else { views.get(2).setImageResource(arrays[currentPage + 1]); } viewPager.setCurrentItem(1, false); currentItem = viewPager.getCurrentItem(); System.out.println("--currentItem--11--:" + currentItem); break; } } // 當(dāng)當(dāng)前頁(yè)面被滑動(dòng)時(shí)調(diào)用 @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // TODO Auto-generated method stub // System.out.println("--onPageScrolled--position--:" + position); } // 當(dāng)新的頁(yè)面被選中時(shí)調(diào)用 @Override public void onPageSelected(int position) { // TODO Auto-generated method stub System.out.println("--onPageSelected--position--:" + position); } }
分享題目:Android使用ViewPager實(shí)現(xiàn)左右無(wú)限滑動(dòng)-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://www.rwnh.cn/article8/epgip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、全網(wǎng)營(yíng)銷(xiāo)推廣、云服務(wù)器、微信公眾號(hào)、網(wǎng)站維護(hù)、軟件開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
營(yíng)銷(xiāo)型網(wǎng)站建設(shè)知識(shí)