From ad7d4f2ff355929b5204ebec09b63aed7b249310 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Wed, 10 Oct 2018 14:27:32 +0300 Subject: [PATCH] bottom nav->tabs+icons --- .../mthmmy/activities/main/MainActivity.java | 85 ++++++++----------- .../res/color/activity_main_tabs_selector.xml | 6 ++ .../res/color/bottom_navigation_selector.xml | 5 -- app/src/main/res/drawable/ic_announcement.xml | 5 +- app/src/main/res/layout/activity_main.xml | 33 ++++--- .../main/res/menu/bottom_navigation_basic.xml | 12 --- .../res/menu/bottom_navigation_logged_in.xml | 11 --- 7 files changed, 62 insertions(+), 95 deletions(-) create mode 100644 app/src/main/res/color/activity_main_tabs_selector.xml delete mode 100644 app/src/main/res/color/bottom_navigation_selector.xml delete mode 100644 app/src/main/res/menu/bottom_navigation_basic.xml delete mode 100644 app/src/main/res/menu/bottom_navigation_logged_in.xml diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java index 8b7ccd60..3ba361df 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java @@ -4,11 +4,9 @@ import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; -import android.view.Menu; -import android.view.MenuInflater; import android.widget.Toast; -import com.google.android.material.bottomnavigation.BottomNavigationView; +import com.google.android.material.tabs.TabLayout; import java.util.ArrayList; import java.util.List; @@ -17,8 +15,8 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentPagerAdapter; import androidx.fragment.app.FragmentStatePagerAdapter; -import androidx.fragment.app.FragmentTransaction; import androidx.preference.PreferenceManager; +import androidx.viewpager.widget.ViewPager; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.LoginActivity; import gr.thmmy.mthmmy.activities.board.BoardActivity; @@ -28,6 +26,7 @@ import gr.thmmy.mthmmy.activities.main.recent.RecentFragment; import gr.thmmy.mthmmy.activities.main.shoutbox.ShoutboxFragment; import gr.thmmy.mthmmy.activities.main.unread.UnreadFragment; import gr.thmmy.mthmmy.activities.profile.ProfileActivity; +import gr.thmmy.mthmmy.activities.settings.SettingsActivity; import gr.thmmy.mthmmy.activities.topic.TopicActivity; import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.model.Board; @@ -53,7 +52,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF private static final String DRAWER_INTRO = "DRAWER_INTRO"; private long mBackPressed; private SectionsPagerAdapter sectionsPagerAdapter; - private BottomNavigationView bottomNavigation; + private ViewPager viewPager; + private TabLayout tabLayout; @Override protected void onCreate(Bundle savedInstanceState) { @@ -79,46 +79,33 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); sectionsPagerAdapter.addFragment(RecentFragment.newInstance(1), "RECENT"); sectionsPagerAdapter.addFragment(ForumFragment.newInstance(2), "FORUM"); - if (sessionManager.isLoggedIn()) + if (sessionManager.isLoggedIn()) { sectionsPagerAdapter.addFragment(UnreadFragment.newInstance(3), "UNREAD"); + sectionsPagerAdapter.addFragment(ShoutboxFragment.newInstance(4), "SHOUTBOX"); + } - FragmentTransaction initialFragmentTransaction = getSupportFragmentManager().beginTransaction(); - RecentFragment initialRecentFragment = RecentFragment.newInstance(1); - initialFragmentTransaction.add(R.id.fragment_container, initialRecentFragment); - initialFragmentTransaction.commit(); - - bottomNavigation = findViewById(R.id.main_bottom_navigation); - if (sessionManager.isLoggedIn()) bottomNavigation.inflateMenu(R.menu.bottom_navigation_logged_in); - bottomNavigation.setOnNavigationItemSelectedListener(menuItem -> { - FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); - switch (menuItem.getItemId()) { - case R.id.action_recent: - fragmentTransaction = getSupportFragmentManager().beginTransaction(); - RecentFragment recentFragment = RecentFragment.newInstance(1); - fragmentTransaction.replace(R.id.fragment_container, recentFragment); - fragmentTransaction.commit(); - return true; - case R.id.action_forum: - ForumFragment forumFragment = ForumFragment.newInstance(2); - fragmentTransaction.replace(R.id.fragment_container, forumFragment); - fragmentTransaction.commit(); - return true; - case R.id.action_unread: - UnreadFragment unreadFragment = UnreadFragment.newInstance(3); - fragmentTransaction.replace(R.id.fragment_container, unreadFragment); - fragmentTransaction.commit(); - return true; - case R.id.action_shoutbox: - ShoutboxFragment shoutboxFragment = ShoutboxFragment.newInstance(4); - fragmentTransaction.replace(R.id.fragment_container, shoutboxFragment); - fragmentTransaction.commit(); - return true; - default: - return false; - } - }); + //Set up the ViewPager with the sections adapter. + viewPager = findViewById(R.id.container); + viewPager.setAdapter(sectionsPagerAdapter); + tabLayout = findViewById(R.id.tabs); + tabLayout.setupWithViewPager(viewPager); sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + int preferredTab = Integer.parseInt(sharedPrefs.getString(SettingsActivity.DEFAULT_HOME_TAB, "0")); + if ((preferredTab != 3 && preferredTab != 4) || sessionManager.isLoggedIn()) { + tabLayout.getTabAt(preferredTab).select(); + } + for (int i = 0; i < tabLayout.getTabCount(); i++) { + if (i == 0) { + tabLayout.getTabAt(i).setIcon(getResources().getDrawable(R.drawable.ic_access_time_white_24dp)); + } else if (i == 1) { + tabLayout.getTabAt(i).setIcon(getResources().getDrawable(R.drawable.ic_forum_white_24dp)); + }else if (i == 2) { + tabLayout.getTabAt(i).setIcon(getResources().getDrawable(R.drawable.ic_fiber_new_white_24dp)); + } else if (i == 3) { + tabLayout.getTabAt(i).setIcon(getResources().getDrawable(R.drawable.ic_announcement)); + } + } setMainActivity(this); } @@ -209,6 +196,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF fragmentList.remove(position); fragmentTitleList.remove(position); notifyDataSetChanged(); + if (viewPager.getCurrentItem() == position) + viewPager.setCurrentItem(position - 1); } @Override @@ -235,15 +224,15 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF } public void updateTabs() { - if (!sessionManager.isLoggedIn() && sectionsPagerAdapter.getCount() == 3) + if (!sessionManager.isLoggedIn() && sectionsPagerAdapter.getCount() == 4) { + sectionsPagerAdapter.removeFragment(3); sectionsPagerAdapter.removeFragment(2); - else if (sessionManager.isLoggedIn() && sectionsPagerAdapter.getCount() == 2) + } + else if (sessionManager.isLoggedIn() && sectionsPagerAdapter.getCount() == 2) { sectionsPagerAdapter.addFragment(UnreadFragment.newInstance(3), "UNREAD"); - if (!sessionManager.isLoggedIn() && bottomNavigation.getMenu().size() > 2) { - bottomNavigation.getMenu().removeItem(R.id.action_shoutbox); - bottomNavigation.getMenu().removeItem(R.id.action_unread); - } else if (sessionManager.isLoggedIn() && bottomNavigation.getMenu().size() < 4) { - bottomNavigation.inflateMenu(R.menu.bottom_navigation_logged_in); + sectionsPagerAdapter.addFragment(UnreadFragment.newInstance(4), "SHOUTBOX"); + tabLayout.getTabAt(3).setIcon(R.drawable.ic_fiber_new_white_24dp); + tabLayout.getTabAt(4).setIcon(R.drawable.ic_announcement); } } //-------------------------------FragmentPagerAdapter END------------------------------------------- diff --git a/app/src/main/res/color/activity_main_tabs_selector.xml b/app/src/main/res/color/activity_main_tabs_selector.xml new file mode 100644 index 00000000..c1433b72 --- /dev/null +++ b/app/src/main/res/color/activity_main_tabs_selector.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/color/bottom_navigation_selector.xml b/app/src/main/res/color/bottom_navigation_selector.xml deleted file mode 100644 index e8c22ee2..00000000 --- a/app/src/main/res/color/bottom_navigation_selector.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_announcement.xml b/app/src/main/res/drawable/ic_announcement.xml index 7d965a41..8a402416 100644 --- a/app/src/main/res/drawable/ic_announcement.xml +++ b/app/src/main/res/drawable/ic_announcement.xml @@ -1,6 +1,7 @@ - + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8277e571..053c2fb7 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,5 +1,5 @@ - + - - - - + android:layout_height="match_parent" + app:layout_behavior="@string/appbar_scrolling_view_behavior"/> + diff --git a/app/src/main/res/menu/bottom_navigation_basic.xml b/app/src/main/res/menu/bottom_navigation_basic.xml deleted file mode 100644 index 2ec0c775..00000000 --- a/app/src/main/res/menu/bottom_navigation_basic.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/src/main/res/menu/bottom_navigation_logged_in.xml b/app/src/main/res/menu/bottom_navigation_logged_in.xml deleted file mode 100644 index 5884a579..00000000 --- a/app/src/main/res/menu/bottom_navigation_logged_in.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - \ No newline at end of file