actionBar + viewPager实现活动tab

package com.qloudfin.jpaybusiness.pos;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.TypedArray;
import android.content.res.Resources.Theme;
import android.view.Menu;
import android.view.MenuItem;

import com.qloudfin.jpaywallet.R;
import com.qloudfin.jpaywallet.card.MyCardActivity;
import com.qloudfin.jpaywallet.card.NewCardActivity;
import com.qloudfin.jpaywallet.common.TabbedActivity;
import com.qloudfin.jpaywallet.scan.ActivityIntegrator;
import com.qloudgen.qwidget.internal.FragmentPagerAdapter;
import com.qloudgen.qwidget.internal.ViewPager;

/*
 * ------刷卡 功能模块--------
 */
public class PosActivity extends TabbedActivity {

	private ViewPager mViewPager;
	private ViewPagerAdapter mViewPagerAdapter;

	private CardDetailFragment fr1 = new CardDetailFragment();
	private ConsumeFragment fr2 = new ConsumeFragment();
	private AwardFragment fr3 = new AwardFragment();

	private static final int TAB_INDEX_COUNT = 3;
	private static final int TAB_INDEX_ONE = 0;
	private static final int TAB_INDEX_TWO = 1;
	private static final int TAB_INDEX_THREE = 2;

	@Override
	protected void setupTab() {

		setContentView(R.layout.ac_container);

		ActionBar ab = getActionBar();
		ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		setUpViewPager();
		setUpTabs();
	}

	// 使用下面的响应方法,需要在TabbedActivity中实现ActionBar.TabListener方法。
	@Override
	public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

		mViewPager.setCurrentItem(tab.getPosition());
	}

	private void setUpTabs() {
		final ActionBar actionBar = getActionBar();
		for (int i = 0; i < mViewPagerAdapter.getCount(); ++i) {
			actionBar.addTab(actionBar.newTab()
					.setText(mViewPagerAdapter.getPageTitle(i))
					.setTabListener(this));
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	private void setUpViewPager() {
		mViewPagerAdapter = new ViewPagerAdapter(getFragmentManager());
		mViewPager = (ViewPager) findViewById(R.id.viewPager);
		mViewPager.setAdapter(mViewPagerAdapter);
		mViewPager
				.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
					@Override
					public void onPageSelected(int position) {
						final ActionBar actionBar = getActionBar();
						actionBar.setSelectedNavigationItem(position);
					}

					@Override
					public void onPageScrollStateChanged(int state) {
						switch (state) {
						case ViewPager.SCROLL_STATE_IDLE:
							// TODO
							break;
						case ViewPager.SCROLL_STATE_DRAGGING:
							// TODO
							break;
						case ViewPager.SCROLL_STATE_SETTLING:
							// TODO
							break;
						default:
							// TODO
							break;
						}
					}
				});
	}

	public class ViewPagerAdapter extends FragmentPagerAdapter {

		public ViewPagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			switch (position) {
			case TAB_INDEX_ONE:
				return fr1;
			case TAB_INDEX_TWO:
				return fr2;
			case TAB_INDEX_THREE:
				return fr3;
			}
			throw new IllegalStateException("No fragment at position "
					+ position);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return TAB_INDEX_COUNT;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			String tabLabel = null;
			switch (position) {
			case TAB_INDEX_ONE:
				tabLabel = getString(R.string.carddetail);
				break;
			case TAB_INDEX_TWO:
				tabLabel = getString(R.string.consume);
				break;
			case TAB_INDEX_THREE:
				tabLabel = getString(R.string.award);
				break;
			}
			return tabLabel;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuItem mi = menu.add(Menu.NONE, R.id.new_card_scan, 50, "new");
		mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

		TypedArray a = getTheme().obtainStyledAttributes(
				new int[] { R.attr.ic_scan });
		int rid = a.getResourceId(0, 0);
		a.recycle();
		if (rid > 0)
			mi.setIcon(rid);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.new_card_scan:
			ActivityIntegrator integrator = new ActivityIntegrator(this);
			integrator.initiateScan();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值