如何将数据绑定到标头?
我有一个主要活动,在操作栏中有一个侧面导航抽屉,在default_screen.xml中指定如下(请注意,为简洁起见,省略了很多代码):
I have a main activity with a side navigation drawer in the action bar, specified as follows (note that a lot of code has been omitted for brevity) in default_screen.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="@drawable/honeycomb"
android:orientation="vertical"
>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
/>
布局/标题如下(同样,为简洁起见,省略了许多行):
where layout/header is as follows (again, a bunch of lines omitted for brevity):
<data>
<variable name="user" type="oose2017.place2b.ClientUser"/>
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.displayName}"
android:textSize="14sp"
android:textColor="#FFF"
android:textStyle="bold"
android:gravity="left"
android:paddingBottom="4dp"
android:id="@+id/username"
android:layout_above="@+id/email"
android:layout_alignLeft="@+id/profile_image"
android:layout_alignStart="@+id/profile_image" />
</RelativeLayout>
我在主活动中实例化default_screen的位置如下:
Where I instantiate my default_screen in the main activity as follows:
setContentView(R.layout.default_screen);
如何将数据绑定到标题?我尝试了一些失败的尝试,主要是:
How do I data bind to the header? I have tried a few things unsuccessfully, mainly:
DefaultScreenBinding binding = DataBindingUtil.setContentView(R.layout.default_screen);
这不起作用.我该怎么办?
Which does not work. How can I do this?
有效的解决方案如下,在主要活动OnCreate中添加标题视图:
Valid solution will be following, add header view in main activity OnCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
NavHeaderMainBinding _bind = DataBindingUtil.inflate(getLayoutInflater(), R.layout.nav_header_main, binding
.navView, false);
binding.navView.addHeaderView(_bind.getRoot());
_bind.setUser(Session.getUserProfile());
}