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?