如何将ViewModel添加到使用ViewPager和Tablayout的项目中



你好,我是安卓开发的新手,所以这个问题对你来说可能很奇怪。

我有一个名为IdentityCardInfo的类,它包含变量。我在InfoFragment中得到了这些变量。

在InfoFragment中,我想在ResultsFragment中显示这些变量,它有3个选项卡和tablayout。

我想使用ViewModel将数据传递到表格布局,但我不知道如何使用。我已经搜索过了,但遇到了问题。

以下是IdentityCardInfo类的代码:

class IdentityCardInfo {
companion object AppConstant{
const val serieNo ="67G74444"
const val validDate = "01/01/2022"
const val dateOfBirth = "10/08/1998"
const val fullName = "Muhittin KAYA"
const val identityNo: Long = 11111111111
const val gender = "MALE"
const val nationality = "TUR"
}
}

以下是InfoFragment:的代码

class InfoFragment : BaseFragment() {
lateinit var navController: NavController
companion object {
private const val TAG = "Info Fragment"
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_info, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
button_info_idcard.setOnClickListener {
//start OcrActivity
val intent = Intent(activity, OcrActivity::class.java)
startActivityForResult(intent, 101)
}
imagebutton_info_settings.setOnClickListener {
navController.navigate(R.id.action_infoFragment_to_settingsFragment)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Log.i("Muhittin", "onActivityResult()")
if (requestCode == 101) {
val message = data?.getStringExtra("TEST_TEXT")
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
val serieNo = IdentityCardInfo.serieNo
val validDate = IdentityCardInfo.validDate
val dateOfBirth = IdentityCardInfo.dateOfBirth
val fullname = IdentityCardInfo.fullName
val gender = IdentityCardInfo.gender
val identityNo = IdentityCardInfo.identityNo
val nationality = IdentityCardInfo.nationality
val bundle = bundleOf(
"TEST_TEXT" to message,
"SERIE_NO" to serieNo,
"VALID_DATE" to validDate,
"DOB" to dateOfBirth,
"FULL_NAME" to fullname,
"GENDER" to gender,
"IDENTITY_NO" to identityNo,
"NATIONALITY" to nationality
)
navController.navigate(
R.id.action_infoFragment_to_detailFragment,
bundle
)
}
}
}

这是针对ResultsFrage:(我不添加表格布局代码和视图寻呼机代码(

class ResultsFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_results, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val tabLayout: TabLayout = requireView().findViewById(R.id.tabLayout_results)
tabLayout.tabGravity = TabLayout.GRAVITY_FILL
val tabTitles = arrayOf(
resources.getString(R.string.result_tab1_name),
resources.getString(R.string.result_tab2_name),
resources.getString(R.string.result_tab3_name)
)
val viewPager: ViewPager = requireView().findViewById(R.id.viewpager_results)
viewPager.offscreenPageLimit = 3
val adapter = PagerAdapter(fragmentManager, tabTitles)
viewPager.adapter = adapter
tabLayout.setupWithViewPager(viewPager)
}
}

如果您想使用ViewModels在同一活动中的片段之间传递数据,这里有关于它的Android文档。它允许片段访问共享对象,而无需了解彼此,或通过活动进行通信(使用一堆代码来实现通信(

你基本上会这样做:

// create a ViewModel that holds the data you want to share
class IdentityCardViewModel : ViewModel() {
val identityInfo = MutableLiveData<IdentityCardInfo>()
fun setInfo(info: IdentityCardInfo) {
identityInfo.value = info
}
}

然后,每个片段将通过执行以下操作获取同一ViewModel实例的副本:

// Get a reference to the IdentityCardViewModel created and held by the activity
private val model: IdentityCardViewModel by activityViewModels()

然后CCD_ 2可以用CCD_。ResultsFragment可以观察这些数据,并在看到新值时采取措施:

model.identityInfo.observe(viewLifecycleOwner, Observer<IdentityCardInfo> { info ->
// Do whatever you need with the updated info, like displaying it
})

@muhhittin kaya参考以下代码来解决问题

InfoFragment

class InfoFragment : BaseFragment() {
lateinit var navController: NavController
// Initialize your variable
lateinit var infoFragmentInterface: InfoFragmentDataInterface
// If you get initialization exception then uncomment below line and comment above line
//private var  infoFragmentInterface: InfoFragmentDataInterface? = null

companion object {
private const val TAG = "Info Fragment"
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_info, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
button_info_idcard.setOnClickListener {
//start OcrActivity
val intent = Intent(activity, OcrActivity::class.java)
startActivityForResult(intent, 101)
}
imagebutton_info_settings.setOnClickListener {
navController.navigate(R.id.action_infoFragment_to_settingsFragment)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Log.i("Muhittin", "onActivityResult()")
if (requestCode == 101) {
val message = data?.getStringExtra("TEST_TEXT")
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
val serieNo = IdentityCardInfo.serieNo
val validDate = IdentityCardInfo.validDate
val dateOfBirth = IdentityCardInfo.dateOfBirth
val fullname = IdentityCardInfo.fullName
val gender = IdentityCardInfo.gender
val identityNo = IdentityCardInfo.identityNo
val nationality = IdentityCardInfo.nationality
val bundle = bundleOf(
"TEST_TEXT" to message,
"SERIE_NO" to serieNo,
"VALID_DATE" to validDate,
"DOB" to dateOfBirth,
"FULL_NAME" to fullname,
"GENDER" to gender,
"IDENTITY_NO" to identityNo,
"NATIONALITY" to nationality
)
//Add This line for passing your data
//You can also pass string data here
infoFragmentInterface.getInfoFragmentData(bundle)
navController.navigate(
R.id.action_infoFragment_to_detailFragment,
bundle
)
}
}
}
// Create this interface to pass your data
interface InfoFragmentDataInterface {
fun getInfoFragmentData(bundle: Any)
}

结果片段

class ResultsFragment : Fragment(), InfoFragmentDataInterface {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_results, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val tabLayout: TabLayout = requireView().findViewById(R.id.tabLayout_results)
tabLayout.tabGravity = TabLayout.GRAVITY_FILL
val tabTitles = arrayOf(
resources.getString(R.string.result_tab1_name),
resources.getString(R.string.result_tab2_name),
resources.getString(R.string.result_tab3_name)
)
val viewPager: ViewPager = requireView().findViewById(R.id.viewpager_results)
viewPager.offscreenPageLimit = 3
val adapter = PagerAdapter(fragmentManager, tabTitles)
viewPager.adapter = adapter
tabLayout.setupWithViewPager(viewPager)
}
override fun getInfoFragmentData(bundle: Any) {
// You'll get your Bundle data here.
}
}

相关内容

  • 没有找到相关文章

最新更新