簡體   English   中英

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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM