0

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.

user9333933
  • 247
  • 1
  • 3
  • 14
  • It's basically the same as changing any other `Activity` into a `Fragment` – extend `Fragment` instead, inflate your layout in `onCreateView()` instead of calling `setContentView()`, and call `setHasOptionsMenu(true)`. The only difference in this case would be passing `childFragmentManager` to your `SectionsPagerAdapter` instead of `supportFragmentManager`. – Mike M. Apr 16 '19 at 03:05
  • Ohh cool thanks I'll try this. Any idea why I have negative votes for this question? – user9333933 Apr 16 '19 at 11:17
  • The guidelines for downvoting are in a tooltip on that button: "This question does not show any research effort; it is unclear or not useful". I wouldn't say that this is terribly unclear (maybe a little), or not at all useful, but I do know that there are many, many posts here that cover changing an `Activity` into a `Fragment`, so maybe lack of research? – Mike M. Apr 16 '19 at 11:22
  • 1
    Ah, I guess I didn't realize to search for turning my activity into a fragment, I was just looking for how to embed tabbed activity into a bottom nav, eek. That actually does make my search a lot easier, thanks a ton – user9333933 Apr 16 '19 at 11:27
  • One more question. How do I actually make my fragment show up onCreate and on "home" button click of the main activity? – user9333933 Apr 16 '19 at 11:33
  • That's gonna be a little more involved. `BottomNavigationView` is itself often (usually?) used with `Fragment`s, either in a `ViewPager`, or just transacted in and out manually. However, with your setup, it's kinda jamming two different patterns together; plain `View` manipulation, and an odd-one-out `Fragment`. Showing the `Fragment` is simple enough – `supportFragmentManager.beginTransaction().replace()...` – but if you stay with `ListView` on the other two, you're gonna have to hide/show that somehow, which is a little unwieldy. Might be easier to do all `Fragment`s in `MainActivity`, too. – Mike M. Apr 16 '19 at 11:44
  • I'm a little lost, what I was doing previously was just clearing my listview and populating it with the data necessary within each listview. However what's confusing me is what you mean by do all Fragments in MainActivity. – user9333933 Apr 16 '19 at 12:07
  • I meant, create `Fragment`s for `navigation_dashboard` and `navigation_notifications`, too. I was just saying that it _might_ be easier to do that, because `FragmentTransaction`s can take care of hiding the other `Fragment`s when you show a particular one. Otherwise, as I kind of described above, you will have to take care of hiding/showing that `ListView` yourself, because a `FragmentTransaction` does not affect any `View`s that don't belong to a `Fragment`. You don't really have to do that, though. Whatever's easier for you to tackle. – Mike M. Apr 16 '19 at 12:22
  • Pretty much everything is working, except switching tabs does nothing currently. I'm not sure how to re-write what's in my tabbedactivity/fragment container.adapter = mSectionsPagerAdapter container.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs)) tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(container)) – user9333933 Apr 17 '19 at 14:04
  • I am trying is to use my view object by saying v.container, but I'm getting a null pointer exception on this line tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(v.container)) – user9333933 Apr 17 '19 at 14:19
  • I just realized what I'm doing wrong. I was using tabs but not specifying the view, so it came out null. It's all working. Thanks for the help!! – user9333933 Apr 17 '19 at 14:30

0 Answers0