gpt4 book ai didi

android - Kotlin:不同 fragment 之间的通信

转载 作者:行者123 更新时间:2023-12-02 13:22:16 27 4
gpt4 key购买 nike

我目前正在使用 Kotlin 开发 Android 应用。

我正在使用默认的抽屉导航和 fragment 。我想做这样的事情:

  1. 在当前 fragment 上,将一些数据放入微调器中(完成)
  2. 单击 TextView 时,保持数据(来自选定的微调器)处于选中状态(几乎完成),然后 (3)
  3. 转到另一个 fragment (完成)
  4. 在这个 fragment 上,用一些数据创建一个微调器并将它们发送到第一个 fragment (几乎完成)
  5. 在第一个 fragment 中插入新值和旧值

所以我有两个微调器,第二个微调器(在第二个 fragment 上)有一个适配器。

我的问题是:有没有办法更轻松地做到这一点?我正在为 fragment 和适配器之间的所有这些 bundle 而苦苦挣扎,但我真的相信有一种比我正在做的更简单的方法......

第一个 fragment :

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.mes_informations, container, false)
val thisBundle = this.arguments
if(thisBundle != null){
val builder = StringBuilder("Extras:\n")
for (key in thisBundle.keySet()) {
val value = thisBundle.get(key)
builder.append(key).append(": ").append(value).append("\n")
}
selectedArret.text = thisBundle.get("Arret").toString()
}

return v
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val thisBundle = this.arguments

if(thisBundle != null){
val builder = StringBuilder("Extras:\n")
for (key in thisBundle.keySet()) {
val value = thisBundle.get(key)
builder.append(key).append(": ").append(value).append("\n")
}
Log.i(TAG, builder.toString())
}

bundle = Bundle()


spinnerDepartement.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
when (position) {
position -> departement = bundle.putString("departement", spinnerDepartement.selectedItem.toString())
else -> bundle.putString("departement", "Cher")
}
Log.i(TAG, spinnerDepartement.selectedItem.toString())
}

override fun onNothingSelected(parent: AdapterView<*>) {
}
}


val fragmentTransaction = fragmentManager?.beginTransaction()
val rechercheFragm = RechercherArret()
rechercheFragm.arguments = bundle
ligneReguliereLayout.setOnClickListener {
fragmentTransaction
?.replace(R.id.content_frame, rechercheFragm)
?.addToBackStack(null)
?.commit()
}
}

第二个:

   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
lignes = ArrayList()

val resultArgument = arguments

val queryFill = resources.getStringArray(R.array.fillSearchQuery2)
for(ligne in queryFill){
lignes.add(ligne)
}

adapter = ListAdapterCustom(view.context, R.layout.list_adapter, lignes, resultArgument)

listSearchView.adapter = adapter

search.queryHint = "Entrez un arrêt"

search.setOnQueryTextListener(object : SearchView.OnQueryTextListener {

override fun onQueryTextChange(newText: String): Boolean {
adapter.filter.filter(newText.trim())
if(newText.trim() != ""){
listSearchView.visibility = View.VISIBLE
}else{
listSearchView.visibility = View.GONE
}
return false
}

override fun onQueryTextSubmit(query: String): Boolean {
Toast.makeText(view.context, "Submit $query", Toast.LENGTH_SHORT).show()
return false
}

})
}

和适配器:

class ListAdapterCustom(context: Context, resource: Int, list: ArrayList<String>, private val arguments: Bundle?) : ArrayAdapter<String>(context, resource, list) {


override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = super.getView(position, convertView, parent)

val thisBundle = arguments
if(thisBundle != null){
val builder = StringBuilder("Extras:\n")
for (key in thisBundle.keySet()) {
val value = thisBundle.get(key)
builder.append(key).append(": ").append(value).append("\n")
}
Log.i("Extras", builder.toString())
}

val arret = view.findViewById<TextView>(R.id.arret)

arret.setOnClickListener {
val fragment = MesInformations()
val bundle = Bundle()
bundle.putString("Arret", arret.text.toString())
fragment.arguments = bundle

Snackbar.make(view, arret.text, Snackbar.LENGTH_SHORT).show()

val fragmentManager = (context as AppCompatActivity).supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()

fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack(null)
fragmentTransaction.commit()
fragmentTransaction.addToBackStack(null)
}

if (position % 2 == 1) {
view.setBackgroundResource(R.color.colorWhite)
} else {
view.setBackgroundResource(R.color.grayBackground)
}
return view
}

最佳答案

您可以创建共享的 ViewModel 以在 fragment 之间进行通信。创建一个 ViewModel 并使用每个 fragment 内的托管 Activity 上下文访问它们。

这是一个 ViewModel 文档中复制的示例:https://developer.android.com/topic/libraries/architecture/viewmodel

class SharedViewModel : ViewModel() {
val selected = MutableLiveData<Item>()

fun select(item: Item) {
selected.value = item
}
}

class MasterFragment : Fragment() {

private lateinit var itemSelector: Selector

private lateinit var model: SharedViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
model = activity?.run {
ViewModelProviders.of(this).get(SharedViewModel::class.java)
} ?: throw Exception("Invalid Activity")

itemSelector.setOnClickListener { item ->
model.select(item) // <-- This will notify the `DetailFragment`
}
}
}

class DetailFragment : Fragment() {

private lateinit var model: SharedViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
model = activity?.run {
ViewModelProviders.of(this).get(SharedViewModel::class.java)
} ?: throw Exception("Invalid Activity")

model.selected.observe(this, Observer<Item> { item ->
// Update the UI
})
}
}

这里,SharedViewModelMasterFragmentDetailFragment 中被访问。两者都在访问 SharedViewModel 的同一个实例,因为它们都从 Activity 的上下文访问 ViewModel:

ViewModelProviders.of(*ACTIVITY*).get(SharedViewModel::class.java)

现在您可以在 SharedViewModel 中包含一些 LiveData 并且两个 fragment 都可以监听/更新它们,这最终也会反射(reflect)在另一个 fragment 上。

关于android - Kotlin:不同 fragment 之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53671423/

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