如何实现ViewPagerIndicator标题和圆圈

时间:2016-04-22 11:15:08

标签: android android-viewpager android-custom-view viewpagerindicator

如何实现像SwiftPagingNav iOS Library

这样的ViewPagerIndicator
  • 仅显示当前页面的标题
  • 当滑动显示当前页面和下一页或上一页

现在我可以显示标题指示符和圆圈指示符,但现在它显示2或3页面标题,我只想要当前页面和上一个或下一个滑动

images

抱歉英语不好

1 个答案:

答案 0 :(得分:0)

使用以下代码根据需要设置View Pager Indicator:

 /*
* Function to set Indicator to ViewPager.
* */
public void setIndicator() {
    yourViewPager = (ViewPager) findViewById(R.id.yourViewPager);
    yourAdapter adpt = new yourAdapter(YourActivity.this);
    yourViewPager.setAdapter(adpt);
    count_layout = (LinearLayout) findViewById(R.id.image_count);
    count = yourArrayList.size();
    page_text = new ImageView[count];
    for (int i = 0; i < count; i++) {
        page_text[i] = new ImageView(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(5, 0, 5, 0);
        page_text[i].setLayoutParams(lp);
        page_text[i].setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pagination_default));
        count_layout.addView(page_text[i]);
    }
    yourViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            try {
                for (int i = 0; i < count; i++) {
                    page_text[i]
                            .setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pagination_default));
                }

                page_text[position]
                        .setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pagination_selected));
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
}

从代码逻辑开始,你可以看到我为整个图像创建了数组,即page_text[]

您的布局可能如下所示:

 <RelativeLayout
    android:id="@+id/relContent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin">

    <android.support.v4.view.ViewPager
        android:id="@+id/yourViewPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/image_count"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:background="#00000000"
        android:gravity="center"
        android:orientation="horizontal"></LinearLayout>


    <LinearLayout
        android:id="@+id/linTemp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/viewPagerItemDetails"
        android:orientation="horizontal">

        <ScrollView
            android:id="@+id/scrollTags"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></ScrollView>
    </LinearLayout>
</RelativeLayout>