来自片段中变量的数据绑定,android?
Data binding from variable in fragment, android?
我的片段中有一个字符串变量,我想在 XML 中使用它进行数据绑定
这是我的片段
class HomeFragment : Fragment() {
var struc: String = "Organization Structure"
lateinit var binding: FragmentHomeBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding = FragmentHomeBinding.inflate(inflater, container, false)
return binding.getRoot()
}}
这是XML
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="data"
type="com.mountmeru.view.HomeFragment" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.HomeFragment">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvOrganization"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ivOrganization"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="@{data.struc}"
android:textAlignment="center"
android:textColor="@color/mountmerured"
android:textSize="15sp" />
</FrameLayout></layout>
当我 运行 应用程序时,从未设置文本视图文本。
这里有什么问题?
我认为你在布局膨胀时弄错了。请尝试以下代码
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
return binding.getRoot()
}}
第一种方式:
This not direct way which you asked. But you can also do using data class, this is little bit long way but you can add multiple data.
制作数据class
data class Data(var struc: String = "Organization Structure")
在xml
中添加这个
<data>
<variable
name="dataModel"
type="Data" /> // here data class path of step1
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.HomeFragment">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvOrganization"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ivOrganization"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="@{dataModel.struc}" // use like this
android:textAlignment="center"
android:textColor="@color/mountmerured"
android:textSize="15sp" />
</FrameLayout></layout>
在Activity或片段
private fun setData() {
val data = Data(struc= "Organization")
bindObject.dataModel = data
}
第二种方式:
或者其他直接的方法是
<data>
<variable
name="struc"
type="String" />
</data>
来自 activity/fragment
bindObject.struc = "Organization Structure";
第三种方式:
或者在您的情况下,您需要在 onCreateView 中执行此操作并且一切正常
binding.data = this;
我的片段中有一个字符串变量,我想在 XML 中使用它进行数据绑定 这是我的片段
class HomeFragment : Fragment() {
var struc: String = "Organization Structure"
lateinit var binding: FragmentHomeBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding = FragmentHomeBinding.inflate(inflater, container, false)
return binding.getRoot()
}}
这是XML
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="data"
type="com.mountmeru.view.HomeFragment" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.HomeFragment">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvOrganization"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ivOrganization"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="@{data.struc}"
android:textAlignment="center"
android:textColor="@color/mountmerured"
android:textSize="15sp" />
</FrameLayout></layout>
当我 运行 应用程序时,从未设置文本视图文本。
这里有什么问题?
我认为你在布局膨胀时弄错了。请尝试以下代码
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
return binding.getRoot()
}}
第一种方式:
This not direct way which you asked. But you can also do using data class, this is little bit long way but you can add multiple data.
制作数据class
data class Data(var struc: String = "Organization Structure")
在xml
中添加这个<data> <variable name="dataModel" type="Data" /> // here data class path of step1 </data> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".view.HomeFragment"> <androidx.appcompat.widget.AppCompatTextView android:id="@+id/tvOrganization" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/ivOrganization" android:layout_marginTop="20dp" android:gravity="center" android:text="@{dataModel.struc}" // use like this android:textAlignment="center" android:textColor="@color/mountmerured" android:textSize="15sp" /> </FrameLayout></layout>
在Activity或片段
private fun setData() { val data = Data(struc= "Organization") bindObject.dataModel = data }
第二种方式:
或者其他直接的方法是
<data>
<variable
name="struc"
type="String" />
</data>
来自 activity/fragment
bindObject.struc = "Organization Structure";
第三种方式:
或者在您的情况下,您需要在 onCreateView 中执行此操作并且一切正常
binding.data = this;