viewPager 实现左右都漏出item 的例子

我们需要实现viewPager 实现左右都漏出item 的例子,这个可以很方便的扩展,以满足不同的需求,
imageViews可以添加同样的布局,也可以添加不同的布局。以下有两种方式来实现,
1.设置
 
  
mPager.setPageMargin(-60);
2.重写
 
  
PagerAdapter 的getPageWidth方法

 
  
主程序代码: 
  

package com.yongche.demoproject.ui;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.yongche.demoproject.R;

public class SlideActivity extends Activity {
   private static final int NUM_PAGES = 5;
   private ViewPager mPager;
   private MyAdViewPagerAdapter mPagerAdapter;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.slide_activity);
      mPager = (ViewPager) findViewById(R.id.pager);
      ArrayList imageViews= new ArrayList();
      for(int i = 0;i<6;i++){
         ImageView imageView = new ImageView(SlideActivity.this);
         imageView.setPadding(50, 0, 50, 0);
         imageView.setImageResource(R.drawable.slide_01);
         imageViews.add(imageView);
      }
      mPager.setBackgroundResource(R.drawable.link);
      mPagerAdapter = new MyAdViewPagerAdapter(SlideActivity.this,imageViews);
      mPager.setAdapter(mPagerAdapter);
      mPager.setPageMargin(-60);
   }



   public class MyAdViewPagerAdapter extends PagerAdapter {
//    @Override
//    public float getPageWidth(int position) {
//       return 0.9f;
//    }

      private ArrayList pagerList;
      private Context ctx;

      public MyAdViewPagerAdapter(Context ctx, ArrayList pagerList) {
         this.ctx = ctx;
         this.pagerList = pagerList;
      }

      @Override
      public int getCount() {
         return pagerList.size();
      }

      @Override
      public Object instantiateItem(View container, int position) {
         ((ViewPager) container).addView(pagerList.get(position));
         return pagerList.get(position);
      }

      @Override
      public void destroyItem(View container, int position, Object view) {
         ((ViewPager) container).removeView(pagerList.get(position));
      }

      @Override
      public boolean isViewFromObject(View view, Object object) {
         return view == object;
      }

      @Override
      public Parcelable saveState() {
         return null;
      }

      @Override
      public void restoreState(Parcelable arg0, ClassLoader arg1) {
      }

      @Override
      public void startUpdate(View arg0) {
      }

      @Override
      public void finishUpdate(View arg0) {
      }
   }


}


布局文件:
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:paddingRight="30px"
    android:paddingLeft="30px"/>




你可能感兴趣的:(android,ViewPager)