From fe5f595f522beec02b01478538d626ef068144f1 Mon Sep 17 00:00:00 2001 From: makovkastar Date: Sat, 20 Apr 2013 10:58:52 +0300 Subject: [PATCH] Implement swiping between tabs --- .../activity_choose_photos.xml | 1 - client/res/layout/activity_choose_photos.xml | 11 +++- .../senab/photup/PhotoSelectionActivity.java | 20 ++++++- .../adapters/FragmentsViewPagerAdapter.java | 54 +++++++++++++++++++ 4 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 client/src/uk/co/senab/photup/adapters/FragmentsViewPagerAdapter.java diff --git a/client/res/layout-large-land/activity_choose_photos.xml b/client/res/layout-large-land/activity_choose_photos.xml index 1e4ce96..3e4e67d 100644 --- a/client/res/layout-large-land/activity_choose_photos.xml +++ b/client/res/layout-large-land/activity_choose_photos.xml @@ -6,7 +6,6 @@ android:orientation="horizontal" > - + + + \ No newline at end of file diff --git a/client/src/uk/co/senab/photup/PhotoSelectionActivity.java b/client/src/uk/co/senab/photup/PhotoSelectionActivity.java index 593bcff..ea3981b 100644 --- a/client/src/uk/co/senab/photup/PhotoSelectionActivity.java +++ b/client/src/uk/co/senab/photup/PhotoSelectionActivity.java @@ -15,6 +15,7 @@ *******************************************************************************/ package uk.co.senab.photup; +import uk.co.senab.photup.adapters.FragmentsViewPagerAdapter; import uk.co.senab.photup.events.PhotoSelectionAddedEvent; import uk.co.senab.photup.events.PhotoSelectionRemovedEvent; import uk.co.senab.photup.events.UploadsModifiedEvent; @@ -29,6 +30,7 @@ import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; +import android.support.v4.view.ViewPager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; @@ -49,6 +51,8 @@ public class PhotoSelectionActivity extends AbstractPhotoUploadActivity implemen private UploadActionBarView mUploadActionView; private UploadsActionBarView mUploadsActionView; + + private ViewPager mViewPager; private boolean mSinglePane; @@ -79,6 +83,15 @@ public void onCreate(Bundle savedInstanceState) { ab.addTab(ab.newTab().setText(R.string.tab_photos).setTag(TAB_PHOTOS).setTabListener(this)); if (mSinglePane) { + mViewPager = (ViewPager) findViewById(R.id.vp_fragments); + FragmentsViewPagerAdapter viewPagerAdapter = new FragmentsViewPagerAdapter(getSupportFragmentManager()); + mViewPager.setAdapter(viewPagerAdapter); + mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){ + @Override + public void onPageSelected(int position) { + getSupportActionBar().setSelectedNavigationItem(position); + } + }); ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); ab.addTab(ab.newTab().setTag(TAB_SELECTED).setTabListener(this)); ab.addTab(ab.newTab().setText(R.string.tab_uploads).setTag(TAB_UPLOADS).setTabListener(this)); @@ -207,8 +220,11 @@ public void onTabReselected(Tab tab, FragmentTransaction ft) { } public void onTabSelected(Tab tab, FragmentTransaction ft) { - final int id = (Integer) tab.getTag(); - replacePrimaryFragment(id, ft); + if (mViewPager != null) { + // When the tab is selected, switch to the + // corresponding page in the ViewPager. + mViewPager.setCurrentItem(tab.getPosition()); + } // Refresh Action Bar so correct Menu is displayed supportInvalidateOptionsMenu(); diff --git a/client/src/uk/co/senab/photup/adapters/FragmentsViewPagerAdapter.java b/client/src/uk/co/senab/photup/adapters/FragmentsViewPagerAdapter.java new file mode 100644 index 0000000..6bf77c9 --- /dev/null +++ b/client/src/uk/co/senab/photup/adapters/FragmentsViewPagerAdapter.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright 2013 Chris Banes. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *******************************************************************************/ +package uk.co.senab.photup.adapters; + +import uk.co.senab.photup.fragments.SelectedPhotosFragment; +import uk.co.senab.photup.fragments.UploadsFragment; +import uk.co.senab.photup.fragments.UserPhotosFragment; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; + +public class FragmentsViewPagerAdapter extends FragmentPagerAdapter { + + public FragmentsViewPagerAdapter(FragmentManager fragmentManager) { + super(fragmentManager); + } + + @Override + public Fragment getItem(int position) { + Fragment fragment; + switch (position) { + case 0: + fragment = new UserPhotosFragment(); + return fragment; + case 1: + fragment = new SelectedPhotosFragment(); + return fragment; + case 2: + fragment = new UploadsFragment(); + return fragment; + default: + return null; + } + } + + @Override + public int getCount() { + return 3; + } + +}