如何在缩放其中一个viewpager图片时锁定viewpager

时间:2013-08-26 20:33:40

标签: android android-fragments android-viewpager

我有一个带有数组的viewPage有3张图片,我想锁定viewPager当缩放其中一个导致当我在右侧滚动时出现下一张照片! 这是我的代码

enter code here
 public class ExploreImageAdapter  extends PagerAdapter {
Context context;
boolean touchEvent;
private int[] exploreImages = new int[] {
    R.drawable.carfourele,
    R.drawable.carfourele2,
    R.drawable.carfourele3,
    R.drawable.carfourele4
};

private LayoutInflater inflater;
ExploreImageAdapter(Context context){
    this.context=context;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i=1 ; i<=exploreImages.length ; i++){

}     }

@Override
public int getCount() {
  return exploreImages.length;
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
               View topImageLayout = inflater.inflate(R.layout.item_pager_images, container, false);
          int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
        TouchImageView  imageView= (TouchImageView)topImageLayout.findViewById(R.id.image);
        imageView.setFocusable(true);
                imageView.setPadding(padding, padding, padding, padding);
          imageView.setImageResource(exploreImages[position]);
          imageView.setMaxZoom(4f);
          ((ViewPager) container).addView(topImageLayout, 0);
              return topImageLayout;

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
  ((ViewPager) container).removeView((View) object);
}

}

1 个答案:

答案 0 :(得分:2)

执行此操作的最佳方法是创建自定义ViewPager并覆盖onTouchEvent()onTouchInterceptEvent()方法。例如,

public class LockedViewPager extends ViewPager {
    private boolean pagingEnabled;

    public LockedViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        pagingEnabled = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (pagingEnabled) {
           return super.onTouchEvent(event);
        }
        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (pagingEnabled) {
           return super.onTouchEvent(event);
        }
        return false;
    }

    /**
     * @param pagingEnabled
     *      if true, ViewPager acts as normal
     *      if false, ViewPager cannot be scrolled by a user's touch
     */
    public void setPagingEnabled(boolean pagingEnabled) {
        this.pagingEnabled = pagingEnabled;
    }
}

每当您想阻止用户滚动时,只需致电lockedViewPager.setPagingEnabled(false);
然后当你想重新开启时,lockedViewPager.setPagingEnabled(true);

为了简单解释一下,onTouchEvent方法通常是处理分页行为的入口点,所以我们只是在开始时只是在没有启用分页时就停止它。此外,如果方法处理了事件,则通常返回true,因此我们必须返回false以指示我们已忽略它。

为了将其合并到您的应用中,首先将LockedViewPager添加到您的活动的XML中,但不要忘记your_appyour_package的名称来创建LockedViewPager类的路径(如果你使用的是Android Studio,开始输入LockedViewPager会自动识别并为你建议路径:

<com.your_app.your_package.LockedViewPager
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/lockedViewPager"/>

然后,你会在你的活动中使用它:

public class MyActivity {

    private LockedViewPager lockedViewPager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // other activity setup, etc.

        lockedViewPager = (LockedViewPager) findViewById(R.id.lockedViewPager);

        // set up pager, set its adapter and any listeners, etc.

    }

    /*
     * Call this method once you've zoomed, for example
     */
    public void onZoom() {
        lockedViewPager.setPagingEnabled(false);
    }

    /*
     * Don't forget to call this method at some time later to re-enable the paging
     */
    public void resumePaging() {
        lockedViewPager.setPagingEnabled(true);
    }
}