使用绑定从片段访问父 activity 的 UI 元素

Using Binding for accessing UI element of parent activity from fragment

所以,我正在尝试从 kotlin 合成迁移到 Jetpack 视图绑定。

这是 kotlin 合成代码(工作正常),它只是将片段中父 activity 中 TextView 的可见性设置为不可见。

import kotlinx.android.synthetic.main.activity_main.*

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

        requireActivity().textView.visibility = View.INVISIBLE
        return view
    }
}

下面是我要迁移的内容:

import com.mypc.myapp.databinding.FragmentFirstBinding
import com.mypc.myapp.databinding.ActivityMainBinding
    
class FirstFragment : Fragment() {
    
    private var _binding: FragmentFirstBinding? = null
    private val binding get() = _binding!!
    
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View {
            _binding = FragmentFirstBinding.inflate(inflater, container, false)
            return binding.root
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    
            binding.textView.visibility = View.INVISIBLE
            binding.textview.setOnClickListener {
            Navigation.findNavController(view).navigate(R.id.goto_secondfragment)
        }
        }
    
        override fun onDestroyView() {
            super.onDestroyView()
            _binding = null
        }
    }

我在 'textview' 收到错误 'Unsolved reference':

binding.textView.visibility = View.INVISIBLE

在:

binding.textview.setOnClickListener {
            Navigation.findNavController(view).navigate(R.id.goto_secondfragment)
        }

显然编译器无法找到 Activity 中的 TextView 我添加了这一行:

import com.mypc.myapp.databinding.ActivityMainBinding

由于您的 bindingMainActivity 是私有的,因此您只能从 MainActivity 中引用您的 textView。要从 FirstFragment show/hide 此视图,您可以在 MainActivity 中创建一个 public 函数并从您的 FirstFragment.

中调用它
class MainActivity: AppCompatActivity {

    private var _binding: ActivityMainBinding? = null
    private val binding get() = _binding!!

    fun showHideTextView(visible: Boolean) {
        binding.textView.isVisible = visible
    }
}

在你的片段中,你可以调用:

(requireActivity() as MainActivity).showHideTextView(false) // This will hide the textView

首先,您应该在 baseActivity 中定义 activity 视图绑定的实例,它是 MainActivity 的父级 class,然后定义方法来更改您的文本视图,例如 'showTextView' ,之后在基础片段 class 中初始化基础 activity 实例,并在 onAttach 方法中使用铸造上下文对象。 我给你一些代码:

abstract class BaseRegisterActivity : BaseActivity() {

    //---

    protected lateinit var binding: ActivityRegisterBinding

    private val navHostFragment by lazy {
        supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as 
        NavHostFragment
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_register)

        //---
    }

     fun showTextView() {
        binding.textView.visibility = View.VISIBLE
        binding.textView.setOnClickListener {
            val fragment =
                navHostFragment.childFragmentManager.fragments[0]

            if (fragment is FirstFragment) {
               //todo
            }
        }
    }

}
abstract class BaseFragment : Fragment(), Injectable {

    //--

    lateinit var baseActivity: BaseRegisterActivity

   
    override fun onAttach(context: Context) {
        super.onAttach(context)
        baseActivity = context as BaseRegisterActivity
    }
    
    
    //--
}