Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to data bind to a header?

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> 

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?

like image 958
Eric Avatar asked Nov 27 '15 17:11

Eric


People also ask

What is data binding with example?

Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change. For example, if the user edits the value in a TextBox element, the underlying data value is automatically updated to reflect that change.

How do you use data binding with tag?

Data Binding in <include> Layouts xml layout to enable data binding. The <data> and <variable> tags are used to bind the student object. To pass the user to included content_student_main layout, bind:student=”@{student}” is used. Without this, the user object won't be accessible in content_student_main layout.


1 Answers

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()); } 
like image 184
Nick Kucherenko Avatar answered Oct 11 '22 04:10

Nick Kucherenko