Bind data in an android:include element

65 views
Skip to first unread message

eniot...@gmail.com

unread,
Jul 21, 2012, 4:17:56 PM7/21/12
to android...@googlegroups.com
Hi,

Is it possible to bind data on an android:include element ?

For example, imagine I have the following class:
public class Player
{
  public StringObservable Name;
}

I have a view call player.xml which is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:binding="http://www.gueei.com/android-binding/"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:gravity="center_horizontal">

<TextView
        android:id="@+id/playerName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Computer #1"
        android:textStyle="bold"
        binding:text="Name"
/>
</LinearLayout>

I also have my viewmodel class:
public class MainViewModel
{
   public Observable<Player> HumanPlayer;
   public ArrayListObservable<Player> OtherPlayers;
}

In my main view (main.xml) I would like to use my view "player.xml" with an android:include element and be able to say to my view to use my "HumanPlayer" object for binding:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:binding="http://www.gueei.com/android-binding/"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
      
    <!-- SOME PIECES OF XML -->
   
    <GridView
        android:id="@+id/gvPlayers"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:numColumns="3"
        binding:itemSource="OtherPlayers"
        binding:itemTemplate="@layout/player"
        />
    <!-- OTHER PIECES OF XML -->
     <include layout="@layout/player" datasource="HumanPlayer"/>
</LinearLayout>

Is it possible ? How ?
Thanks a lot for your help.
Regards

Antoine


martin martinez

unread,
Jul 21, 2012, 7:50:11 PM7/21/12
to android...@googlegroups.com
I can say Yes because I've done it and it works :).

Martin

Andy Tsui

unread,
Jul 21, 2012, 7:51:20 PM7/21/12
to android...@googlegroups.com, eniot...@gmail.com
Consider using BindableFrameLayout. 

The BindableFrameLayout is an empty container (frame layout) that accepts another layout xml and view model as data source, which I believe it can do exactly what you need above. 

The usage is included in the markup demo ics. 

Andy Tsui

unread,
Jul 21, 2012, 8:05:19 PM7/21/12
to android...@googlegroups.com
<include> could work, but it is not too reusable, since there's no way to specific "datasource" to <include>. So for example you want:

  <somelayout>
   <include layout="player" datasource="human"/>
   <include layout="player" datasource="android"/> 
 </somelayout>

Above should not work. But

  <somelayout>
   <binding.BindableFrameLayout layoutId="player" dataSource="human"/>
   < binding.BindableFrameLayout  layoutId="player" dataSource="android"/> 
 </somelayout>

you can specify the data source. 

martin martinez

unread,
Jul 21, 2012, 8:08:09 PM7/21/12
to android...@googlegroups.com
Agree, but if you want to just reuse a control I think the <include> it's easier, though not sure about the difference on performance.

Andy Tsui

unread,
Jul 21, 2012, 8:09:58 PM7/21/12
to android...@googlegroups.com
include should be much faster. Since BFL is a widget. 

eniot...@gmail.com

unread,
Aug 6, 2012, 6:33:42 PM8/6/12
to android...@googlegroups.com, eniot...@gmail.com
Hi,

Sorry for my late answer, I was on holidays. I used BindableFrameLayout that seems to be exactly what I need and it worked great.
Thanks for your precious help !

Antoine

dened...@gmail.com

unread,
Oct 10, 2014, 5:57:36 AM10/10/14
to android...@googlegroups.com
Hi Andy,
 human and android in datasource should have been different viewmodel class or a Arraylist in Main ViewModel Class ?
Reply all
Reply to author
Forward
0 new messages