Android --------------------ActionBar 与 ViewPager 和 ActionTab 切换 的源代码实现

參考网址:

点击打开链接

源代码实现:

package com.example.actionbardemo2;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
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;
import android.view.MenuItem; public class MainActivity extends FragmentActivity implements
ActionBar.TabListener { private dFragment1 fragment1 = new dFragment1();
private dFragment2 fragment2 = new dFragment2();
private dFragment3 fragment3 = new dFragment3(); private static final int TAB_INDEX_COUNT = 2; private static final int TAB_INDEX_ONE = 0;
private static final int TAB_INDEX_TWO = 1; private ViewPager viewPager;
private myFrgamentPagerAdapter viewPagerAdapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 1 设置 ViewPager
setViewPager(); // 2 获取ActionBar
getActionBarMethod(); } // 1 设置 ViewPager
private void setViewPager() { viewPagerAdapter = new myFrgamentPagerAdapter(getSupportFragmentManager()); viewPager = (ViewPager)this.findViewById(R.id.viewpager);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
super.onPageSelected(position); final ActionBar actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); } @Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
super.onPageScrollStateChanged(state);
} }); } // 2 获取ActionBar
private void getActionBarMethod() {
//获取ActionBar
ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // 设置actionBar的颜色
Drawable draw = new ColorDrawable(Color.GREEN);
actionBar.setBackgroundDrawable(draw); // 设置ActionBar tab
ActionBar.Tab tab = actionBar.newTab();
tab.setText("核心产品");
tab.setIcon(draw);
tab.setTabListener(this);
actionBar.addTab(tab); tab = actionBar.newTab();
tab.setText("平台产品");
tab.setIcon(draw);
tab.setTabListener(this);
actionBar.addTab(tab); // 去掉左側小箭头右边的图标:
// 代表 左側要有一个箭头
actionBar.setDisplayHomeAsUpEnabled(true);
// 代表 禁用Logo
actionBar.setDisplayUseLogoEnabled(false);
// 使用 透明色来取代原有的图标,相当于 不显示
actionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));
} /**
* ActionBar action 点击事件的处理:
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
} return super.onOptionsItemSelected(item);
} /**
* ActionTab 的 点击事件:
*
* */
@Override
public void onTabReselected(Tab arg0, android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub } @Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
// 依据ActionBar Tab 的 位置 index 来设置 ViewPager 的 当前的显示页。 viewPager.setCurrentItem(tab.getPosition());
} @Override
public void onTabUnselected(Tab arg0, android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub } /**
* ActionBar 的样式,设置。
*
* */
@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;
} //TODO 2 Viewpager 的自己定义类:
class myFrgamentPagerAdapter extends FragmentPagerAdapter { public myFrgamentPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
} @Override
public Fragment getItem(int position) {
// TODO 设置 返回的数据
switch (position)
{
case TAB_INDEX_ONE: return fragment1; case TAB_INDEX_TWO: return fragment2;
} throw new IllegalStateException("No fragment at position " + position);
} @Override
public int getCount() {
// TODO Auto-generated method stub
return TAB_INDEX_COUNT;
} } }
上一篇:AMD,CMD,UMD,CommonJS


下一篇:.Net 平台下的互联网架构新思考