Browse Source

bottom nav->tabs+icons

pull/61/merge
Thodoris1999 6 years ago
parent
commit
ad7d4f2ff3
  1. 85
      app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java
  2. 6
      app/src/main/res/color/activity_main_tabs_selector.xml
  3. 5
      app/src/main/res/color/bottom_navigation_selector.xml
  4. 5
      app/src/main/res/drawable/ic_announcement.xml
  5. 33
      app/src/main/res/layout/activity_main.xml
  6. 12
      app/src/main/res/menu/bottom_navigation_basic.xml
  7. 11
      app/src/main/res/menu/bottom_navigation_logged_in.xml

85
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-------------------------------------------

6
app/src/main/res/color/activity_main_tabs_selector.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/accent" android:state_selected="true" />
<item android:color="@color/white" android:state_selected="false" />
</selector>

5
app/src/main/res/color/bottom_navigation_selector.xml

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/accent" android:state_checked="true" />
<item android:color="@color/white" android:state_checked="false" />
</selector>

5
app/src/main/res/drawable/ic_announcement.xml

@ -1,6 +1,7 @@
<vector android:height="24dp" android:viewportHeight="297"
android:viewportWidth="297" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#ffffff"
android:viewportWidth="297" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"
android:tint="#FFFFFF">
<path android:fillColor="#FF000000"
android:pathData="M282.82,107.559h-25.792V10.025c0,-4.056 -2.443,-7.711 -6.19,-9.261c-3.745,-1.555 -8.059,-0.693 -10.925,2.177C195.46,47.434 157.039,68.643 132.635,78.6C106.33,89.333 90.261,89.635 90.159,89.637H14.18c-5.536,0 -10.023,4.488 -10.023,10.024v89.631c0,5.536 4.487,10.023 10.023,10.023h10.2l37.887,91.497c1.551,3.746 5.206,6.189 9.261,6.189h55.538c0.006,-0.001 0.012,-0.001 0.02,0c5.536,0 10.023,-4.488 10.023,-10.023c0,-1.588 -0.37,-3.09 -1.025,-4.424l-33.809,-81.646c22.4,4.443 73.884,21.285 137.641,85.1c1.917,1.921 4.483,2.939 7.094,2.939c0.055,0 0.109,0 0.164,0c5.468,-0.079 9.877,-4.536 9.877,-10.023c0,-0.214 -0.006,-0.428 -0.02,-0.639l-0.002,-96.896h25.792c5.536,0 10.023,-4.488 10.023,-10.024v-53.779C292.844,112.048 288.356,107.559 282.82,107.559zM24.204,109.683h55.932v69.584H24.204V109.683zM78.226,276.952l-31.139,-75.196h33.839l31.138,75.196H78.226zM100.183,180.201v-71.452c20.889,-3.123 72.28,-16.674 136.797,-75.301v84.121v0.015v53.779c0,0.008 0,0.017 0,0.025l0.002,84.111C172.466,196.876 121.072,183.326 100.183,180.201zM272.796,161.34h-15.768v-33.732h15.768V161.34z"
android:strokeColor="#ffffff" android:strokeWidth="1"/>
</vector>

33
app/src/main/res/layout/activity_main.xml

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
@ -14,22 +14,21 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ToolbarTheme">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/accent"
app:tabTextColor="@color/white"
app:tabIconTint="@color/activity_main_tabs_selector" />
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@+id/fragment_container"
<androidx.viewpager.widget.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/main_bottom_navigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/primary"
app:itemIconTint="@color/bottom_navigation_selector"
app:itemTextColor="@color/bottom_navigation_selector"
app:menu="@menu/bottom_navigation_basic"
app:labelVisibilityMode="labeled"/>
</RelativeLayout>
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

12
app/src/main/res/menu/bottom_navigation_basic.xml

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_recent"
android:icon="@drawable/ic_access_time_white_24dp"
android:title="@string/recent" />
<item
android:id="@+id/action_forum"
android:icon="@drawable/ic_forum_white_24dp"
android:title="@string/forum" />
</menu>

11
app/src/main/res/menu/bottom_navigation_logged_in.xml

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_unread"
android:icon="@drawable/ic_fiber_new_white_24dp"
android:title="@string/unread" />
<item
android:id="@+id/action_shoutbox"
android:icon="@drawable/ic_announcement"
android:title="@string/shoutbox" />
</menu>
Loading…
Cancel
Save