侧滑菜单和Fragment的使用
程序开发
2023-09-18 15:48:44
1.侧滑菜单的设计和制作
制作侧滑菜单,有“我的小狗狗,我的小老鼠,我的小兔子,我的羊羊”四个菜单项,并实现对侧滑菜单的控制(打开和关闭)
2.Fragment 的动态加载
添加4个Fragment,并显示4个小宠物,点击侧滑菜单,加载对应的Fragment,并实现Fragment的回退操作
activity_main.xml
fragment_bag.xml
fragment_file.xml
fragment_photo.xml
fragment_save.xml
MainActivity.kt
package com.example.mymenuimport androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.TextView
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.example.mymenu.fragment.BagFragment
import com.example.mymenu.fragment.FileFragment
import com.example.mymenu.fragment.PhotoFragment
import com.example.mymenu.fragment.SaveFragmentclass MainActivity : AppCompatActivity() {lateinit var listView_menu: ListViewlateinit var drawerLayout: DrawerLayoutlateinit var tv_title:TextViewlateinit var menus:Listlateinit var fragments:Listlateinit var fragmentNow:Fragment//存放当前正在显示的Fragmentoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)listView_menu=findViewById(R.id.listview_menu)drawerLayout=findViewById(R.id.drawerlayout)tv_title=findViewById(R.id.tv_title)init_menu()init_Fragment()loadFragment()listView_menu.setOnItemClickListener { adapterView, view, i, l ->tv_title.setText(menus[i])openLeftMenu()replaceFragment(fragments[i])}}/*** left菜单打开关闭*/fun openLeftMenu(){if(drawerLayout.isDrawerOpen(Gravity.LEFT)){drawerLayout.closeDrawer(Gravity.LEFT)}else{drawerLayout.openDrawer(Gravity.LEFT)}}/*** right菜单打开关闭*/fun openRightMenu(){if(drawerLayout.isDrawerOpen(Gravity.RIGHT)){drawerLayout.closeDrawer(Gravity.RIGHT)}else{drawerLayout.openDrawer(Gravity.RIGHT)}}/****/fun init_Fragment(){var bagFragment=BagFragment();var fileFragment=FileFragment();var photoFragment=PhotoFragment();var saveFragment=SaveFragment();fragments= listOf(bagFragment,photoFragment,fileFragment,saveFragment)//}/****/fun init_menu(){menus= listOf("我的小狗狗","我的小老鼠","我的小兔子","我的羊羊")//定义适配器var arrayAdapter:ArrayAdapter=ArrayAdapter(this,android.R.layout.activity_list_item,android.R.id.text1,menus)listView_menu.adapter=arrayAdapter//设置适配器}/*** Fragment*/fun loadFragment(){var fm:FragmentManager=supportFragmentManagervar ft:FragmentTransaction=fm.beginTransaction();ft.add(R.id.content_layout,fragments[0])ft.addToBackStack(null)fragmentNow=fragments[0]ft.commit()}fun replaceFragment(fragment: Fragment){var fm:FragmentManager=supportFragmentManagervar ft:FragmentTransaction=fm.beginTransaction()if (fragment.isAdded){//判断有没有添加过ft.hide(fragmentNow).show(fragment)}else{ft.hide(fragmentNow).add(R.id.content_layout,fragment)ft.addToBackStack(null)}fragmentNow=fragmentft.commit()}fun menu1(view: View) {tv_title.setText("菜单1")openRightMenu()}fun menu2(view: View) {tv_title.setText("菜单2")openRightMenu()}fun menu3(view: View) {tv_title.setText("菜单3")openRightMenu()}fun menu4(view: View) {tv_title.setText("菜单4")openRightMenu()}}
BagFragment.kt (其余类似)
package com.example.mymenu.fragmentimport android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.mymenu.Rclass BagFragment : Fragment() {override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_bag, container, false)}
}
标签:
相关文章
-
无相关信息