我想为我的ViewPageIndicator使用ViewPager。我发现了thisdotspageindicator,但它似乎只适用于AndroidWear。有趣的是,每个android 6设备的主页上都有几乎完全相同的东西,如下图所示(点击查看更大的图片)。
android - 是否有默认的DotsPageIndicator for Android mobile(不穿)?-LMLPHP
那么,有没有什么我只是遗漏了,它能像所有默认的android一样容易实现呢?或者我还需要使用外部库吗?如果后者是真的,我该怎么做?

最佳答案

我很久以前就找到了这个问题的答案,但我忘了我还有一个答案,所以我现在就试着用我记忆中的东西来回答这个问题。所以请注意,这可能不是完美的,但我会尽量把它做好。
您需要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添加到xml中(我选择了circle版本)。这应该与您希望具有这些指示器的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....()方法中完成的,无论您使用什么。作为一个基本规则:这个部分应该在返回视图的同一个方法中。例如,我的方法是aonCreateDialog()方法,因为aViewPager中有aDialog
@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
希望这有帮助。请随时要求澄清。再说一遍,我不能保证这就是它的工作原理,但它应该让你去。

07-26 09:40
查看更多