替换视图时 ViewBinding 无法正常工作

发布于 2025-01-13 23:30:56 字数 2457 浏览 0 评论 0原文

我正在将一个旧项目转换为 viewBinding。 BaseActivity 有一个协调器布局。应用程序更改以 main_view 作为 id 的相对布局的视图。例如,当用户打开WorkActivity时,main_view与activity_work切换。

activity_base.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_main_screen"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <include
        android:id="@+id/toolbar_layout"
        layout="@layout/toolbar_layout" />

    <RelativeLayout
        android:id="@+id/main_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

    <RelativeLayout
        android:id="@+id/title_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="gone" />

    <RelativeLayout
        android:id="@+id/bottom_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="gone" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

BaseActivity 中的replaceViews 函数:

    private void replaceViews(View holder, int layoutID) {
        View toReplace = getLayoutInflater().inflate(layoutID, binding.coordinator, false);
        ViewTools.replaceView(holder, toReplace);
    }

viewTools.replaceView 函数:

    public static void replaceView(View currentView, View newView) {
        ViewGroup parent = getParent(currentView);
        if (parent == null) {
            return;
        }
        final int index = parent.indexOfChild(currentView);
        removeView(currentView);
        removeView(newView);
        parent.addView(newView, index);
    }

下面的代码片段(来自BaseActivity 中的replaceViews 函数)似乎很麻烦,因为绑定没有效果或者布局充气器不会膨胀视图。

    View toReplace = getLayoutInflater().inflate(layoutID, binding.coordinator, false);

viewBinding 与这样的替换操作兼容吗?我是否忘记了layoutInflater的任何操作?

I am converting an old project to viewBinding. BaseActivity has a coordinator layout. App changes view of relative layout which has main_view as id. For example when user opens WorkActivity, main_view switches with activity_work.

activity_base.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_main_screen"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <include
        android:id="@+id/toolbar_layout"
        layout="@layout/toolbar_layout" />

    <RelativeLayout
        android:id="@+id/main_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

    <RelativeLayout
        android:id="@+id/title_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="gone" />

    <RelativeLayout
        android:id="@+id/bottom_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="gone" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

replaceViews function in BaseActivity:

    private void replaceViews(View holder, int layoutID) {
        View toReplace = getLayoutInflater().inflate(layoutID, binding.coordinator, false);
        ViewTools.replaceView(holder, toReplace);
    }

viewTools.replaceView function :

    public static void replaceView(View currentView, View newView) {
        ViewGroup parent = getParent(currentView);
        if (parent == null) {
            return;
        }
        final int index = parent.indexOfChild(currentView);
        removeView(currentView);
        removeView(newView);
        parent.addView(newView, index);
    }

Code snippet below (from replaceViews function in BaseActivity) seems to be the trouble, since binding has no effect or layout inflater does not inflate the view.

    View toReplace = getLayoutInflater().inflate(layoutID, binding.coordinator, false);

Is viewBinding compatible with replace operations like this? Did I forget any operation for layoutInflater?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文