【问题标题】:onclick using databinding in fragmentonclick 在片段中使用数据绑定
【发布时间】:2020-03-21 15:54:52
【问题描述】:

我正在尝试在 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);

事件处理程序类

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>

【问题讨论】:

    标签: android android-fragments mvvm data-binding


    【解决方案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">
    

    【讨论】:

    • 尝试删除此行View root = inflater.inflate(R.layout.fragment_home, container, false);
    • 这就是问题所在,我绝对是白痴。谢谢。
    【解决方案2】:
      <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
       }
    

    你的方法应该是这样的。

    【讨论】:

    • 我已经更新了xml,请您再看一遍,谢谢
    【解决方案3】:

    试试这个:

     <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" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 1970-01-01
      • 2022-08-04
      • 2014-01-25
      • 1970-01-01
      • 2018-12-27
      相关资源
      最近更新 更多