gpt4 book ai didi

android - 单击列表项时如何从一个 fragment 切换到另一个 fragment ?

转载 作者:行者123 更新时间:2023-11-29 15:33:57 25 4
gpt4 key购买 nike

我在“所有用户” fragment 中有一个列表。单击列表中的名称时,它应该切换到“用户详细信息” fragment 。我似乎无法找到一种方法来以某种方式破坏这个 fragment 并调出新 fragment ,或者只是在旧 fragment 之上显示新 fragment 。

使用的语言是 Kotlin。我对此很陌生。我尝试了一些使用 Fragment Manager 的方法,但似乎无法找到正确的语法。

主要 Activity .kt

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

//Default view when app starts
title = resources.getString(R.string.allusers)
loadFragment(AllUsersFragment())

//Bottom Navigation Bar
navigationView.setOnNavigationItemSelectedListener {
when (it.itemId) {
R.id.navigation_allusers -> {
title = resources.getString(R.string.allusers)
loadFragment(AllUsersFragment())
return@setOnNavigationItemSelectedListener true
}

R.id.navigation_transfer -> {
title = resources.getString(R.string.transfer)
loadFragment(TransferFragment())
return@setOnNavigationItemSelectedListener true
}

R.id.navigation_logs -> {
title = resources.getString(R.string.logs)
loadFragment(LogsFragment())
return@setOnNavigationItemSelectedListener true
}

}
false

}
}

//function to load fragment when bottom navigation is clicked

fun loadFragment(fragment: Fragment) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.container, fragment)
transaction.addToBackStack(null)
transaction.commit()
}

}

AllUsersFragment.kt

class AllUsersFragment : Fragment(){

private lateinit var listView:ListView

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_allusers, container, false)
val details = UserDetailsFragment()
val bundle = Bundle() //To transfer information

listView = rootView.findViewById(R.id.usersList)

val userNames = arrayOf(
"Andrew Jackson",
"Barry Alan",
"Caitlyn Snow",
"Drake Ramoray"
)

val adapter = ArrayAdapter(context, android.R.layout.simple_list_item_1, userNames)
listView.adapter = adapter

//List view item clicked listener
listView.onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, i, l ->

val userName = userNames[i]
bundle.putString("str", userName)
bundle.putInt("int", i)
details.arguments = bundle
}

return rootView
}
}

用户详细信息.kt

class UserDetailsFragment : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_user_details,container,false)

val nameField = rootView.findViewById<TextView>(R.id.nameField)
val imageField = rootView.findViewById<ImageView>(R.id.imageField)

//Importing images
val andrewjackson = BitmapFactory.decodeResource(context!!.resources,R.drawable.andrewjackson)
val barryalan = BitmapFactory.decodeResource(context!!.resources,R.drawable.barryalan)
val caitlynsnow = BitmapFactory.decodeResource(context!!.resources,R.drawable.caitlynsnow)
val drakeramoray = BitmapFactory.decodeResource(context!!.resources,R.drawable.drakeramoray)

val userImages = ArrayList<Bitmap>()

userImages.add(andrewjackson)
userImages.add(barryalan)
userImages.add(caitlynsnow)
userImages.add(drakeramoray)

val name = arguments!!.getString("str") //Displays name clicked on Details Page
nameField.setText(name)

val i =arguments!!.getInt("int") //Displays image of user from array
imageField.setImageBitmap(userImages[i])

return rootView
}
}

我认为在 MainActivity.kt 中使用 loadFragment 函数可能是可行的,但我想不出在 fragment 中使用它的方法。

预期输出:

单击列表中的名称时,它应更改为同一容器中显示用户详细信息的另一个 fragment 。

最佳答案

您可以在 fragment 中使用此代码来替换容器中的 fragment

val someFragment = YourUserDetailsFragment()
val transaction = fragmentManager!!.beginTransaction()
transaction.replace(R.id.container, someFragment)
transaction.addToBackStack(null)
transaction.commit()

关于android - 单击列表项时如何从一个 fragment 切换到另一个 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56784628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com