安卓Viewpager之PagerTitleStrip和PagerTabStrip使用

PagerTabStrip 和 PagerTitleStrip 都是安卓ViewPager组件的一个“卡片”,效果如下图。

安卓Viewpager之PagerTitleStrip和PagerTabStrip使用_第1张图片 

PagerTitleStrip 是不可交互的,图中顶部那个就是(只能展示不能点击);PagerTabStrip 是可交互的,能点击底部那个tab跳转viewpager的pager。下面是主要代码

<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<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/viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >
		<!-- 顶部的 标题 tip -->
        <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.ViewPager>

</LinearLayout>
下面是MainActivity的java代码。

 
 
<pre name="code" class="java">package com.example.viewpagerandpagetitlestip;
 
import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
 /**
  * PagerTabStrip(可交互的,就是能点击跳转page) 就是比PagerTitleStrip多了个下条。。。 看api:http://blog.toolib.net/reference/android/support/v4/view/PagerTabStrip.html 简单
  * PagerTitleStrip(不可交互的,就是个title点击不会跳转page) 用户:http://www.open-open.com/lib/view/open1350291294336.html
  * @author Administrator
  */
public class PagerTitleDemoActivity extends Activity {
    /** Called when the activity is first created. */
    private ViewPager mViewPager;
    private PagerTitleStrip mPagerTitleStrip;
    private PagerTabStrip mPagerTabStrip;   //这个和上面那个区别是非交互性与交互性的indicator, 就是多了一个 滑块。。。。indicator, 都是作为viewpager的子view加进去ok
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewPager = (ViewPager)findViewById(R.id.viewpager);
        mPagerTitleStrip = (PagerTitleStrip)findViewById(R.id.pagertitle);
//        FrameLayout fl = (FrameLayout) this.findViewById(android.R.id.content);
//        mViewPager.removeView(mPagerTitleStrip); //移除布局文件中加进去的子
//        mPagerTitleStrip.setTextColor(Color.RED);
//        mPagerTitleStrip.setTextSpacing(10);
//        mPagerTitleStrip.setTextSize(TypedValue.COMPLEX_UNIT_PX, 33);
        
        mPagerTabStrip = new PagerTabStrip(this);
        ViewPager.LayoutParams params = new ViewPager.LayoutParams();
        params.width = ViewPager.LayoutParams.WRAP_CONTENT;
        params.height = ViewPager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.BOTTOM;  //相当于设置 此view的 gravity
        
        mViewPager.addView(mPagerTabStrip,params); //这个一旦加进去,上面那个 mPagerTitleStrip就会失效
        
        
         
         
        //每个页面的Title数据
        final ArrayList<TextView> views = new ArrayList<TextView>();
         
        final ArrayList<String> titles = new ArrayList<String>();
        for (int i = 1; i <= 20; i++) {
        	TextView tv = new TextView(this);
        	tv.setBackgroundColor(0xffffffff*new Random().nextInt());
        	views.add(tv);
        	titles.add("tab"+i);
			
		}
         
        //填充ViewPager的数据适配器
        PagerAdapter mPagerAdapter = new PagerAdapter() {
             
            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
            }
             
            @Override
            public int getCount() {
                return views.size();
            }
 
            @Override
            public void destroyItem(View container, int position, Object object) {
                ((ViewPager)container).removeView(views.get(position));
            }
 
            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }
            
            
 
            @Override
            public Object instantiateItem(View container, int position) {
                ((ViewPager)container).addView(views.get(position));
                return views.get(position);
            }
        };
         
        mViewPager.setAdapter(mPagerAdapter);
    }
}

代码很简单。想直接运行的朋友就 下载完整项目直接导入运行看效果吧。奋斗

 
 

你可能感兴趣的:(viewpager,PagerTabStrip,PagerTitleStrip)