Back button to navigate tabs on main view

This commit is contained in:
Eugen Rochko 2017-03-07 11:02:41 +01:00
parent d299dd34ed
commit 9ec3fd47dc
2 changed files with 44 additions and 2 deletions

View file

@ -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<Integer> pageHistory = new Stack<Integer>();
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());
}
}
}

View file

@ -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" />
<Space
android:layout_width="wrap_content"