Tim*_*j93 5 android android-viewpager viewpagerindicator
我想将ViewPageIndicator用作我的ViewPager。我找到了这个 DotsPageIndicator,但似乎只用于AndroidWear。有趣的是,每个Android 6设备的首页上都有几乎完全相同的东西,如下图所示(单击以查看大图)。
那么,有什么我只是简单地缺少的东西,它可以像所有默认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'
}
Run Code Online (Sandbox Code Playgroud)
接下来,您需要将PageIndicator您喜欢的(我选择了 Circle 版本)添加到 XML 中。ViewPager这应该与您想要为其提供这些指标的XML 文件位于同一 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>
Run Code Online (Sandbox Code Playgroud)
然后,您需要初始化PageIndicator并设置其ViewPager. 这是在onCreate....()您使用的任何方法中完成的。作为基本规则:这部分应该与返回视图的方法相同。例如,我的在一个onCreateDialog()方法中,因为我有一个ViewPager内部 a Dialog。
@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);
}
Run Code Online (Sandbox Code Playgroud)
如果我没记错的话,应该就这些了。
作为参考,您可以在此处查看相关网站ViewPagerIndicator: http:
//viewpagerindicator.com
请注意:据我所知,您不必从该网站下载任何内容(至少如果您使用Android Studio)。您所做的第一步应该做好准备VPI。
希望这可以帮助。请随时要求澄清。再次强调,我不能保证这就是它的工作原理,但它应该可以帮助您继续下去。
| 归档时间: |
|
| 查看次数: |
2179 次 |
| 最近记录: |