在底部导航活动/片段中创建选项卡式活动

时间:2019-04-16 02:54:47

标签: android bottomnavigationview android-tabbed-activity

目前,我正在尝试整合两个不同的活动。一个是我的mainAcitivity,这是一个底部导航活动,另一个是我的tabbedActivity,这是我的带有标签的活动。我不确定如何使我的tabbedActivity适应我的MainActivity,因此底部导航(首页)的一部分会容纳这些标签。我可以创建作为tabbedActivity父母的片段吗?如果是这样,那会是什么样?

我所有的片段都只包含列表视图,唯一的区别是列表视图的内容。另外,我没有包含PlaceHolderFragment2,因为它与PlaceHolderFragment相同,除了预先填充的arraylist

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

目前,我无法将这两个活动整合在一起。理想情况下,我可以在底部导航的某一部分中看到标签。

0 个答案:

没有答案