1

我有一个带有数组的 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);
}

}

4

1 回答 1

2

最好的方法是创建一个自定义 ViewPager 并覆盖onTouchEvent()andonTouchInterceptEvent()方法。例如,

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 中,但不要忘记创建 LockedViewPager 类的名称your_appyour_package路径(如果您使用的是 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);
    }
}
于 2013-08-27T03:50:49.043 回答