我想知道如何让HorizontalScrollView像主屏幕一样“捕捉”,这意味着我在我的HorizontalScrollView中有两个页面,我可以在每个页面之间滑动。
你有两个选项。
要么使用ViewFlipper,要么使用Android Gallery。
我建议你使用Android Gallery,因为它更易于控制和实现。
在developer.android.com网站上的默认Android Gallery示例中,适配器使用了getView()方法返回一个ImageView。你可以通过修改代码,使其返回一个类似以下代码的充气XML布局。
class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return 5;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.ownview, null);
return convertView;
}
}
Android中没有内置的视图/小部件来实现这一点,但有一个开源项目包括一个优秀的实现,称为SwipeView,我已经在许多项目中成功使用。它是一个自定义视图,您可以以类似于许多其他ViewGroup实现的方式使用:
你可能正在寻找android.support.v4.view.ViewPager
。
这个东西有点用适配器和片段,使生活变得相当轻松。首先,在这里看一下:
http://android-developers.blogspot.co.nz/2011/08/horizontal-view-swiping-with-viewpager.html