ich Databinding in meinem Android-Projekt mit bin und meine dashboard_fragment_layout.xml Linearlayout enthalten, die Textview und Custom enthalten:generiert FragmentBinding enthält nicht meine Custom id
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.ui.dashboard.DashboardFragment">
<data>
<variable
name="ViewModel"
type="com.example.ui.dashboard.DashboardViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvSome"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<view
android:id="@+id/viewCustom"
class="com.example.ui.dashboard.CustomView"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="bottom|center_horizontal" />
</LinearLayout>
</layout>
Wenn ich über erzeugt versuchen, meine benutzerdefinierte Ansicht zugreifen FragmentDashboardBinding:
mCustomView = mBinding.viewCustom;
ich erhalte in Android Studio 'Kann Symbol viewCustom nicht lösen'. Und ich habe nicht dieses Problem mit Textview, es ist erreichbar von mBinding Objekt:
mSomeTextView = mBinding.tvSome; // all fine, no errors
Ich bin immer diesen Fehler mit benutzerdefinierten Ansichten bekommen, und der einzige Weg, um meine benutzerdefinierte Ansicht Objekt zuzugreifen, dies zu tun durch alte Art und Weise mit findViewById:
mCustomView = view.findViewById(R.id.viewCustom); // that works
Alle zusammen:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewDataBinding = DataBindingUtil.inflate(inflater, fragment_dashboard_layout, container, false);
view = mViewDataBinding.getRoot();
mCustomView = mViewDataBinding.viewCustom; // 'Can't resolve symbol viewCustom
mTextView = mViewDataBinding.tvSome; // all fine
mCustomView = view.findViewById(R.id.viewCustom); // that works
}
Wie kann ich benutzerdefinierte Ansichten über generierte Databinding-Objekt zugreifen?
Das funktioniert, danke! –
Viel Glück für Sie.Sie können meine Antwort akzeptieren.Vielen Dank. @ VladMorzhanov – KeLiuyue