From 9ec3fd47dc4dfff3034cf97a749a449fd665ddfa Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 7 Mar 2017 11:02:41 +0100 Subject: [PATCH] Back button to navigate tabs on main view --- .../com/keylesspalace/tusky/MainActivity.java | 44 ++++++++++++++++++- app/src/main/res/layout/item_follow.xml | 2 +- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index 899bd1a3..acaf77da 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -43,6 +43,7 @@ import org.json.JSONObject; import java.util.HashMap; import java.util.Map; +import java.util.Stack; public class MainActivity extends BaseActivity { private static final String TAG = "MainActivity"; // logging tag and Volley request tag @@ -52,6 +53,8 @@ public class MainActivity extends BaseActivity { private boolean notificationServiceEnabled; private String loggedInAccountId; private String loggedInAccountUsername; + Stack pageHistory = new Stack(); + private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { @@ -81,16 +84,44 @@ public class MainActivity extends BaseActivity { getString(R.string.title_public) }; adapter.setPageTitles(pageTitles); - ViewPager viewPager = (ViewPager) findViewById(R.id.pager); + viewPager = (ViewPager) findViewById(R.id.pager); int pageMargin = getResources().getDimensionPixelSize(R.dimen.tab_page_margin); viewPager.setPageMargin(pageMargin); Drawable pageMarginDrawable = ThemeUtils.getDrawable(this, R.attr.tab_page_margin_drawable, R.drawable.tab_page_margin_dark); viewPager.setPageMarginDrawable(pageMarginDrawable); viewPager.setAdapter(adapter); + TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); tabLayout.setupWithViewPager(viewPager); + tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { + @Override + public void onTabSelected(TabLayout.Tab tab) { + viewPager.setCurrentItem(tab.getPosition()); + + if (pageHistory.empty()) { + pageHistory.push(0); + } + + if (pageHistory.contains(tab.getPosition())) { + pageHistory.remove(pageHistory.indexOf(tab.getPosition())); + } + + pageHistory.push(tab.getPosition()); + } + + @Override + public void onTabUnselected(TabLayout.Tab tab) { + + } + + @Override + public void onTabReselected(TabLayout.Tab tab) { + + } + }); + // Retrieve notification update preference. SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); notificationServiceEnabled = preferences.getBoolean("pullNotifications", true); @@ -218,6 +249,17 @@ public class MainActivity extends BaseActivity { return true; } } + return super.onOptionsItemSelected(item); } + + @Override + public void onBackPressed() { + if(pageHistory.empty()) { + super.onBackPressed(); + } else { + pageHistory.pop(); + viewPager.setCurrentItem(pageHistory.lastElement()); + } + } } diff --git a/app/src/main/res/layout/item_follow.xml b/app/src/main/res/layout/item_follow.xml index 4f732fb4..7844731d 100644 --- a/app/src/main/res/layout/item_follow.xml +++ b/app/src/main/res/layout/item_follow.xml @@ -83,7 +83,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/notification_username" - android:textColor="?attr/status_text_color_secondary" /> + android:textColor="?android:textColorSecondary" />