现在很多App在进入的时候,都会在首页的顶部有一个广告栏,具体实现了以下的功能:
1.实现用户左右手动滑动
2.实现广告栏自动轮播
3.实现广告栏点击事件
4.无论是用户手动滑动还是广告栏自动轮播的时候,下方的指示点都会随之改变状态
5.实现用户点击或者拖拽时轮播停止,松手时继续轮播
先上3个效果gif吧:
第一个gif,展示的是功能1和2;
第二个gif,展示的是功能3;
第三个gif展示的是功能5;
那功能4,每个gif都可以看得到。
以上就是所有的效果,那么以下部分,是代码逻辑,我们一部分一部分粘贴出来,很多地方都已经写了非常详细的注释了:
先上布局文件,布局文件就非常简单,activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.honey.guanggaoview.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="180dp"></android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/vp"
android:background="#44000000"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:id="@+id/point_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"