我终于弄清楚了我想要它的方式。我终于在使用 ViewPager 时成功地做到了这一点:
画廊适配器:
public class GalleryAdapter extends PagerAdapter {
/**
* The collection of images contained in the view pager.
*/
public Integer[] mImages;
private InternalDatabase dbhelper;
private RuntimeExceptionDao<GalleryImage, Integer> imagesDao;
/**
* Constructor for the adapter. Initializes the images needed.
*/
public GalleryAdapter(Context context, Integer[] galleryImages) {
dbhelper = OpenHelperManager.getHelper(context, InternalDatabase.class);
imagesDao = dbhelper.getRuntimeExceptionDao(GalleryImage.class);
// TODO: create the proper collection to be able to display the images in the collection.
mImages = galleryImages;
}
@Override
public int getCount() {
if (mImages != null) {
if (mImages.length > 0) {
return mImages.length;
}
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Get the context from which the adapter is created.
// Need the context to create new views.
Context context = container.getContext();
// Create a new image view that will contain the image.
ImageView imageView = new ImageView(context);
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(params);
// Scaling the image inside the wrapping view pager.
//imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
// Setting the image.
if (mImages != null) {
if (mImages.length > 0) {
Integer id = mImages[position];
GalleryImage image = imagesDao.queryForId(id);
Bitmap decodedByte = BitmapFactory.decodeByteArray(image.getImage(), 0, image.getImage().length);
imageView.setImageBitmap(decodedByte);
}
}
// Add the image to the view pager.
((ViewPager) container).addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
线性布局视图:
public class CardMiniatureGallery extends AbstractCompoundLinearLayout {
/**
* The viewpager that is used as a miniature gallery.
*/
public ViewPager mGallery;
private Integer[] mIDs;
public CardMiniatureGallery(Context context) {
super(context);
}
public CardMiniatureGallery(Context context, Integer[] images) {
super(context);
mIDs = images;
createLayout(context, mIDs);
}
public CardMiniatureGallery(Context context, AttributeSet attrs) {
super(context, attrs);
createLayout(context, mIDs);
}
public CardMiniatureGallery(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
createLayout(context, mIDs);
}
protected void createLayout(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.card_miniature_gallery, this, true);
}
private void createLayout(Context context, Integer[] ids) {
mGallery = (ViewPager) getChildAt(0);
GalleryAdapter adapter = new GalleryAdapter(context, mIDs);
mGallery.setAdapter(adapter);
mGallery.setOffscreenPageLimit(adapter.getCount());
mGallery.setClipToPadding(false);
int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 43, getResources().getDisplayMetrics());
mGallery.setPageMargin(margin);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="250dp"
android:overScrollMode="never"
android:paddingLeft="85dp"
android:paddingRight="85dp"
android:layout_marginBottom="@dimen/padding_medium" />
有了这些类和我用来从数据库中获取数据的整数数组,我可以根据需要显示图像。现在我只需要弄清楚如何通过查看它必须加载的图像来动态设置 ViewPager 高度。