本文介绍了带有底部圆点的 Android ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 ViewPager 中添加 3 个底部点,就像这样.

我使用 FragmentActivity 并支持库 ViewPager.

解决方案

不需要那么多代码.

只需将 viewpagertablayout 结合使用,您无需编写太多代码即可完成所有这些工作.>

您的主要布局:

<android.support.v4.view.ViewPagerandroid:id="@+id/pager";android:layout_width=match_parent"android:layout_height=match_parent"></android.support.v4.view.ViewPager><android.support.design.widget.TabLayoutandroid:id="@+id/tabDots";android:layout_alignParentBottom="true"android:layout_width=match_parent"android:layout_height="wrap_content";app:tabBackground="@drawable/tab_selector";app:tabGravity="center";app:tabIndicatorHeight=0dp"/></RelativeLayout>

按如下方式连接不活动的 UI 元素或片段:

Java 代码:

mImageViewPager = (ViewPager) findViewById(R.id.pager);TabLayout tabLayout = (TabLayout) findViewById(R.id.tabDots);tabLayout.setupWithViewPager(mImageViewPager, true);

就是这样,你可以走了.

您需要在 drawable 文件夹中创建以下

tab_indicator_selected.

tab_indicator_default.

tab_selector.

 

和我一样懒惰?好了,上面所有的代码都转换成一个库了!用法在您的 gradle 中添加以下内容:实现'com.chabbal:slidingdotsplash:1.0.2'将以下内容添加到您的 Activity 或 Fragment 布局中.

<com.chabbal.slidingdotsplash.SlidingSplashViewandroid:id="@+id/闪屏";android:layout_width=match_parent"android:layout_height=match_parent"app:imageResources="@array/img_id_arr"/>

strings. 中创建一个整数数组,例如

<item>@drawable/img1</item><item>@drawable/img2</item><item>@drawable/img3</item><item>@drawable/img4</item></整数数组>

完成!额外为了监听页面变化使用addOnPageChangeListener(listener);Github 链接.

I want to add 3 bottom dots to my ViewPager, like this.

I use FragmentActivity and support library ViewPager.

解决方案

No need for that much code.

You can do all this stuff without coding so much by using only viewpager with tablayout.

Your main Layout:

<RelativeLayout
   

Hook up your UI elements inactivity or fragment as follows:

Java Code:

mImageViewPager = (ViewPager) findViewById(R.id.pager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabDots);
tabLayout.setupWithViewPager(mImageViewPager, true);

That's it, you are good to go.

You will need to create the following drawable folder.

tab_indicator_selected.

<?

tab_indicator_default.

<?

tab_selector.

 <?

Feeling as lazy as I am? Well, all the above code is converted into a library!UsageAdd the following in your gradle:implementation 'com.chabbal:slidingdotsplash:1.0.2'Add the following to your Activity or Fragment layout.

<com.chabbal.slidingdotsplash.SlidingSplashView
        android:id="@+id/splash"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:imageResources="@array/img_id_arr"/>

Create an integer array in strings. e.g.

<integer-array name="img_id_arr">
   <item>@drawable/img1</item>
   <item>@drawable/img2</item>
   <item>@drawable/img3</item>
   <item>@drawable/img4</item>
</integer-array>

Done!Extra in order to listen page changes use addOnPageChangeListener(listener);Github link.

这篇关于带有底部圆点的 Android ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 20:44
查看更多