ViewBinding在片段中不进行任何更改

发布于 2025-01-17 20:16:46 字数 1819 浏览 1 评论 0原文

我的目标

我正在尝试使用ViewBinding在我的片段中创建的小部件。

我所做的/有关我的应用程序的信息

  • 我正在使用的语言是kotlin。

  • 我已经将下面的代码添加到Gradle中:

    buildfeatures { databinding = true ViewBinding = true }

  • 我已经测试了 binding.atextview.setText(“代码工作。”)在我的主要活动中,它起作用。

问题是什么

我已经在活动中测试了SetText代码,并且有效。现在的问题是我进入片段时的代码相同,这是行不通的。而且我确信该代码已被执行,因为我将烤面包放在其上方,并且吐司成功执行,这意味着它至少应该已经达到了这一点,但由于什么原因没有任何更改,因此不确定。

我的主攻击代码:

class MainProgramActivity : AppCompatActivity() {
lateinit var binding: ActivityMainProgramBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainProgramBinding.inflate(layoutInflater)
    setContentView(binding.root)
    replaceFragment(FragmentMainPage())
}
private fun replaceFragment(fragment: Fragment){
    val fragmentManager = supportFragmentManager
    val fragmentTransaction = fragmentManager.beginTransaction()
    fragmentTransaction.replace(R.id.fragmentContainerView,fragment)
    fragmentTransaction.commit()
}

}

我的片段代码:

class FragmentMainPage : Fragment(R.layout.fragment_main_page) {

lateinit var binding: FragmentMainPageBinding

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    Toast.makeText(getActivity(),"Text!",Toast.LENGTH_SHORT).show();
    binding = FragmentMainPageBinding.inflate(layoutInflater)
    binding.aTextView.setText("Code working") //<-- I want this code to make changes towards the textView
    return super.onCreateView(inflater, container, savedInstanceState)
}

}

atextView本身在开始时是空的,预期的结果将是显示“代码工作”的AtextView。

My Goal

I am trying to access the widget that was created inside my fragment using viewBinding.

What I have done / Info about my app

  • The language I am using is kotlin.

  • I have already added the code below into gradle:

    buildFeatures{
    dataBinding = true
    viewBinding = true
    }

  • I have tested binding.aTextView.setText("Code working.") inside my main activity and it works.

What's the problem

I have tested the setText code inside activity and it works. The problem right now is the same code when I move into the fragment it wouldn't work. And I am sure that the code has been executed as I putted a toast above it and the toast executed successfully which mean it should have at least reached that point before but not sure due to what reason there wasn't any changes.

My mainActivity Code:

class MainProgramActivity : AppCompatActivity() {
lateinit var binding: ActivityMainProgramBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainProgramBinding.inflate(layoutInflater)
    setContentView(binding.root)
    replaceFragment(FragmentMainPage())
}
private fun replaceFragment(fragment: Fragment){
    val fragmentManager = supportFragmentManager
    val fragmentTransaction = fragmentManager.beginTransaction()
    fragmentTransaction.replace(R.id.fragmentContainerView,fragment)
    fragmentTransaction.commit()
}

}

My fragment code:

class FragmentMainPage : Fragment(R.layout.fragment_main_page) {

lateinit var binding: FragmentMainPageBinding

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    Toast.makeText(getActivity(),"Text!",Toast.LENGTH_SHORT).show();
    binding = FragmentMainPageBinding.inflate(layoutInflater)
    binding.aTextView.setText("Code working") //<-- I want this code to make changes towards the textView
    return super.onCreateView(inflater, container, savedInstanceState)
}

}

The aTextView itself is empty at the beginning, the expected result will be the aTextView to show "Code working".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大海や 2025-01-24 20:16:46

我发现您的代码有两个问题。首先,正如迈克尔所指出的那样。当您应该返回刚刚创建的视图 (binding.root) 时,您正在返回 super 方法。其次,你目前正在泄露你的片段。当您视图绑定片段时,您应该在 onDestroyView() 中将变量设置为 null,按照 文档

class FragmentMainPage : Fragment(R.layout.fragment_main_page) {

  private var _binding: FragmentMainPageBinding? = null
  private val binding get() = _binding!! // non-null variable in order to avoid having safe calls everywhere

  // create the view through binding
  override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View? {
    _binding = FragmentMainPageBinding.inflate(layoutInflater, container, false)
    return binding.root
  }

  // view already created, do whatever with it
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    binding.aTextView.setText("Code working")
  }

  // clear the binding in order to avoid memory leaks
  override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
  }
}

I see two problems with your code. First, exactly what Michael pointed out. You're returning the super method when you should be returning the View you just created (binding.root). Second, you're currenly leaking your fragment. When you viewbind a fragment, you are supposed to set the variable to null in onDestroyView(), as per defined in the documentation.

class FragmentMainPage : Fragment(R.layout.fragment_main_page) {

  private var _binding: FragmentMainPageBinding? = null
  private val binding get() = _binding!! // non-null variable in order to avoid having safe calls everywhere

  // create the view through binding
  override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View? {
    _binding = FragmentMainPageBinding.inflate(layoutInflater, container, false)
    return binding.root
  }

  // view already created, do whatever with it
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    binding.aTextView.setText("Code working")
  }

  // clear the binding in order to avoid memory leaks
  override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文