MainActivity.kt 3.39 KB
Newer Older
Nihar Thakkar's avatar
Nihar Thakkar committed
1
2
3
4
package io.eelo.appinstaller

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
5
6
7
8
import android.support.design.internal.BottomNavigationItemView
import android.support.design.internal.BottomNavigationMenuView
import android.support.design.widget.BottomNavigationView
import android.annotation.SuppressLint
9
import android.content.Context
10
11
12
13
14
15
16
import android.support.v4.app.Fragment
import android.view.MenuItem
import io.eelo.appinstaller.categories.CategoriesFragment
import io.eelo.appinstaller.home.HomeFragment
import io.eelo.appinstaller.search.SearchFragment
import io.eelo.appinstaller.settings.SettingsFragment
import io.eelo.appinstaller.updates.UpdatesFragment
17
import kotlinx.android.synthetic.main.activity_main.*
Nihar Thakkar's avatar
Nihar Thakkar committed
18

19
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener {
20
    private var currentFragment: Fragment? = null
21
22
    private val homeFragment = HomeFragment()
    private val categoriesFragment = CategoriesFragment()
23
    private val searchFragment = SearchFragment()
24
25
    private val updatesFragment = UpdatesFragment()
    private val settingsFragment = SettingsFragment()
Nihar Thakkar's avatar
Nihar Thakkar committed
26
27
28
29

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
30
31

        // Show the home fragment by default
32
        showFragment(homeFragment)
33

34
        bottom_navigation_view.setOnNavigationItemSelectedListener(this)
35
36

        // Disable shifting of nav bar items
37
        removeShiftMode(bottom_navigation_view)
Nihar Thakkar's avatar
Nihar Thakkar committed
38
    }
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when {
            item.itemId == R.id.menu_home -> {
                showFragment(homeFragment)
                return true
            }
            item.itemId == R.id.menu_categories -> {
                showFragment(categoriesFragment)
                return true
            }
            item.itemId == R.id.menu_search -> {
                showFragment(searchFragment)
                return true
            }
            item.itemId == R.id.menu_updates -> {
                showFragment(updatesFragment)
                return true
            }
            item.itemId == R.id.menu_settings -> {
                showFragment(settingsFragment)
                return true
            }
        }
        return false
    }

    private fun showFragment(fragment: Fragment) {
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.frame_layout, fragment)
                .commit()
71
        currentFragment = fragment
72
73
    }

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    @SuppressLint("RestrictedApi")
    private fun removeShiftMode(bottomNavigationView: BottomNavigationView) {
        val menuView = bottomNavigationView.getChildAt(0) as BottomNavigationMenuView
        try {
            val mShiftingMode = menuView.javaClass.getDeclaredField("mShiftingMode")
            mShiftingMode.isAccessible = true
            mShiftingMode.setBoolean(menuView, false)
            mShiftingMode.isAccessible = false
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }

        for (i in 0 until menuView.childCount) {
            val itemView = menuView.getChildAt(i) as BottomNavigationItemView
            itemView.setShiftingMode(false)
            itemView.setChecked(itemView.itemData.isChecked)
        }
    }

Nihar Thakkar's avatar
Nihar Thakkar committed
95
}