包含两个项目的 VIewPager - Android

VIewPager with two items - Android

我有一个 ViewPager,它正在工作。它在每一页上显示一个图像和一个文本。

这是我的适配器:

private class SuggestionsAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {

    private LayoutInflater inflater;
    private ViewPager pager;

    public SuggestionsAdapter(ViewPager pager) {
        this.pager = pager;
    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            pager.setCurrentItem(gallery.size(), true);
        } else if (position == gallery.size() + 1) {
            pager.setCurrentItem(1, true);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public int getCount() {
        return gallery.size();
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.one_suggestion_layout, container, false);

        ImageView ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
        ivImage.setImageBitmap(gallery.get(position));

        // Add viewpager_item.xml to ViewPager
        container.addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        container.removeView((RelativeLayout) object);
    }
}

而且我在我的代码中使用的非常简单:

ViewPager vpSuggestions = (ViewPager) findViewById(R.id.vpSuggestions);
SuggestionsAdapter sugAdapter = new SuggestionsAdapter(vpSuggestions);
vpSuggestions.setAdapter(sugAdapter);

这是滑块的 mu xml:

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="7">

        <TextView
            android:gravity="center"
            android:background="@color/white"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/suggestions"
            android:id="@+id/textView"
            android:layout_gravity="center_horizontal" />

        <android.support.v4.view.ViewPager
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:id="@+id/vpSuggestions" />

    </LinearLayout>

这是一页 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ivImage1"
        android:adjustViewBounds="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="0dp"
        android:src="@drawable/cat_321_sofa" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:gravity="center"
        android:id="@+id/tvTitle1"
        android:padding="10dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/ivImage"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</LinearLayout>

因为分页器的尺寸是矩形(高度小,宽度大)我想在一页中显示两个图像。

寻呼机现在看起来像这个图像:

我想成为这样的形象:

我该怎么做?

解决方法是重写getPageWidth(),除以2

    @Override
    public float getPageWidth(int position) {
        return (super.getPageWidth(position) / 2);
    }