【问题标题】:Android Implement a page counter like the one on the home screenAndroid 实现一个页面计数器,如主屏幕上的页面计数器
【发布时间】:2012-04-17 23:53:04
【问题描述】:

有谁知道如何像主屏幕上的那样设置页面/查看计数器(小点)的示例?

像这个例子:

谢谢

【问题讨论】:

标签: android


【解决方案1】:

PagerTitleStrip 是您与ViewPager 一起使用的,用于指示您当前所在的页面。这是您添加到 XML 的内容。

布局 XML

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

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

    <android.support.v4.view.PagerTitleStrip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top" />
</android.support.v4.view.ViewPager>

</LinearLayout>

在您的 PagerAdapter 中

@Override
    public CharSequence getPageTitle (int position) {
        return "Your static title";
    }

不过,PagerTitleStrip 的可定制性不高,但 ViewPagerIndicator 可定制性很强,并且包含您正在寻找的虚线指示符。您需要对其进行主题化以在 OP 中重新创建图片。

圆圈指示器与 ViewPagerIndicator:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.viewpagerindicator.sample"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    />
<com.viewpagerindicator.CirclePageIndicator
    android:id="@+id/indicator"
    android:padding="10dip"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    />

</LinearLayout>

然后在你的代码中

CirclePageIndicator mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);

ViewPagerExtensions 是另一个开源库,它为您可能感兴趣的ViewPager 提供自定义视图。

【讨论】:

  • 谢谢,这可能正是我需要的。
  • 我无法让它工作,JakeWarton 上的项目似乎不容易配置。您还有其他相关文档可以提供帮助吗?
  • 这非常简单,我相信你会得到它的工作。这是ViewPagerIndicator website 的链接和a series of articles on customizing it. 的链接
  • 我的意思是 CirclePageIndicator,我没有尝试默认的 ViewPageIndicator。
  • 我在帖子中举了一个使用CirclePagerIndicator 的例子。我不明白你遇到了什么麻烦。就文档而言,我只知道我在谷歌上搜索的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-25
  • 2010-09-11
相关资源
最近更新 更多