带标题和导航点的ViewPager

带标题和导航点的ViewPager带标题和导航点的ViewPager带标题和导航点的ViewPager

关键代码:

xml布局:

<FrameLayout 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"
    tools:context=".MainActivity" >

    <android.support.v4.view.ViewPager
        android:id="@+id/mViewPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >

        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pagertitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top" >
        </android.support.v4.view.PagerTitleStrip>
    </android.support.v4.view.ViewPager>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/img1"
            android:layout_width="40dp"
            android:layout_height="40dp" />

        <ImageView
            android:id="@+id/img2"
            android:layout_width="40dp"
            android:layout_height="40dp" />

        <ImageView
            android:id="@+id/img3"
            android:layout_width="40dp"
            android:layout_height="40dp" />
    </LinearLayout>

</FrameLayout>

MainActivity:

private ViewPager mViewPager;
 private PagerTitleStrip mPagerTitleStrip;
 private List<ImageView> imgs;

@Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mViewPager = (ViewPager) findViewById(R.id.mViewPager);
   mPagerTitleStrip = (PagerTitleStrip)findViewById(R.id.pagertitle);
   //每页显示的内容      也可以用LayoutInflater mLi = LayoutInflater.from(this);View view1 = mLi.inflate((R.layout.view1, null);添加布局
  final List<ImageView> views = new ArrayList<ImageView>();
  ImageView img = new ImageView(this);     img.setImageResource(R.drawable.ic_launcher);
  ImageView img2 = new ImageView(this);   img2.setImageResource(R.drawable.ic_launcher);
  ImageView img3 = new ImageView(this);    img3.setImageResource(R.drawable.ic_launcher);
  views.add(img);
  views.add(img2);
  views.add(img3);
  //初始化导航点ImageView数组
  imgs=new ArrayList<ImageView>();
  ImageView i1=(ImageView)findViewById(R.id.img1);
  ImageView i2=(ImageView)findViewById(R.id.img2);
  ImageView i3=(ImageView)findViewById(R.id.img3);
  //数组中添加数据
  imgs.add(i1);
  imgs.add(i2);
  imgs.add(i3);
  for (int j = 0; j < views.size(); j++)

{
    if (j==0)

   {
       imgs.get(j).setBackgroundResource(R.drawable.a);
   }
   else {
    imgs.get(j).setBackgroundResource(R.drawable.b);
   }
  }
  /*// 每个页面的Title数据     添加标题方法一:
  final ArrayList<String> titles = new ArrayList<String>();
  titles.add("tab1");
  titles.add("tab2");
  titles.add("tab3");*/
  //适配器
  PagerAdapter mPagerAdapter = new PagerAdapter()
  {
   //用于判断是否由对象生成界面
   @Override
   public boolean isViewFromObject(View arg0, Object arg1)
   {
    return arg0 == arg1;
   }
    //Return the number of views 总共有几页?
   @Override
   public int getCount()
   {
    return views.size();
   }
      //Remove a page for the given position从container中移除当前view
   @Override
   public void destroyItem(View container, int position, Object object)
   {
    //删除页面
    ((ViewPager) container).removeView((View) object);//等价于:((ViewPager) container).removeView(views.get(position));
   }
   //Returns an Object representing the new page返回一个对象表示新的一页
   @Override
   public Object instantiateItem(View container, int position)
   {
    //添加页面
    ((ViewPager) container).addView(views.get(position));
    return views.get(position);
   }
   @Override
   public CharSequence getPageTitle(int position)
   {

     //添加标题方法二:
    switch (position)
    {
     case 0:   
      return "tab1";
     case 1:
      return "tab2";
     case 2:
      return "tab3";
    default:
     break;
    }
    //return titles.get(position);  //添加标题方法一:
    return null;
   }
  };
  mViewPager.setAdapter(mPagerAdapter);

//事件处理
  mViewPager.setOnPageChangeListener(new OnPageChangeListener()
  {
   @Override
   public void onPageSelected(int position)
   {
    for (int i = 0; i < views.size(); i++)
    {
     if (i==position)//选中时
     {
      imgs.get(i).setBackgroundResource(R.drawable.a);
     }
     else {
      imgs.get(i).setBackgroundResource(R.drawable.b);
     }
    }
   }
  });
 }

 

关于自动轮播:

// 当前轮播页
 private int currentItem = 0;

 

  final Handler handler = new Handler(){
     @Override
     public void handleMessage(Message msg) {
     super.handleMessage(msg);
     mViewPager.setCurrentItem(currentItem);
        }
     };

 

Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {

   @Override
   public void run() {
    synchronized (mViewPager) {
     currentItem = (currentItem + 1) % mList.size();
     handler.obtainMessage().sendToTarget();
    }
   }
  }, 0, 5, TimeUnit.SECONDS);

参考:http://www.it165.net/pro/html/201406/16227.html

http://www.apkbus.com/android-83696-1-1.html

一个学习网:http://www.godcoder.com/

//轮播公告
  for (int i = 1; i <= 5; i++) {
   final int j=i;
   TextView t=new TextView(this);
   t.setText("优惠公告"+i);
   t.setTextColor(Color.WHITE);
   mFlipper.addView(t);
   t.setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
     Toast.makeText(MainActivity.this, "优惠公告 "+j, Toast.LENGTH_SHORT).show();
    }
   });
  }
   mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
                 R.anim.push_up_in));
         mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                 R.anim.push_up_out));
   mFlipper.startFlipping();

 

http://www.cnblogs.com/ok-lanyan/archive/2012/08/02/2619701.html  写的超好!

http://www.linuxidc.com/Linux/2012-05/60763.htm  Android中利用ViewFliper实现屏幕切换效果

Android垂直滑动新特性:应用欢迎界面

http://www.godcoder.com/project/view/270

你可能感兴趣的:(带标题和导航点的ViewPager)