带有打开其他子片段的片段的 Android 导航抽屉



我正在开发一个使用导航抽屉的Android应用程序(我是新手(。我创建了多个片段来表示侧菜单中的各种项目。其中一个具有RecyclerView,当用户单击RecyclerView的其中一个项目时,我想显示另一个包含各种详细信息的片段。

我已经成功创建了结构,在 RecyclerView 项上实现了单击侦听器,但我不知道如何显示详细信息片段(以及如何从中返回(。

如果显示另一个片段不是正确的方法,请建议我获取所需导航的最佳方式。

法典

菜单活动.xml

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
android:theme="@style/AppTheme.AppBarOverlay"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:titleTextColor="@android:color/white"/>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"/>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/drawer_view"
app:headerLayout="@layout/nav_header"/>

MenuActivity.kt

class MenuActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
val vendutoFragment = VendutoFragment()
val prezziFragment = PrezziFragment()
val giacenzeFragment = GiacenzeFragment()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_menu)
setSupportActionBar(toolbar)
val toggle = ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
navigationView.setNavigationItemSelectedListener(this)
if(savedInstanceState == null){
addFragment(vendutoFragment)
navigationView.setCheckedItem(nav_venduto)
}
}
override fun onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
var selectedFragment = Fragment()
when (item.itemId) {
R.id.nav_venduto -> {
selectedFragment = vendutoFragment
}
R.id.nav_prezzi -> {
selectedFragment = prezziFragment
}
R.id.nav_giacenze -> {
selectedFragment = giacenzeFragment
}
}
replaceFragment(selectedFragment)
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
private fun addFragment(fragment: Fragment){
supportFragmentManager.beginTransaction().add(R.id.frameLayout, fragment).commit()
}
private fun replaceFragment(fragment: Fragment){
supportFragmentManager.beginTransaction().replace(R.id.frameLayout, fragment).commit()
}

}

带有回收器视图和可点击项目的第一个片段

class GiacenzeFragment: Fragment(){
var global: Global? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
{
//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments
return inflater.inflate(R.layout.fragment_giacenze, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{
super.onViewCreated(view, savedInstanceState)
//you can set the title for your toolbar here for different fragments different titles
activity!!.title = "Giacenze"
}
override fun onActivityCreated(savedInstanceState: Bundle?)
{
super.onActivityCreated(savedInstanceState)
giacenzeTable.layoutManager = LinearLayoutManager(context) as RecyclerView.LayoutManager?
global = getActivity()?.getApplication() as Global
// Access the RecyclerView Adapter and load the data into it
giacenzeTable.adapter = GiacenzeTableAdapter(global!!.prodotti, { prodotto: Prodotto -> prodottoItemClicked(prodotto) })
}
private fun prodottoItemClicked(prodotto: Prodotto)
{
Toast.makeText(context, "Clicked: ${prodotto.name}", Toast.LENGTH_SHORT).show()
var serbatoiFrag =  SerbatoiFragment()
serbatoiFrag.idProdotto = prodotto.idProdotto
serbatoiFrag.nameProdotto = prodotto.name
fragmentManager?.beginTransaction()!!.replace(R.id.frameLayout, serbatoiFrag).commit()
}

}

如果你想在新活动中加载片段,只需打开新活动并使用replaceFragment如果你想加载相同的活动,只需使用replaceFragment你知道如何使用replaceFragment

吗?

相关内容

  • 没有找到相关文章

最新更新