gpt4 book ai didi

android - Android Kotlin将数据从Viewpager的基本 fragment 传递到其Tab fragment 之一

转载 作者:行者123 更新时间:2023-12-02 13:29:40 25 4
gpt4 key购买 nike

因此,在这种情况下,我有一个片段充当由两个标签组成的viewpager的基本片段。我要做的是将信息从基本片段传递到其中一个选项卡片段。

这是基本片段的代码片段

BaseFragment.kt

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

val args = arguments?.let { SwitchingHistoryDetailFragmentArgs.fromBundle(it) }

val selectedOrderId = args?.orderId //This variable is the one that i want to pass.

// Set action bar title to "Main Dashboard"
(activity as AppCompatActivity).supportActionBar?.title = "Switching History Details"

val binding: FragmentSwitchingHistoryDetailBinding = DataBindingUtil.inflate(inflater,
R.layout.fragment_switching_history_detail, container, false)


Log.i("History", "Selected History: $selectedOrderId")


binding.viewpagerMain.adapter =
MyPagerAdapter((activity as AppCompatActivity).supportFragmentManager)
binding.tabsMain.setupWithViewPager(viewpager_main)


return binding.root
}

如您在上面看到的,我要传递的变量是 selectedOrderId。这是我要将信息传递到的选项卡片段:

DetailFragment.kt
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

// Binding object for this fragment and the layout
binding = DataBindingUtil.inflate(inflater,
R.layout.fragment_switching_history_detail_tab, container, false)

....
....
....

//Return.... i don't know.
return binding.root
}

这是我正在使用的PagerAdapter:

MyPagerAdapter.kt
class MyPagerAdapter(fm: FragmentManager): FragmentStatePagerAdapter(fm){

// sebuah list yang menampung objek Fragment
private val pages = listOf(
DetailFragment(), <- This is where i want to pass the information into
DetailFragment2()
)

// menentukan fragment yang akan dibuka pada posisi tertentu
override fun getItem(position: Int): Fragment {
return pages[position]
}

override fun getCount(): Int {
return pages.size
}

// judul untuk tabs
override fun getPageTitle(position: Int): CharSequence? {
return when(position){
0 -> "Details"
else -> "Previous Changes"
}
}
}

我对viewPager很陌生。我尝试使用Intent,但不起作用。有没有办法做到这一点?如果我想指出任何细节,请随时提出。

最佳答案

简单的方法是将selectedOrderId传递给适配器,如下所示:

binding.viewpagerMain.adapter =
MyPagerAdapter((activity as AppCompatActivity).supportFragmentManager,selectedOrderId)

然后从 PagerAdapter将该orderId传递给片段构造函数,如下所示:
class MyPagerAdapter(fm: FragmentManager,val orderId : String): FragmentStatePagerAdapter(fm){

// sebuah list yang menampung objek Fragment
private val pages = listOf(
DetailFragment.getInstance(orderId), <- This is where i want to pass the information into
DetailFragment2()
)

// menentukan fragment yang akan dibuka pada posisi tertentu
override fun getItem(position: Int): Fragment {
return pages[position]
}

override fun getCount(): Int {
return pages.size
}

// judul untuk tabs
override fun getPageTitle(position: Int): CharSequence? {
return when(position){
0 -> "Details"
else -> "Previous Changes"
}
}
}

您的详细信息片段如下所示:
class DetailFragment : Fragment() { 

private var orderId: String? = null


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

// Binding object for this fragment and the layout
binding = DataBindingUtil.inflate(inflater,
R.layout.fragment_switching_history_detail_tab, container, false)



if (arguments != null) {
orderId = arguments?.getString("orderId")
}
// do whatever you want to do with this variable

return binding.root
}

companion object {

/**
* Method used to get the instance of the fragment.
*
* @return fragment instance
*/
@JvmStatic
fun getInstance(orderId: String): DetailFragment {
val fragment = DetailFragment()
val bundle = Bundle()
bundle.putString("orderId", orderId)
fragment.arguments = bundle
return fragment
}
}
}

如果您对此有任何疑问,请告诉我。愉快的编码:)

关于android - Android Kotlin将数据从Viewpager的基本 fragment 传递到其Tab fragment 之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62228493/

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