Android--ViewPager

package com.cnn.viewpager01;

import java.util.List;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

public class MyPagerAdapter extends PagerAdapter {
    List<View> viewList;
    List<String> titleList;
    
    public MyPagerAdapter(List<View> viewList,List<String> titleList){
        this.viewList=viewList;
        this.titleList=titleList;
    }
    //初始化view
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // TODO 自动生成的方法存根
        container.addView(viewList.get(position));
        return viewList.get(position);
    }
    //移除View
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO 自动生成的方法存根
        container.removeView(viewList.get(position));
    }
    
    @Override
    public CharSequence getPageTitle(int position) {
        // TODO 自动生成的方法存根
        return titleList.get(position);
    }
    
    @Override
    public int getCount() {
        // TODO 自动生成的方法存根
        return viewList.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO 自动生成的方法存根
        return arg0==arg1;
    }
    
}
package com.cnn.viewpager01;

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

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends Activity {
    ViewPager viewPager;
    List<View> viewList;
    List<String> titleList;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        viewPager=(ViewPager) findViewById(R.id.viewPager1);
        View view1=View.inflate(MainActivity.this, R.layout.page1, null);
        View view2=View.inflate(MainActivity.this, R.layout.page2, null);
        View view3=View.inflate(MainActivity.this, R.layout.page3, null);
        
        viewList = new ArrayList<View>();
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        
        titleList=new ArrayList<String>();
        titleList.add("第一页");
        titleList.add("第二页");
        titleList.add("第三页");
        
        MyPagerAdapter pagerAdapter=new MyPagerAdapter(viewList, titleList);
        viewPager.setAdapter(pagerAdapter);
    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
<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/viewPager1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/tab"
            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>

 

posted @ 2015-12-23 15:55  在路上的白羊  阅读(163)  评论(0编辑  收藏  举报