From 173638b9a4c1d039aeae350765d7fbf8330d91e1 Mon Sep 17 00:00:00 2001 From: Artur Dryomov Date: Sun, 21 Jul 2013 02:11:37 +0300 Subject: Change ComputersActivity to use swipe tabs. Such method even remembers a current tab on orientation changes. Change-Id: Iad14af060b41a4b8d225e83af450cdff0a6e78e0 --- android/sdremote/res/layout/activity_computers.xml | 6 +++ .../impressremote/activity/ComputersActivity.java | 62 +++++++++++++++++----- .../adapter/ComputersPagerAdapter.java | 42 +++++++++++++++ .../impressremote/fragment/ComputersFragment.java | 12 +++-- .../impressremote/util/ActionBarTabListener.java | 46 ---------------- 5 files changed, 105 insertions(+), 63 deletions(-) create mode 100644 android/sdremote/res/layout/activity_computers.xml create mode 100644 android/sdremote/src/org/libreoffice/impressremote/adapter/ComputersPagerAdapter.java delete mode 100644 android/sdremote/src/org/libreoffice/impressremote/util/ActionBarTabListener.java diff --git a/android/sdremote/res/layout/activity_computers.xml b/android/sdremote/res/layout/activity_computers.xml new file mode 100644 index 000000000000..6e5332d1ea67 --- /dev/null +++ b/android/sdremote/res/layout/activity_computers.xml @@ -0,0 +1,6 @@ + + \ No newline at end of file diff --git a/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java b/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java index 14ace705a644..3a5bbb6beb88 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java +++ b/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java @@ -10,23 +10,26 @@ package org.libreoffice.impressremote.activity; import android.content.Intent; import android.os.Bundle; -import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentTransaction; +import android.support.v4.view.PagerAdapter; +import android.support.v4.view.ViewPager; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; -import org.libreoffice.impressremote.util.ActionBarTabListener; -import org.libreoffice.impressremote.fragment.ComputersFragment; +import org.libreoffice.impressremote.adapter.ComputersPagerAdapter; import org.libreoffice.impressremote.util.Intents; import org.libreoffice.impressremote.R; -public class ComputersActivity extends SherlockFragmentActivity { +public class ComputersActivity extends SherlockFragmentActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setContentView(R.layout.activity_computers); setUpTabs(); + setUpComputersPager(); } private void setUpTabs() { @@ -37,26 +40,59 @@ public class ComputersActivity extends SherlockFragmentActivity { } private ActionBar.Tab buildBluetoothServersTab() { - ComputersFragment aFragment = ComputersFragment.newInstance( - ComputersFragment.Type.BLUETOOTH); - - return buildActionBarTab(aFragment, R.string.title_bluetooth); + return buildActionBarTab(R.string.title_bluetooth); } - private ActionBar.Tab buildActionBarTab(Fragment aFragment, int aTitleResourceId) { + private ActionBar.Tab buildActionBarTab(int aTitleResourceId) { ActionBar.Tab aTab = getSupportActionBar().newTab(); - aTab.setTabListener(new ActionBarTabListener(aFragment)); + aTab.setTabListener(this); aTab.setText(aTitleResourceId); return aTab; } + @Override + public void onTabSelected(ActionBar.Tab aTab, FragmentTransaction aTransaction) { + getComputersPager().setCurrentItem(aTab.getPosition()); + } + + private ViewPager getComputersPager() { + return (ViewPager) findViewById(R.id.pager_computers); + } + + @Override + public void onTabUnselected(ActionBar.Tab aTab, FragmentTransaction aTransaction) { + } + + @Override + public void onTabReselected(ActionBar.Tab aTab, FragmentTransaction aTransaction) { + } + private ActionBar.Tab buildWiFiServersTab() { - ComputersFragment aFragment = ComputersFragment.newInstance( - ComputersFragment.Type.WIFI); + return buildActionBarTab(R.string.title_wifi); + } + + private void setUpComputersPager() { + getComputersPager().setAdapter(buildComputersPagerAdapter()); + getComputersPager().setOnPageChangeListener(this); + } + + private PagerAdapter buildComputersPagerAdapter() { + return new ComputersPagerAdapter(getSupportFragmentManager()); + } + + @Override + public void onPageSelected(int aPosition) { + getSupportActionBar().setSelectedNavigationItem(aPosition); + } + + @Override + public void onPageScrolled(int aPosition, float aPositionOffset, int aPositionOffsetPixels) { + } - return buildActionBarTab(aFragment, R.string.title_wifi); + @Override + public void onPageScrollStateChanged(int aPosition) { } @Override diff --git a/android/sdremote/src/org/libreoffice/impressremote/adapter/ComputersPagerAdapter.java b/android/sdremote/src/org/libreoffice/impressremote/adapter/ComputersPagerAdapter.java new file mode 100644 index 000000000000..5a4ddaada85a --- /dev/null +++ b/android/sdremote/src/org/libreoffice/impressremote/adapter/ComputersPagerAdapter.java @@ -0,0 +1,42 @@ +package org.libreoffice.impressremote.adapter; + +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; + +import org.libreoffice.impressremote.fragment.ComputersFragment; + +public class ComputersPagerAdapter extends FragmentPagerAdapter { + private static final int PAGER_SIZE = 2; + + private static final class PagerPositions { + private PagerPositions() { + } + + public static final int BLUETOOTH = 0; + public static final int WIFI = 1; + } + + public ComputersPagerAdapter(FragmentManager aFragmentManager) { + super(aFragmentManager); + } + + @Override + public Fragment getItem(int aPosition) { + switch (aPosition) { + case PagerPositions.BLUETOOTH: + return ComputersFragment.newInstance(ComputersFragment.Type.BLUETOOTH); + + case PagerPositions.WIFI: + return ComputersFragment.newInstance(ComputersFragment.Type.WIFI); + + default: + return null; + } + } + + @Override + public int getCount() { + return PAGER_SIZE; + } +} diff --git a/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java b/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java index 18491d6cc73e..fffd1cfb5566 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java +++ b/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java @@ -97,10 +97,6 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo } private void loadComputers() { - if (!isAdded()) { - return; - } - if (!isServiceBound()) { return; } @@ -196,6 +192,8 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo super.onResume(); registerIntentsReceiver(); + + loadComputers(); } private void registerIntentsReceiver() { @@ -277,6 +275,12 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo @Override public boolean onContextItemSelected(android.view.MenuItem aMenuItem) { + if (!getUserVisibleHint()) { + // Wrong context menu could be dispatched. + // Android’s issue #20065. + return false; + } + int aComputerPosition = getListItemPosition(aMenuItem); Server aComputer = getComputersAdapter().getItem(aComputerPosition); diff --git a/android/sdremote/src/org/libreoffice/impressremote/util/ActionBarTabListener.java b/android/sdremote/src/org/libreoffice/impressremote/util/ActionBarTabListener.java deleted file mode 100644 index e21911adaee2..000000000000 --- a/android/sdremote/src/org/libreoffice/impressremote/util/ActionBarTabListener.java +++ /dev/null @@ -1,46 +0,0 @@ -/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ -package org.libreoffice.impressremote.util; - -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentTransaction; - -import com.actionbarsherlock.app.ActionBar; - -public class ActionBarTabListener implements ActionBar.TabListener { - private final Fragment mTabFragment; - - public ActionBarTabListener(Fragment aTabFragment) { - mTabFragment = aTabFragment; - } - - @Override - public void onTabSelected(ActionBar.Tab aTab, FragmentTransaction aFragmentTransaction) { - if (mTabFragment.isDetached()) { - aFragmentTransaction.attach(mTabFragment); - } - - aFragmentTransaction.replace(android.R.id.content, mTabFragment); - } - - @Override - public void onTabUnselected(ActionBar.Tab aTab, FragmentTransaction aFragmentTransaction) { - if (mTabFragment.isDetached()) { - return; - } - - aFragmentTransaction.detach(mTabFragment); - } - - @Override - public void onTabReselected(ActionBar.Tab aTab, FragmentTransaction aFragmentTransaction) { - } -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit