ViewPager + Fragment 替換 TabActivityandroid
- <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="fill_parent"
- android:background="#ffffff"
- android:flipInterval="30"
- android:persistentDrawingCache="animation"
- android:layout_centerInParent="true"
- >
- </android.support.v4.view.ViewPager>
- public class MainFragmentPagerAdapter extends FragmentPagerAdapter {
- private ArrayList<Fragment> fragments;
- public MainFragmentPagerAdapter(FragmentManager fm) {
- super(fm);
- // TODO Auto-generated constructor stub
- }
- public MainFragmentPagerAdapter(FragmentManager fm,ArrayList<Fragment> fragments){
- super(fm);
- this.fragments = fragments;
- }
- /* (non-Javadoc)
- * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
- */
- @Override
- public Fragment getItem(int arg0) {
- return fragments.get(arg0);
- }
- /* (non-Javadoc)
- * @see android.support.v4.view.PagerAdapter#getCount()
- */
- @Override
- public int getCount() {
- return fragments.size();
- }
- @Override
- public int getItemPosition(Object object) {
- // TODO Auto-generated method stub
- return super.getItemPosition(object);
- }
- }
- viewPager.setOnPageChangeListener(pageChangeListener);
- viewPager.setOnTouchListener(touchListener);
- pageChangeListener定義以下:
- private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {
- @Override
- public void onPageSelected(int arg0) {
- setIconSelected(arg0);
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- }
- @Override
- public void onPageScrollStateChanged(int arg0) {
- }
- };
- private OnTouchListener touchListener = new OnTouchListener(){
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (currentIndex != 0) {
- return false;
- }
- int[] location = new int[2];
- homeFragment.gallery.getLocationOnScreen(location);
- if (location[0] != 0) {
- return false;
- }
- if (event.getRawY() > location[1]
- && event.getRawY() - location[1] < homeFragment.gallery
- .getHeight()) {
- return homeFragment.gallery.dispatchTouchEvent(event);
- }
- return false;
- }
- };
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.setting);
- getView();
- setListener();
- }
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- Utils.log("onCreateView");
- View v = inflater.inflate(R.layout.setting, container, false);
- getViews(v);
- setListener();
- return v;
- }
- private OnTouchListener touchListener = new OnTouchListener(){
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (currentIndex != 0) {
- return false;
- }
- int[] location = new int[2];
- homeFragment.gallery.getLocationOnScreen(location);
- if (location[0] != 0) {
- return false;
- }
- if (event.getRawY() > location[1]
- && event.getRawY() - location[1] < homeFragment.gallery
- .getHeight()) {
- return homeFragment.gallery.dispatchTouchEvent(event);
- }
- return false;
- }
- };