android花屏效果的实现(ViewPager的基本使用)

1、程序运行效果图

android花屏效果的实现(ViewPager的基本使用)


二、代码实现

1、main.xml

 

<RelativeLayout 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"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >



    <android.support.v4.view.ViewPager

        android:id="@+id/viewpager"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center" >



        <android.support.v4.view.PagerTitleStrip

            android:id="@+id/pageTitleStrip"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_gravity="top" >

        </android.support.v4.view.PagerTitleStrip>

    </android.support.v4.view.ViewPager>



</RelativeLayout>


 

2、tab1.xml、tab2.xml、tab3.xml

 

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:background="@drawable/a" >



</LinearLayout>


 


3、MainActivity

 

package com.njupt.viewpagertest3;



import java.util.ArrayList;

import java.util.List;



import android.os.Bundle;

import android.app.Activity;

import android.support.v4.view.PagerAdapter;

import android.support.v4.view.PagerTitleStrip;

import android.support.v4.view.ViewPager;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.View;

import android.view.ViewGroup;



public class MainActivity extends Activity {



	private ViewPager viewPager;

	private PagerTitleStrip pagerTitleStrip;//表示滑动的每一页的标题

	private List<View> list;//表示装载滑动的布局

	private List<String> titlelist;//表示滑动的每一页的标题...

	

	

	@Override

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		

		viewPager = (ViewPager) findViewById(R.id.viewpager);

		pagerTitleStrip = (PagerTitleStrip) findViewById(R.id.pageTitleStrip);

		

		//动态加载布局...

		View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab1, null);

		View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab2, null);

		View view3 = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab3, null);

		

		list = new ArrayList<View>();

		list.add(view1);

		list.add(view2);

		list.add(view3);

		

		titlelist = new ArrayList<String>();

		titlelist.add("章泽天1");

		titlelist.add("章泽天2");

		titlelist.add("章泽天3");

		

		viewPager.setAdapter(new MyAdapter());

		

	}



	public class MyAdapter extends PagerAdapter{



		@Override

		public int getCount() {

			return list.size();

		}



		@Override

		public void destroyItem(ViewGroup container, int position, Object object) {

			((ViewPager)container).removeView(list.get(position));

		}

		

		

		

		@Override

		public Object instantiateItem(ViewGroup container, int position) {

			((ViewPager)container).addView(list.get(position));

			return list.get(position);

		}

		

		@Override

		public CharSequence getPageTitle(int position) {

			return titlelist.get(position);

		}

		

		@Override

		public boolean isViewFromObject(View arg0, Object arg1) {

			return arg0 == arg1;

		}

		

	}

	@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;

	}



}


 

 

你可能感兴趣的:(viewpager)