亲宝软件园·资讯

展开

Android动态添加Indicator导航 Android动态给ViewPager添加Indicator导航

段炼Android 人气:0
想了解Android动态给ViewPager添加Indicator导航的相关内容吗,段炼Android在本文为您仔细讲解Android动态添加Indicator导航的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,ViewPager,Indicator,导航,下面大家一起来学习吧。

先看下效果

小圆点的形状和颜色都是可以自己定义的,看需求

首先第一步,滑2个圆点,一个是选中后的圆点,一个是未选中的圆点,看选中的圆点shape

<?xml version="1.0" encoding="utf-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="oval"
  >
  <solid android:color="@color/colorPrimary"/>

</shape>

未选中的

<?xml version="1.0" encoding="utf-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="oval"
  >
  <solid android:color="@color/colorAccent"/>

</shape>

是不是很简单,就指定一个圆形,一个颜色

接下来就是定义一个selector,用来控制选中和非选中

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:drawable="@drawable/indicator_checked_shape" android:state_enabled="true"/>
  <item android:drawable="@drawable/indicator_unchecked_shape" android:state_enabled="false"/>
</selector>

然后就是mainActivity的布局文件,放置一个ViewPager,下面放置一个LinearLayout,用来装小圆点

<?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.duanlian.viewpagerindicatordemo.MainActivity">

  <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="170dp"
    />

  <LinearLayout
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_below="@+id/viewpager"
    android:layout_marginTop="-40dp"
    android:gravity="center"
    android:orientation="horizontal"></LinearLayout>
</RelativeLayout>

接下来就是MainActivity的代码,其实很简单,都有注释,就不细说了

package com.duanlian.viewpagerindicatordemo;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
  private ViewPager mViewPager;
  private LinearLayout mLinear;
  private List<ImageView> mList;
  private PagerAdapter mPagerAdapter;
  private int[] imageArray = {R.mipmap.banner1, R.mipmap.banner2, R.mipmap.banner3, R.mipmap.ic_launcher};

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    initPoint();
  }

  private void initView() {
    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    mLinear = (LinearLayout) findViewById(R.id.linear);
    //给ViewPager添加图片显示
    mList = new ArrayList<>();
    for (int i = 0; i < imageArray.length; i++) {
      ImageView imageView = new ImageView(this);
      imageView.setImageResource(imageArray[i]);
      mList.add(imageView);
    }
    mPagerAdapter = new PagerAdapter(mList);
    mViewPager.setAdapter(mPagerAdapter);
    //ViewPager切换的监听事件
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        position = position % mList.size();// 需要对position的值进行重新赋值,否则会造成数组越界
        // 更新小圆点的显示
        for (int i = 0; i < mList.size(); i++) {
          ImageView iv = (ImageView) mLinear.getChildAt(i);
          // 当前滑到的是那一页就让第几个小圆点处于选中状态
          if (position == i) {
            iv.setEnabled(true);
          } else {
            iv.setEnabled(false);
          }
        }
      }

      @Override
      public void onPageSelected(int position) {

      }

      @Override
      public void onPageScrollStateChanged(int state) {

      }
    });
  }

  /**
   * 初始化指示器
   */
  private void initPoint() {
    for (int i = 0; i < mList.size(); i++) {
      // 往llContainer添加一个小圆点
      ImageView iv = new ImageView(this);
      //设置背景
      iv.setImageResource(R.drawable.indicator_viewpager_selector);
      //设置原点大小
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
      //如果是切好的图片就直接自适应
//      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      iv.setLayoutParams(params);
      // 默认第0个小圆点显示红色
      if (i == 0) {
        iv.setEnabled(true);
      } else {
        iv.setEnabled(false);
      }
      //设置原点之间的间距
      iv.setPadding(5, 5, 5, 5);
      //添加到LinearLayout中
      mLinear.addView(iv);
    }
  }
}

最后附上ViewPager的adapter

package com.duanlian.viewpagerindicatordemo;


import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

/**
 * viewPager的adapter
 */

public class PagerAdapter extends android.support.v4.view.PagerAdapter{
  private List<ImageView> list;

  public PagerAdapter(List<ImageView> list) {
    this.list = list;
  }

  @Override
  public int getCount() {
    return list.size();
  }

  @Override
  public boolean isViewFromObject(View view, Object object) {
    return view==object;
  }
  @Override
  public Object instantiateItem(ViewGroup container, int position) {
    container.addView(list.get(position));//添加页卡
    return list.get(position);
  }

  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView(list.get(position));//删除页卡
  }
}


加载全部内容

相关教程
猜你喜欢
用户评论