編輯2:示例代碼已被推送到TouchImageView的主分支。這是一個link to the example activity和一個link to the ExtendedViewPager。
編輯:添加代碼,調整示例鏈接到TouchImageView。注意:您將需要最新的代碼,該代碼當前位於開發分支中。將來,這將包含在v1.2.0中。如果TouchImageView覆蓋canScrollHorizontally,你知道你有最新的代碼。
第1步:擴展ViewPager並覆蓋canScroll來調用canScrollHorizontallyFroyo。
public class ExtendedViewPager extends ViewPager {
public ExtendedViewPager(Context context) {
super(context);
}
public ExtendedViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof TouchImageView) {
return ((TouchImageView) v).canScrollHorizontallyFroyo(-dx);
} else {
return super.canScroll(v, checkV, dx, x, y);
}
}
}
步驟2:修改TouchImageView通過添加canScrollHorizontallyFroyo:
public boolean canScrollHorizontallyFroyo(int direction) {
return canScrollHorizontally(direction);
}
步驟3:你的活動
public class TouchImageViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ExtendedViewPager mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
setContentView(mViewPager);
mViewPager.setAdapter(new TouchImageAdapter());
}
static class TouchImageAdapter extends PagerAdapter {
private static int[] images = { R.drawable.img1, R.drawable.img2, R.drawable.img3 };
@Override
public int getCount() {
return images.length;
}
@Override
public View instantiateItem(ViewGroup container, int position) {
TouchImageView img = new TouchImageView(container.getContext());
img.setImageResource(images[position]);
container.addView(img, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
return img;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
步驟4: main.xml中
<com.example.touch.ExtendedViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
TouchImageView實際上是我的項目。我目前在dev branch中有a fix與ViewPagers集成,將在即將發佈的版本中推送給Master。不幸的是,由於蜂窩和以前不叫canScrollHorizontally
,此修補程序僅適用於API 14及更高版本。如果您需要支持較舊的API,則需要在ViewPager中實施解決方法。 Here is an example.
這個解決方法對我來說不起作用。:(如果我用更高的速度滾動,它會將我移動到下一張幻燈片。 –
@AvtarGuleria我的編輯幫助中包含了另一個解決方法嗎?我知道我已經成功地僱用了它在過去 –
沒有第二個解決方法也沒有爲我工作每當我水平滾動,它只是將我移動到第二張幻燈片但如果我滾動垂直或以其他角度水平以外的其他角度..so,再次水平它不工作... –