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

1524人阅读 评论(0) 收藏 举报
分类:

1、程序运行效果图



二、代码实现

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

}


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1887199次
    • 积分:32457
    • 等级:
    • 排名:第148名
    • 原创:1199篇
    • 转载:278篇
    • 译文:3篇
    • 评论:181条
    学习经历
    毕业学校:南京邮电大学本科
    成长的地方:创新工场、美团点评
    职位:游戏研发工程师、Android研发工程师
    努力奋斗、快乐生活
    Android课程交流答疑QQ: 1019648568
    博客专栏
    最新评论