Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android花屏效果的實現(ViewPager的基本使用)

android花屏效果的實現(ViewPager的基本使用)

編輯:關於Android編程

1、程序運行效果圖

\


二、代碼實現

1、main.xml



    

        
        
    



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








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 list;//表示裝載滑動的布局
	private List 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();
		list.add(view1);
		list.add(view2);
		list.add(view3);
		
		titlelist = new ArrayList();
		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. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved