我正在尝试在Fragment中实现数据绑定,但是它不起作用,相同的代码正在Activity中起作用。
片段类
final FragmentHomeBinding fragmentHomeBinding = DataBindingUtil.inflate(
inflater, R.layout.fragment_home, container, false);
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
fragmentHomeBinding.setVariable(BR.homeViewModel, homeViewModel);
HomeEventHandler homeEventHandler = new HomeEventHandler(getActivity());
fragmentHomeBinding.setHandler(homeEventHandler);
EventHandlerClass
HomeEventHandler(Context mContext) {
this.mContext = mContext;
}
public void onButtonClick(View view) {
Log.e("DB", "onButtonClick: ");
switch (view.getId()){
case R.id.frag_home_ll_stock:
Toast.makeText(mContext, "clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(mContext, StockCheckActivity.class);
mContext.startActivity(intent);
}
}
最后是xml文件
<data>
<import type="android.view.View" />
<variable
name="fragment"
type="com.poc.ui.home.HomeFragment"/>
<variable
name="handler"
type="com.poc.ui.home.HomeEventHandler"/>
</data>
<LinearLayout
android:onClick="@{view->handler.onButtonClick(view)}"
android:id="@+id/frag_home_ll_stock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/grey_25"
android:clickable="true"
android:focusable="true"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp">
<!--
-->
<TextView
android:id="@+id/frag_home_tv_op4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:clickable="false"
android:text="@string/w_stock_check"
android:textColor="@color/black"
android:textSize="18sp" />
<ImageView
android:id="@+id/frag_home_iv_op4"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:clickable="false"
app:srcCompat="@drawable/ic_stock" />
</LinearLayout>
答案 0 :(得分:1)
从您的Fragment
删除以下行
View root = inflater.inflate(R.layout.fragment_home, container, false);
如果需要视图,则可以使用如下所示的参数
android:onClick="@{(view) -> handler.onButtonClick(view)}"
您的LinearLayout
应该如下所示。
<LinearLayout
android:onClick="@{(view)->handler.onButtonClick(view)}"
android:id="@+id/frag_home_ll_stock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/grey_25"
android:clickable="true"
android:focusable="true"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp">
答案 1 :(得分:0)
尝试这个:
<ImageView
android:onClick="@{(view) -> handler.onButtonClick()}"
android:id="@+id/frag_home_iv_op3"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:srcCompat="@drawable/ic_report" />
答案 2 :(得分:0)
<variable
name="HomeviewModel"
type="com.poc.ui.home.HomeviewModel"/> // exact path
you need to declare the viewmodel variable
android:onClick="@{(v) -> homeViewModel.buttonClicked(v, true)}"
if you are passing the parameter the function also should also contain same parameter
for e.g,
private void buttonClicked(View view, boolean value){
// your code
}
您的方法应如下所示。