Currently I'm trying to integrate two different activities. One is my mainAcitivity, which is a bottom navigation activity and the other is my tabbedActivity, which is my activity with tabs. I'm not sure how to get my tabbedActivity to fit in within my MainActivity, so one section of the bottom navigation (the home) would house the tabs. Can I create a fragment that parents the tabbedActivity? And if so what would that look like?
All of my fragments just contain listviews, the only difference is the contents of the list view. Also, I didn't include PlaceHolderFragment2, because it's the same as PlaceHolderFragment, except the pre-populated arraylist
class MainActivity : AppCompatActivity() {
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
// TODO this tab will have a tabbed frag
R.id.navigation_home -> {
val your_array_list = ArrayList<String>()
your_array_list.add("one")
your_array_list.add("two")
val arrayAdapter = ArrayAdapter(this,
android.R.layout.simple_list_item_1,
your_array_list)
menu.adapter = arrayAdapter
return@OnNavigationItemSelectedListener true
}
R.id.navigation_dashboard -> {
val your_array_list = ArrayList<String>()
your_array_list.add("three")
your_array_list.add("four")
val arrayAdapter = ArrayAdapter(this,
android.R.layout.simple_list_item_1,
your_array_list)
menu.adapter = arrayAdapter
return@OnNavigationItemSelectedListener true
}
R.id.navigation_notifications -> {
val your_array_list = ArrayList<String>()
your_array_list.add("five")
your_array_list.add("six")
val arrayAdapter = ArrayAdapter(this,
android.R.layout.simple_list_item_1,
your_array_list)
menu.adapter = arrayAdapter
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}
}
class TabbedActivity : AppCompatActivity() {
private var mSectionsPagerAdapter: SectionsPagerAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tabbed)
setSupportActionBar(toolbar)
mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)
container.adapter = mSectionsPagerAdapter
container.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs))
tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(container))
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_tabbed, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == R.id.action_settings) {
return true
}
return super.onOptionsItemSelected(item)
}
inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
override fun getItem(position: Int): Fragment {
when(position) {
0 -> return PlaceholderFragment()
else -> return PlaceholderFragment2()
}
}
override fun getCount(): Int {
return 3
}
}
class PlaceholderFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_tabbed, container, false)
val your_array_list = ArrayList<String>()
your_array_list.add("a")
your_array_list.add("b")
val arrayAdapter = ArrayAdapter(
context!!,
android.R.layout.simple_list_item_1,
your_array_list
)
rootView.tabsMenu.adapter = arrayAdapter
return rootView
}
}
}
Currently I can't get these two activities to integrate. Ideally I would be able to see tabs within one section of my bottom navigation.