Использование привязки данных внутри макета ящика, где ящик содержит фрагмент - PullRequest
0 голосов
/ 03 февраля 2019

У меня есть Навигационный ящик, но я использую фрагмент внутри ящика.Я не могу получить как привязку данных, так и фрагмент, работающий вместе.С примером кода привязка данных не работает.Если я изменяю binding = ContentNewSessionBinding.inflate(getLayoutInflater()) на binding = DataBindingUtil.setContentView(this, R.layout.content_new_session_);, я получаю сообщение об ошибке, что фрагмент не может быть найден в представлении.

activity_new_session.xml

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

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <include
            layout="@layout/app_bar_new_session_"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true">

            <FrameLayout
                android:id="@+id/new_session_draw_frame"
                android:name="uk.co.alexanderjs.catchtrack2.SpeciesManager_Fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </android.support.design.widget.NavigationView>

    </android.support.v4.widget.DrawerLayout>
</layout>

content_new_session.xml

    <?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="WeatherModel"
                type="uk.co.alexanderjs.models.WeatherModel"/>
        </data>


        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/CL_content_new_session"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context=".NewSession_Activity"
            tools:showIn="@layout/app_bar_new_session_">



            <android.support.constraint.ConstraintLayout
                android:id="@+id/CL_info_tray"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">


                <android.support.v7.widget.AppCompatImageView
                    android:id="@+id/IV_location_type_info_tray"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_marginEnd="4dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@id/IV_watch_info_tray"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:srcCompat="@drawable/ic_cell_tower" />
               </android.support.constraint.ConstraintLayout>
        </android.support.constraint.ConstraintLayout>

NewSession_Activity.java

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_session)

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        toggle.setDrawerIndicatorEnabled(false);






        Class fragmentClass = SpeciesManager_Fragment.class;
        Fragment fragment = null;
        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e ) {
            e.printStackTrace();
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.new_session_draw_frame, fragment);
            fragmentTransaction.commit();
        }
        ContentNewSessionBinding binding = ContentNewSessionBinding.inflate(getLayoutInflater());
        binding.setWeatherModel(MainActivity.weatherModel);

    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...