简体   繁体   English

Android数据绑定导致包含布局问题

[英]Android Data binding causing problem in include layout

Hello hope you are all fine.你好希望你一切都好。 I migrated my project from old version to latest version ie 29. And I resolve all the errors.我将我的项目从旧版本迁移到最新版本,即 29。我解决了所有错误。 The problem is that I cannot resolve my toolbar error.问题是我无法解决我的工具栏错误。

<?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="controller"
            type="com.cvvm.Controller" />

        <variable
            name="menu"
            type="java.lang.Integer" />

        <variable
            name="itemClickListener"
            type="androidx.appcompat.widget.Toolbar.OnMenuItemClickListener" />

        <import type="com.bonnti.misc.NavigationClickListener" />
    </data>

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@drawable/btn_red_gradient"
        android:title="@{controller.title}"
        app:showBackButton="@{controller.previous != null}"
        app:itemClickListener="@{itemClickListener}"
        app:navigationClickListener="@{(NavigationClickListener)controller}"
        app:menu="@{menu}"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

</layout>

and I am including this in other layouts like this我把它包括在其他像这样的布局中

 <?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="controller"
            type="com.bonnti.screens.combthru.combthruitem.ItemCombthruController" />


        <import type="com.bonnti.R" />

        <import type="com.bonnti.misc.utils.TextUtils" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <RelativeLayout
            android:id="@+id/toolbar_item_combthru"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <include
                layout="@layout/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:controller="@{controller}" />

            <LinearLayout
                android:id="@+id/linearLayout2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginTop="4dp"
                android:layout_marginRight="@dimen/size_2dp"
                android:clickable="true"
                android:onClick='@{controller::onThreeDotsClick}'
                android:padding="@dimen/size_10dp"
                android:visibility='@{controller.isThreeDotsVisible}'>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="6dp"
                    android:src="@drawable/ic_point_first_white" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="6dp"
                    android:src="@drawable/ic_point"
                    android:tint="@color/white" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="6dp"
                    android:src="@drawable/ic_point"
                    android:tint="@color/white" />

            </LinearLayout>
        </RelativeLayout>


        <include
            android:id="@+id/list"
            layout="@layout/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar_item_combthru"
            android:layout_marginBottom="@dimen/size_40dp"
            app:adapter="@{controller.adapter}"
            app:loadMoreListener="@{controller}" />

        <RelativeLayout
            android:id="@+id/input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/white"
            android:padding="@dimen/size_10dp"
            android:visibility='@{controller.isCommentMode}'>

            <ImageView
                android:id="@+id/ic_im"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:scaleType="centerInside"
                app:srcCompat="@drawable/ic_combthru_comments" />

            <EditText
                android:id="@+id/et_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="@dimen/size_10dp"
                android:layout_marginRight="@dimen/size_10dp"
                android:layout_toStartOf="@+id/ic_sent"
                android:layout_toEndOf="@+id/ic_im"
                android:background="@null"
                android:hint="@string/comment"
                android:textColor="@color/textColor"
                android:textSize="@dimen/size_14sp" />

            <ImageButton
                android:id="@+id/ic_sent"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentRight="true"
                android:background="@null"
                android:onClick='@{controller::sentComment}'
                android:scaleType="centerInside"
                app:srcCompat="@drawable/ic_comb_sendcomment" />
        </RelativeLayout>


    </RelativeLayout>
</layout>



public abstract class Controller<B extends ViewDataBinding> extends SerializableController<B> {
    @Override Strategy<B> createStrategy() {
        return new FragmentStrategy<>(this);
    }
}

public abstract class BonntiController<B extends ViewDataBinding> extends Controller<B> implements NavigationClickListener {
    private static final String TAG = BonntiController.class.getSimpleName();


    public void onMediaSelected(String uri) {
        // stub
        Log.w(TAG, "onMediaSelected: activity result ignored");
    }

    protected void onBroadcastReceived(Intent intent){
        // stub
        Log.w(TAG, "onBroadcastReceived: activity result ignored");
    }


    //back arrow click
    @Override public void onNavigationClick() {
        back();
    }
}


public class ItemCombthruController extends BonntiController<LayoutItemCombthruBinding> implements RemoveCommentListener,
        LoadMoreListener {

    public final Combthru combthru;
    public BaseAdapter<BaseVM> adapter = new BaseAdapter<>(new ArrayList<>());
    public ObservableBoolean isCommentMode = new ObservableBoolean();

    public ItemCombthruController(Combthru combthru) {
        Log.d("BBB", "YES!!!");
        this.combthru = combthru;
        adapter.add(new ItemCombthruTopVM(combthru, this));
        if (combthru.comments != null && combthru.comments.size() != 0) {
            for (Combthru.Comment comment : combthru.comments) {
                adapter.add(new CombthruCommentVM(this, comment, this));
            }
        }
        isCommentMode.set(true);
    }
}

My problem is that it gives me below error.我的问题是它给了我以下错误。 I have tried everything from stackoverflow (clean project, invaidate cache/restart, rebuild project, include kotlin etc)and issue did not solve.我已经尝试了 stackoverflow 中的所有内容(清理项目、无效缓存/重启、重建项目、包括 kotlin 等),但问题没有解决。 When I press app:controller="@{controller}" it says that "cannot find declararion to go".当我按 app:controller="@{controller}" 时,它说“找不到要执行的声明”。 Any help will be appreciated.任何帮助将不胜感激。 Thanks in advance.提前致谢。

****/ data binding error ****msg:Cannot find the setter for attribute 'app:controller' with parameter type com.bonnti.screens.combthru.combthruitem.ItemCombthruController on com.bonnti.databinding.ToolbarBinding. ****/ 数据绑定错误 ****msg:在 com.bonnti.databinding.ToolbarBinding 上找不到参数类型为 com.bonnti.screens.combthru.combthruitem.ItemCombthruController 的属性“app:controller”的设置器。 file:D:\\bonnti-android-gitlab\\app\\src\\main\\res\\layout\\layout_item_combthru.xml文件:D:\\bonnti-android-gitlab\\app\\src\\main\\res\\layout\\layout_item_combthru.xml

You also need to change the included layout as a data binding layout.您还需要将包含的布局更改为数据绑定布局。 ie you need to wrap the included layout with the layout tag same as in your parent layout.即,您需要使用与父布局中相同的布局标记来包装包含的布局。

Use bind namespace to bind variables .使用bind命名空间来绑定变量。 Then your root layout should look like然后你的根布局应该看起来像

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

and Included One Should be和包括一个应该是

<include
   layout="@layout/toolbar"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   bind:controller="@{controller}" />

Ok so with Updated question what i found the type of controller variable is different in both layouts which should be same .好的,对于更新的问题,我发现两种布局中controller变量的类型不同,这应该是相同的。

So if you are using variable of type com.cvvm.Controller then it should be因此,如果您使用的是com.cvvm.Controller类型的变量,那么它应该是

<variable
        name="controller"
        type="com.cvvm.Controller" />

in both the layouts.在两种布局中。 and your main layout should be as你的主要布局应该是

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
    <variable name="controller" type="com.cvvm.Controller" />
</data>
..............
<include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        bind:controller="@{controller}" />
......................

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM