Android 移动设备(非磨损)是否有默认的 DotsPageIndicator?

Is there a default DotsPageIndicator for Android mobile (not wear)?

我想为我的 ViewPager 使用 ViewPageIndicator。我找到了 this DotsPageIndicator,但它似乎只适用于 AndroidWear。有趣的是,每个 Android 6 设备的主页上都有几乎完全相同的内容,如下图所示(点击查看大图)。

那么,有没有什么我只是想念的东西,它能像所有默认的 Android 东西一样容易实现吗?或者我还需要使用外部库吗?如果后者是真的,我该怎么做?

放置此代码是您的 java class:

@Override
protected void onCreate(Bundle savedBundle) {
    // TODO Auto-generated method stub
    super.onCreate(savedBundle);
    addDots();//setup dot layout 
    selectDot(0); //set initial dot position
 }

protected void addDots() {
        dots = new ArrayList<ImageView>();
        LinearLayout dotsLayout = (LinearLayout) findViewById(R.id.pager_dots);

        for (int i = 0; i < NUM_OF_DOTS; i++) {
            ImageView dot = new ImageView(this);
            dot.setImageDrawable(getResources().getDrawable(
                    R.drawable.ic_pager_dot_not_selected));

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 0, 5, 0);
            dotsLayout.addView(dot, params);

            dots.add(dot);
        }
    }

    public void selectDot(int idx) {
        Resources res = getResources();
        for (int i = 0; i < NUM_PAGES; i++) {
            int drawableId = (i == idx) ? (R.drawable.ic_pager_dot_selected)
                    : (R.drawable.ic_pager_dot_not_selected);
            Drawable drawable = res.getDrawable(drawableId);
            dots.get(i).setImageDrawable(drawable);
        }
    }

这是layout.xml

 <LinearLayout
        android:id="@+id/pager_dots"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/activity_wizard_footer"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="5dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal" />

这是点图。

我很久以前就找到了这个问题的答案,但忘了我还有这个问题,所以我现在要试着用我当时的记忆来回答它。所以请注意,它可能并不完美,但我会尽力让它变得更好。


您将需要 viewPagerIndicator 库。我相信这样做的方法是打开你的 build.gradle (Module: app) Gradle 脚本,然后转到 dependencies 并添加 compile 'com.viewpagerindicator:library:2.4.1@aar'

结果应如下所示:

...
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile "com.android.support:support-v13:24.1.1"
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:support-v4:24.1.1'
    compile 'com.android.support:design:24.1.1'
    compile 'com.google.android.gms:play-services-appindexing:9.4.0'
    compile 'com.viewpagerindicator:library:2.4.1@aar'
}

接下来,您需要将您喜欢的 PageIndicator(我选择了 Circle 版本)添加到您的 XML。这应该与您要为其设置这些指标的 ViewPager 在同一个 XML 文件中。我的文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true">

    <com.example.tim.timapp.CustomStuff.Misc.CustomViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:overScrollMode="never"/>

    <!-- Note that I use a custom ViewPager for app-specific reasons. -->
    <!-- It doesn't matter if you use a custom or default VP as far as I know. -->

    <com.viewpagerindicator.CirclePageIndicator
        android:id="@+id/circlePageIndicator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:fillColor="@color/colorPrimary"
        app:strokeColor="@color/lightGrey"/>

</LinearLayout>

然后,您需要初始化 PageIndicator,并设置其 ViewPager。无论您使用什么,这都是在 onCreate....() 方法中完成的。作为基本规则:这部分应该使用与 returns 您的视图相同的方法。例如我的是 onCreateDialog() 方法,因为我在 Dialog.

中有一个 ViewPager
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate view
    view = inflater.inflate(R.layout.fragment_viewpager, null);

    // Initialize viewpager and set max amount of off screen pages
    mPager = (ViewPager) view.findViewById(R.id.pager);
    mPager.setOffscreenPageLimit(3);

    // Initialize pageradapter and set adapter to viewpager
    mPagerAdapter = new SettingsAdapter(inflater);
    mPager.setAdapter(mPagerAdapter);
    mPagerAdapter.notifyDataSetChanged();

    // Initialize circlePageIndicator and set viewpager for it
    CirclePageIndicator circlePageIndicator = (CirclePageIndicator) view.findViewById(R.id.circlePageIndicator);
    circlePageIndicator.setViewPager(mPager);
}

如果我没记错的话,应该就是这些了。
作为参考,您可以在此处查看此 ViewPagerIndicator 的网站:
http://viewpagerindicator.com

请注意:据我所知,您不必从该网站下载任何东西(至少如果不是你使用 Android Studio)。您所做的第一步应该是准备好 VPI

希望这对您有所帮助。随时要求澄清。同样,我不能保证这就是它的工作原理,但它应该能让你继续。