【问题标题】:How to implement ViewPagerIndicator both title and circle如何实现 ViewPagerIndicator 的标题和圆圈
【发布时间】:2016-04-22 11:15:08
【问题描述】:

如何实现像SwiftPagingNav iOS Library这样的ViewPagerIndicator

  • 仅显示当前页面的标题
  • 滑动时同时显示当前页面和下一页或上一页

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

images

抱歉英语不好

【问题讨论】:

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


【解决方案1】:

使用以下代码根据需要设置查看寻呼机指示器:

 /*
* 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>

【讨论】:

    猜你喜欢
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多