Android-ViewPagerIndicator框架使用——IconPageIndicator

前言:IconPageIndicator是将自定义的图片作为指示图标的,这里的图片使用xml实现的。

    1.自己定义图片:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/perm_group_calendar_selected" />
    <item android:drawable="@drawable/perm_group_calendar_normal" />
</selector>

    2.定义布局文件simple_icons:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <com.viewpagerindicator.IconPageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

    3:代码中使用布局:

        setContentView(R.layout.simple_icons);
        //这个adapter需要实现IconPagerAdapter中的两个方法
        mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mIndicator = (IconPageIndicator) findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);

      其中的mAdapter是实现了IconPagerAdapter中的两个方法:

public interface IconPagerAdapter {
    /**
     * 获得图片作为指示标识
     */
    int getIconResId(int index);

    /**
     * 获得pager个数
     */
    int getCount();
}

      具体使用如下:

class TestFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
    protected static final int[] ICONS = new int[] {
              R.drawable.perm_group_calendar,
              R.drawable.perm_group_camera,
              R.drawable.perm_group_device_alarms,
              R.drawable.perm_group_location
      };
    .......
     @Override
      public int getIconResId(int index) {
        return ICONS[index % ICONS.length];
      }
}

  源码以及Demo下载地址:http://download.csdn.net/detail/as294985925/6796117

转载于:https://www.cnblogs.com/qinghuaideren/p/3501063.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值