这里我们实现的是通过点击按钮来切换ViewPager,就是在ViewPager的底端我们定义一个
LinearLayout布局,在其中放置4个ImageView控件,我们可以通过点击不同的控件来切换ViewPager并改变
ImageView的状态。所以这里我们需要用到选择器。
首先在res文件夹下新建一个drawable文件夹,并在其中新建一个Selector类型的dot.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--
定义选中和未选中时的图片
icon01是灰色,可以接受触摸或点击事件
icon02是白色,不可以接受触摸或点击事件
-->
<item android:drawable="@drawable/icon01" android:state_enabled="true"></item>
<item android:drawable="@drawable/icon02" android:state_enabled="false"></item>
<!--
控件状态:
android:state_selected:选中或未选中
android:state_focused:获得焦点或未获得焦点
android:state_enabled:能否接受触摸或者点击事件
-->
</selector>
然后是在activity_main.xml布局文件中引用我们定义的dot.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp&