在手機QQ上切換封面主題板塊,以及在某些App Shop中,咱們常常看見Gallery效果的影子,但Gallery以及被一塊兒了,這裏咱們實現使用ViewPager實現Gallery效果
java
佈局文件:android
(注意,根佈局和ViewPager都要使用android:clipChildren="false",表示當顯示內容區域超過控件區域,緩存
是否裁剪內容區域的寬度和高度,以實現遮蓋超出區域,默認是true)ide
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/gallery_viewpager_layout" android:clipChildren="false" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/gallery_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:clipChildren="false" /> </LinearLayout>
代碼文件佈局
public class ViewPagerGallery extends Activity { private ViewPager mGalleryViewPager; private LinearLayout mViewPagerContainer; private int pagerWidth = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.viewpager_gallery); mGalleryViewPager = (ViewPager)findViewById(R.id.gallery_viewpager); mViewPagerContainer = (LinearLayout)findViewById(R.id.gallery_viewpager_layout); pagerWidth = (int) (getResources().getDisplayMetrics().widthPixels*3.0f/5.0f); mGalleryViewPager.measure(0, 0); LayoutParams lp = mGalleryViewPager.getLayoutParams(); if(lp==null) { lp = new LayoutParams(pagerWidth,LayoutParams.MATCH_PARENT); }else{ lp.width = pagerWidth; } mGalleryViewPager.setLayoutParams(lp);//設置頁面寬度爲屏幕的3/5 mGalleryViewPager.setOffscreenPageLimit(4); //設置ViewPager至多緩存4個Pager頁面,防止屢次加載 mGalleryViewPager.setPageMargin(50); //設置Pager之間的間距 mGalleryViewPager.setAdapter(new GalleryPagerAdapter()); mGalleryViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){ @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { super.onPageScrolled(position, positionOffset, positionOffsetPixels); if (mViewPagerContainer != null) { mViewPagerContainer.invalidate(); //更新超出區域頁面,不然會出現頁面緩存,致使頁面效果不佳 } } }); } private class GalleryPagerAdapter extends PagerAdapter { @Override public int getCount() { return 6; } @Override public boolean isViewFromObject(View view, Object obj) { return view==obj; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager)container).removeView((ImageView)object); } @Override public Object instantiateItem(ViewGroup container, int position) { int resId = getResources().getIdentifier("img0".concat(String.valueOf(position)), "drawable", getPackageName()); ImageView imageView = createImageView(resId); ((ViewPager)container).addView(imageView, position); return imageView; } public ImageView createImageView(int resId) { ImageView iv = new ImageView(ViewPagerGallery.this); LayoutParams lp = iv.getLayoutParams(); if(lp==null) { lp = new LayoutParams(pagerWidth, LayoutParams.MATCH_PARENT); }else{ lp.width = pagerWidth; lp.height = LayoutParams.MATCH_PARENT; } iv.setScaleType(ScaleType.FIT_CENTER); iv.setImageResource(resId); iv.setLayoutParams(lp); return iv; } } }
Try doing it;this