Fragment窗口導航

先上效果圖



主activity對應的xml佈局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   	android:id="@+id/vp"
   >
   <!-- 指示器 -->
	<android.support.v4.view.PagerTitleStrip
	    android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff"
	    />
</android.support.v4.view.ViewPager>

主activity的代碼

package com.myself.windownav.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity {
	//數據適配器
	private MyFragmentPagerAdapter mFragmentPa;
	private ViewPager viewPager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mFragmentPa = new MyFragmentPagerAdapter(getSupportFragmentManager());
		viewPager = (ViewPager) findViewById(R.id.vp);
		viewPager.setAdapter(mFragmentPa);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_menu, menu);
		return true;
	}
	
	/**
	 * 
	 * @文件名 MainActivity.java
	 * @類說明 (自定義Viewpager適配器)	
	 * @作者 周運韜
	 * @版本 v_1.0
	 * @創建時間 2014-3-26 下午8:38:30
	 */
	class MyFragmentPagerAdapter extends FragmentPagerAdapter{
		public MyFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		/**
		 * 返回顯示的內容
		 */
		@Override
		public Fragment getItem(int arg0) {
			MyFragment fragment = new MyFragment();
			Bundle bundle = new Bundle();
			bundle.putInt(MyFragment.VAL_KEY, arg0 + 1);
			fragment.setArguments(bundle);
			return fragment;
		}

		/***
		 * 一共有幾頁
		 */
		@Override
		public int getCount() {
			return 3;
		}
		
		/***
		 * 指示器的標題
		 */
		@Override
		public CharSequence getPageTitle(int position) {
			switch (position) {
			case 0:
				return getResources().getString(R.string.news);
			case 1:
				return getResources().getString(R.string.img);
			case 2:
				return getResources().getString(R.string.txt);
			}
			return null;
		}
	}
}

MyFragment的具體實現

package com.myself.windownav.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment extends Fragment {
	public static final String VAL_KEY = "val";
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_viewpager, null);
		TextView tvFlag = (TextView) view.findViewById(R.id.flag);
		tvFlag.setText(getArguments().getInt(VAL_KEY) + "");
		return null;
	}
}

layout_viewpager的實現

<?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" >
    <TextView
        android:id="@+id/flag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章