Я хочу включить свой пользовательский вид с привязкой данных из фрагмента и передать значения для привязки.К сожалению, эти значения не передаются, геттеры всегда возвращают ноль.
Это мое пользовательское представление custom_view.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="label"
type="String" />
</data>
<com.my.package.CustomView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='@{label}' />
</com.my.package.CustomView>
и его код CustomView.java
public class CustomView extends ConstraintLayout {
CustomViewBinding binding;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
binding = CustomViewBinding.bind(this);
Log.i("binding", "l:" + binding.getLabel()); // I/binding: l:null
}
}
теперь это представление включено фрагментом. xml
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.HomeFragment">
<include
layout="@layout/custom_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:label="@{@string/main_menu_some_title}" />
<include
layout="@layout/custom_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:label='@{"asd"}' />
</LinearLayout>
Кроме того, MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle =
new ActionBarDrawerToggle(this, drawer, toolbar, string.navigation_drawer_open,
string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
DataBindingUtil.setContentView(this, R.layout.activity_main);
}
Оба метода (ссылочные и литеральные значения) дают одинаковый результат.Любая подсказка, что я могу делать не так?