带位置指示器的无限viewpager

时间:2014-04-16 14:38:32

标签: android android-viewpager infinite-loop viewpagerindicator

无限循环观看者依赖于一种错觉,其中项目的数量增加到一个任意大的数字,通过模数映射到列表中的索引位置。

这个问题是圆形页面指示符等指标得到任意长的数字计数,因为它们使用主机getCount()的{​​{1}}方法,完全打破了无限循环的错觉

IE中。您有3个要循环的项目,您将pageradapter计数设置为1000,当用户到达项目3并滑动到项目" 4"时,项目1再次显示。但指标显示您在第4项,并且有数百个其他项目可以滑动到。而不是仅仅在3个指标选择之间循环。

有解决方案吗?

2 个答案:

答案 0 :(得分:1)

我不想花时间在新图书馆。因此,我使用圆形指示器(ImageView)为无限的视图编写一个简单的OnPageChangeListener。它运作良好。 AutoScrollViewPager用作无限viewPager。当viewpager滚动页面的一半而不是滚动结束时,指示符会发生变化。

屏幕截图(圈子是图片): enter image description here

用法:

bannerPager.setOnPageChangeListener(new OnPageChangeListenerForInfiniteIndicator(getActivity(), bannerList, bannerPager.getCurrentItem()));

的xml:

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/home_banner_indicator_container_height">

            <LinearLayout
                android:id="@+id/container_home_page_indicator"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="@dimen/home_banner_indicator_container_inner_margin_vertical"
                android:layout_marginTop="@dimen/home_banner_indicator_container_inner_margin_vertical"
                android:orientation="horizontal">

            </LinearLayout>
        </RelativeLayout>

OnPageChangeListenerForInfiniteIndicator:

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kartaca.rbtpicker.R;
import com.kartaca.rbtpicker.model.Banner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by amadeus on 9/18/15.
 */
public class OnPageChangeListenerForInfiniteIndicator implements ViewPager.OnPageChangeListener {
    private Activity activity;
    private List<ImageView> pageIndicatorList = new ArrayList<ImageView>();
    private List<Banner> bannerList;
    private LinearLayout containerIndicator;
    private int viewPagerActivePosition;
    private int positionToUse = 0;
    private int actualPosition;

    public OnPageChangeListenerForInfiniteIndicator(Activity activity, List<Banner> bannerList, int currentItem) {
        this.activity = activity;
        this.bannerList = bannerList;
        this.actualPosition = currentItem;
        this.viewPagerActivePosition = currentItem;
        loadIndicators();
    }

    private void loadIndicators() {
        containerIndicator = (LinearLayout) activity.findViewById(R.id.container_home_page_indicator);
        if (pageIndicatorList.size() < 1) {
            for (Banner banner : bannerList) {
                ImageView imageView = new ImageView(activity);
                imageView.setImageResource(R.drawable.banner_pagination_normal);// normal indicator image
                imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                imageView.setLayoutParams(new ViewGroup.LayoutParams(activity.getResources().getDimensionPixelOffset(R.dimen.home_banner_indicator_width), 
            ViewGroup.LayoutParams.MATCH_PARENT));
                pageIndicatorList.add(imageView);
            }
        }
        containerIndicator.removeAllViews();
        for (int x = 0; x < pageIndicatorList.size(); x++) {
            ImageView imageView = pageIndicatorList.get(x);
            imageView.setImageResource(x == positionToUse ? R.drawable.banner_pagination_active :
         R.drawable.banner_pagination_normal); // active and notactive indicator
            containerIndicator.addView(imageView);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        actualPosition = position;
        int positionToUseOld = positionToUse;
        if (actualPosition < viewPagerActivePosition && positionOffset < 0.5f) {
            positionToUse = actualPosition % bannerList.size();
        } else {
            if (positionOffset > 0.5f) {
                positionToUse = (actualPosition + 1) % bannerList.size();
            } else {
                positionToUse = actualPosition % bannerList.size();
            }
        }
        if (positionToUseOld != positionToUse) {
            loadIndicators();
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == 0) {
            viewPagerActivePosition = actualPosition;
            positionToUse = viewPagerActivePosition % bannerList.size();
            loadIndicators();
        }
    }
}

答案 1 :(得分:0)

看看InfinitePageIndicator project,也许这是你需要的东西。